java - 我将如何编写按钮 onclick 事件?

标签 java android events onclick android-event

目的:让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();但我得到了

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++;

    }
}

最佳答案

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

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

关于java - 我将如何编写按钮 onclick 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10668912/

相关文章:

java - 服务器名称指示支持 java 1.6

java - 如何在 Swift 中创建一个字典/ map ,其中日期作为键, boolean 值作为值?

java - 缺少数据库连接 eclipse

java - Java中是否有类似Scheme的 "exact division"之类的东西?

android - 通过 Xamarin 和 C# 使用和构建第 3 方控件

android动画切换UI和active类

java - Swing Dynamical JCombobox 检测用户的选择更改

java - 解析数据时出错 java.lang.IllegalStateException : Not on the main thread when populating Google map

angularjs - AngularJS 中多个子 Controller 的事件命名空间

c# - 当属性在 C# 中只有 getter 时如何创建属性更改事件