0

目的:让 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++;

    }
}
4

1 回答 1

2

您需要获得对该按钮的引用,然后才能使用它。

Button button1 = yourview.findViewById(r.id.button1);
button1.performClick();
于 2012-05-19T21:24:41.457 回答