Android-button event listening, android event listening

Source: Internet
Author: User

Android-button event listening, android event listening

Summary of the four event listening methods for the Button

First, define a Button space in activity_main.xml.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity" >    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:text="Button" /></RelativeLayout>

Well, now let's look at the four ways to write click events:

1. The most common method is to use an anonymous internal class as the event listener class.

package com.basillee.blogdemo;import android.os.Bundle;import android.app.Activity;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity {    private Button btnButton;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        btnButton=(Button)findViewById(R.id.button1);        btnButton.setOnClickListener(new OnClickListener() {            public void onClick(View arg0) {                Toast.makeText(MainActivity.this, "button clicked", Toast.LENGTH_LONG).show();            }        });    }}

 

2. Internal class as listener

package com.basillee.blogdemo;import android.os.Bundle;import android.app.Activity;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity {private Button btnButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btnButton=(Button)findViewById(R.id.button1);MyClicked myClicked=new MyClicked();btnButton.setOnClickListener(myClicked);}private class MyClicked implements OnClickListener{@Overridepublic void onClick(View arg0) {Toast.makeText(getApplicationContext(), "Button clicked!", Toast.LENGTH_LONG).show();}}}

3. The Activity directly implements the OnClickListener interface.

Package com. basillee. blogdemo;

Import android. OS. Bundle;
Import android. app. Activity;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. Toast;

Public class MainActivity extends Activity implements OnClickListener {

Private Button btnButton;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
BtnButton = (Button) findViewById (R. id. button1 );
BtnButton. setOnClickListener (this );
}
@ Override
Public void onClick (View arg0 ){
Toast. makeText (this, "Button clicked! ", Toast. LENGTH_LONG). show ();
}

}

4. In the layout file, use the android: onClick attribute to specify the corresponding method name.

The activity_main.xml code is as follows:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity" >    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:text="Button"         android:onClick="myClick"/></RelativeLayout>

The MainActivity. java code is as follows:

package com.basillee.blogdemo;import android.os.Bundle;import android.app.Activity;import android.view.View;import android.widget.Toast;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void myClick(View v){Toast.makeText(getApplicationContext(),"Button clicked!", Toast.LENGTH_LONG).show();}}

  

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.