目的:使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();

10-05 17:45
查看更多