目的:让if语句通过代码执行onclick事件。
我目前在 xml 中有此按钮。
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Set Call"
android:onClick="makeCall"/>
我尝试过R.id.button1.performClick();
但我得到了
Cannot invoke performClick() on the primitive type int
如果计数低于 5,我将重复以下条件。
public void onActivityResult(int requestCode, int resultCode, Intent data) {
int count=0;
if (count <= 5) {
//make call
R.id.button1.performClick();
count++;
Toast toast=Toast.makeText(this, "Count is currently" + count++ + ",
repeating", Toast.LENGTH_LONG);
toast.show();
}
else {
// Toast Popup when call set button pressed
Toast toast=Toast.makeText(this, "Call count complete, ending method" ,
Toast.LENGTH_LONG);
toast.show();
count++;
}
}
最佳答案
您需要获取对该按钮的引用,然后才能使用它。
Button button1 = yourview.findViewById(r.id.button1);
button1.performClick();
关于java - 我将如何编写按钮 onclick 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10668912/