可能性添加的按钮XML参数? [英] Possibility to add parameters in button xml?

查看:112
本文介绍了可能性添加的按钮XML参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有一些按钮的活动。

I currently have an activity with some buttons.

在我的XML,按键的定义是这样的:

In my xml, buttons are defined like this:

    <ImageButton (...) android:onClick="GoToPageX"/>

和我有我的活动:

public void GotoPageX() {
    startActivity(new Intent(this, PageX.class));
    finish();

}

现在的问题是,我有数百个按钮和不想写

The problem is that I have hundreds of buttons and do not want to write

<ImageButton (...) android:onClick="GoToPage1"/>
<ImageButton (...) android:onClick="GoToPage2"/>
<ImageButton (...) android:onClick="GoToPage3"/>
...
<ImageButton (...) android:onClick="GoToPage100"/>

和所有的脚本。

我现在用

public void GotoPage( int i) {
    startActivity(new Intent(getBaseContext(), activities.get(i)));
    finish();
}

和想从XML给参数i,这可能吗?

and would like to give the parameter i from the xml, is that possible?

感谢很多的任何帮助。

推荐答案

这是不能直接成为可能。但是,也许你可以使用机器人:标签来让你的参数

It is not directly possible. However, maybe you could use android:tag to get your parameter.

<ImageButton (...) android:onClick="goToPage" android:tag="25"/>

public void goToPage(View v) {
    String pageNumber = v.getTag(); 
    /* ... */
}

这篇关于可能性添加的按钮XML参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆