About five different onClick events

Posted by andy2006 on Fri, 22 Nov 2019 21:36:42 +0100

In the development, the onclick event of the button is often used to do some operations. The android onclick event is usually used in five ways. This paper takes android Toast as an example

First, we create a new project and add a button in the view activity main.xml file. The code is as follows:

 
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/activity_main"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="com.extemp.MainActivity">



<Button

android:text="Click to run"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/mBt_run"/>

</RelativeLayout>

 

 

The first onclick method: anonymous inner class, added in MainActivity.java class.

 

public class MainActivity extends AppCompatActivity {



private Button mBt_run;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mBt_run = (Button) findViewById(R.id.mBt_run);

mBt_run.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Toast.makeText(getApplicationContext(),"Clicked",Toast.LENGTH_SHORT).show();

}

});

}

}

 

 

The second one: anonymous inner class is used externally, similar to the first one.

 

public class MainActivity extends AppCompatActivity {



private Button mBt_run;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mBt_run = (Button) findViewById(R.id.mBt_run);

mBtClick();

}

//External methods use anonymous inner classes

public void mBtClick(){

mBt_run.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Toast.makeText(getApplicationContext(),"Clicked",Toast.LENGTH_SHORT).show();

}

});

}

}

 

 

Third, implement the View.OnClickListener interface in MainActivity.java class.

 

public class MainActivity extends AppCompatActivity implements View.OnClickListener{



private Button mBt_run;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mBt_run = (Button) findViewById(R.id.mBt_run);

//You have to register here

mBt_run.setOnClickListener(this);

}

@Override

public void onClick(View v) {

Toast.makeText(getApplicationContext(),"Clicked",Toast.LENGTH_SHORT).show();

}

}

 

 

The fourth is to define internal classes and implement the View.OnClickListener interface.

 

public class MainActivity extends AppCompatActivity{



private Button mBt_run;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mBt_run = (Button) findViewById(R.id.mBt_run);

mBt_run.setOnClickListener(new MyOnclick());

}

//Define internal classes and implement the View.OnClickListener interface

class MyOnclick implements View.OnClickListener{

@Override

public void onClick(View v) {

Toast.makeText(getApplicationContext(),"Clicked",Toast.LENGTH_SHORT).show();

}

}

}

 

Fifth, define onclick events directly in the activity main.xml file, and add onclick attributes to the view file.

 

<Button

android:text="Click to run"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:onClick="clickAlert"/>

We implement the clickAlert method directly in the MainActivity.java file, which is the simplest way.

public class MainActivity extends AppCompatActivity{



private Button mBt_run;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mBt_run = (Button) findViewById(R.id.mBt_run);

}

public void clickAlert(View view){

Toast.makeText(getApplicationContext(),"Clicked",Toast.LENGTH_SHORT).show();

}



}

 

The above five methods of writing android onclick click events have the same effect.

Topics: Android xml Java encoding