有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何编写按钮onclick事件?

目的:让if语句通过代码执行onclick事件

我目前在xml中有这个按钮

<Button 
   安卓:id="@+id/button1" 
   安卓:layout_width="fill_parent" 
   安卓:layout_height="wrap_content" 
   安卓:text="Set Call" 
   安卓: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++;

    }
}

共 (1) 个答案

  1. # 1 楼答案

    你需要获得按钮的引用,然后才能使用它

    Button button1 = yourview.findViewById(r.id.button1);
    button1.performClick();