【问题标题】:How would I code a button onclick event?我将如何编写按钮 onclick 事件?
【发布时间】:2012-05-19 21:15:45
【问题描述】:

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

    }
}

【问题讨论】:

  • 嗯,这个信息很清楚——一个 id 是一个整数,事实上,你不能在它上面调用performClick。如果您想以编程方式调用按钮的处理程序,您需要首先实际获取资源作为按钮。但与你所说的你想要的相比,这是倒退的。您是否看过任何 Android UI 教程?

标签: java android events onclick android-event


【解决方案1】:

你需要得到一个按钮的引用,然后你才能使用它。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-16
    • 1970-01-01
    • 2013-12-13
    • 2011-12-21
    • 1970-01-01
    • 2011-03-27
    • 2020-10-18
    • 1970-01-01
    相关资源
    最近更新 更多