我将如何编写按钮 onclick 事件?
-
11-12-2019 - |
题
目的:让 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();
但我得到
无法对基本类型 int 调用 PerformClick()
如果计数低于 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();
不隶属于 StackOverflow