简体   繁体   English

我可以使用 android:onClick 按钮属性调用方法名称“onClick”或“onclick”吗?

[英]Can i call method name “onClick” or “onclick” using android:onClick button attribute?

Xml File: Xml 文件:

 <RelativeLayout 
        xmlns:android="http:// schemas.android.com/apk/res/android"
        xmlns:app="http:// schemas.android.com/apk/res-auto"
        xmlns:tools="http:// schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.sample.MainActivity" > 
      
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button_send"
            android:onClick="onClick"/> 
    </RelativeLayout>

Java File: Java 文件:

MainActivity.java:
public class MainActivity extends AppCompatActivity{
  
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main);
    } 

    public void onClick(View view) 
    { 
        switch (view.getId()) { 
        case R.id.button_send: 
            // Do something 
        } 
    } 
}

So can I call a method named "onClick" or "onclick" using android:onClick attribute?那么我可以使用android:onClick属性调用名为“onClick”或“onclick”的方法吗?

And how exactly this method works?这种方法究竟是如何工作的?

Of course you can.当然可以。 In the XML file, android:onClick="onClick" denotes the function name 'onClick' which should run when the button is clicked.在 XML 文件中,android:onClick="onClick" 表示 function 名称“onClick”应在单击按钮时运行。 As you have defined the function in your java file, there should not be any issues.正如您在 java 文件中定义的 function 一样,应该没有任何问题。

Yes, you can.是的你可以。 Create a method with any name and provide an object of View class as a formal parameter.创建一个任意名称的方法,并提供一个视图 class 的 object 作为形式参数。 Write the code inside this method which you want to be executed when the user clicks the button.在此方法中编写您希望在用户单击按钮时执行的代码。 In the design window, if you click on the desired button, you will find the onClick option in the right pane.在设计 window 中,如果单击所需的按钮,您将在右侧窗格中找到 onClick 选项。 Click on the drop down arrow beside it and you can see the list of methods that can be called when you press the button.单击它旁边的下拉箭头,您可以看到按下按钮时可以调用的方法列表。 Select the desired method. Select 所需的方法。 Alternatively, you can directly type the method name over there.或者,您可以在那里直接键入方法名称。

Yes.是的。

When the user clicks a button, the Button object receives an on-click event.当用户点击一个按钮时,Button object 会收到一个点击事件。

To define the click event handler for a button, add the android:onClick attribute to the element in your XML layout.要定义按钮的单击事件处理程序,请将 android:onClick 属性添加到 XML 布局中的元素。 The value for this attribute must be the name of the method you want to call in response to a click event.此属性的值必须是您要调用以响应单击事件的方法的名称。 The Activity hosting the layout must then implement the corresponding method.托管布局的 Activity 必须实现相应的方法。

    /** Called when the user touches the button */
    public void sendMessage(View view) {
        // Do something in response to button click
    }

The method you declare in the android:onClick attribute must have a signature exactly as shown above.您在 android:onClick 属性中声明的方法必须具有完全如上所示的签名。 Specifically, the method must:具体来说,该方法必须:

  1. Be public公开
  2. Return void返回无效
  3. Define a View as its only parameter (this will be the View that was clicked)定义一个视图作为它的唯一参数(这将是被点击的视图)

Source: https://developer.android.com/guide/topics/ui/controls/button来源: https://developer.android.com/guide/topics/ui/controls/button

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM