可能性添加的按钮XML参数?可能性、按钮、参数、XML

2023-09-12 04:55:34 作者:Poker Face"

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

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(); 
    /* ... */
}