1.   Inherit listening interface .

2.  xml The way : Set up android:onClick

3. Inner class

4. An anonymous class


1. Realization OnClickListener Interface

package com.itheima28.caller;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText; /**
* @author andong
* Just run the program on the display of the interface
public class MainUI4 extends Activity implements OnClickListener { /**
* Call back this method when the interface is just created
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // This code must be executed . Perform the initialization of the parent class . setContentView(R.layout.main); // Set the layout of the current interface . // Connection method
Button btnCall = (Button) findViewById(R.id.btn_call);

// Register to listen
} //
// If you use inner classes , Then the external class , There is no need to achieve OnClickListener Interface
// class MyOnClickListener implements OnClickListener {
// @Override
// public void onClick(View v) {
// System.out.println("MainUI3 Dial the number ..");
// call();
// }
// } /**
* How to make a call
private void call() { // 1. Take out the number in the input box
EditText etNumber = (EditText) findViewById(R.id.number); // Input box object
String number = etNumber.getText().toString(); // The number to be dialed // 2. Make a call according to the number
Intent intent = new Intent(); // Create an intention
intent.setAction(Intent.ACTION_CALL); // Specify its action as a call
intent.setData(Uri.parse("tel:" + number)); // Specify the number to be dialed
startActivity(intent); // Perform this action
} @Override
public void onClick(View v) {
System.out.println("MainUI4 Dial the number ..");

2 .  xml The way

In layout file xml, add to onClick event , then , From code ,

android:id ="@+id/button1"
android:text=" single click "
android:onClick="clicktest" // Add event

From code , add to clicktest Method , Prototype , Want to be with View.OnClickListener Like a prototype not implemented in , Add (View v) Method parameters of

// Prototypes of methods , And OnClickListener Method prototypes defined in , Agreement .
public void clicktest(View view){ Log.i("Test"," With xml The way , Set up events ")

3.   Inner class

This way, , Easy to understand .

// 1. Declare control 
private Button btn; @override
protected void onCreate(Bundle savedInstanceState) {
supper.onCreate(savedInstanceState); setContentView(R.layou.activity_main); // 2. Control Association
btn =(Button)findViewById(R.id.button1); Listener list = new Listener();
// 3. Register to listen
// Class objects that implement interfaces
} // Inner class
class Listener implements View.OnClickListener { @override
public void onClick(View v){
// The callback method
Log.i("TEST"," It's been clicked ");
} }

Android Click event ,4 Callbacks . More articles about

  1. android Four ways to click on an event

    android Four ways to click on an event The first way : Create an inner class to implement click events The code is as follows : package com.example.dail; import android.text.TextUtils; im ...

  2. Android Four ways to write click events in

    Android Four ways to write click events in Use inner classes to implement click events Use anonymous inner classes to implement click events Give Way MainActivity Realization View.OnClickListener Interface Through the properties of the control in the layout file The first method ...

  3. Android Click the event through kotlin Summary of several implementation methods

    Generally speaking ,Android Click the event through kotlin There are several ways to achieve this : 1. Through the global interface View.OnClickListener Realization , The code is as follows //class MainActivity : AppCom ...

  4. Android Click event

    Android Click event remarks Global implementation View.OnClickListener Maybe we need to put MainActivity Set to public Registration events btn_login.setOnClickListener ...

  5. Android note ——Button There are several ways to write click events

    Button Click event : It can be divided into the following categories : Anonymous inner class Define inner classes , Realization OnClickListener Interface The construction method of definition use Activity Realization OnClickListener Interface Appoint Button ...

  6. Android Study -----Button There are several ways to write click events

    Button Click event : It can be divided into the following categories : Anonymous inner class Define inner classes , Realization OnClickListener Interface The construction method of definition use Activity Realization OnClickListener Interface Appoint Button ...

  7. [Android] Four ways to write click events

    Prerequisites for click events : Realization OnClickListener Interface , rewrite onclick(View v) Method Take the simple case of dialing , As shown in the figure below : Logical process : Get click object , get data Set the listening class for the object Realization OnCl ...

  8. Android studio Code implementation calls + There are four ways to click events

    Android System architecture ( a key ) first floor : application layer Application The second floor : Application framework layer Application Framework The third level :Android The underlying class library layer Libraries.Dalvik virtual ...

  9. Android Click event (click button) Four ways of writing

    I'm learning android When developing and testing, we find that different people are interested in click The way events are written is different , I checked it on the Internet , There are four ways to write it , So I want to compare the four methods The first method : Anonymous inner class Code : package com. ...

  10. android Click in the event of 4 Species writing

    android Get some controls in ( Like buttons ) when , Click events are usually added to it ,android There are a total of 4 Chinese writing . Suppose you declare the following in the layout file ....... <Button android:la ...

Random recommendation

  1. Oracle in wm_concat() Use of functions

    Oracle in wm_concat() Use of functions wm_concat() The function is oracle Row column conversion function , This function can change the value of a column to ‘,’ To separate , And show it in a line . 1. The original data : 2. Group the results into ‘|’ Separate , In one line ...

  2. There are many functions for IIS Just realize it , Why did you write it yourself before ?

    For example, setting the default home page , Error page pseudo static and so on

  3. mac qq When the screenshot function fails , How to restart the screenshot function ?

    stay finder Open the application directory in , find QQ, Right click QQ, Select Show package content , A folder opens . Go to the following path Library/LoginItems Then double click. ScreenCapture This process , The screenshot function is ...

  4. Call between classes inline Efficiency of function

    Problem description : class A { public: int x, y, k, NY; inline int f(int i, int j, int k)  {return ((i)*(NY + 1) * ...

  5. Celery Source code analysis 4 : The realization of timed tasks

    We've seen it in the second part of the series Celery How the execution engine in performs tasks , And in the third part, we also introduce the object of the task , however , At present, what we see is passive task execution , In other words, the current tasks are sent by the third party . can ...

  6. Unity VS Create scripts to automatically add header comments - Time - describe - Author, etc

    Unity Generating scripts automatically add header comments This article provides the whole process , Chinese translation . Chinar Stick to a simple lifestyle , To the world !( Have a better reading experience -- For high resolution users, please adjust the scale of the web page according to your needs ) Chinar -- heart ...

  7. Mysql Group query group by Statement explanation

    (1) group by The meaning of : Query results according to 1 One or more fields to group , The fields with the same value are a set of (2) group by Can be used to group individual fields , It can also be used to group multiple fields select * from employee; ...

  8. SqlParameter In two ways 【 Two 】

    private void Loadprovince() { string sql = "select * from Tables where ArealdPid=@pid"; / The first ...

  9. HTML5 Local storage usage

    HTML5  Local storage of API Medium localStorage And sessionStorage It's the same way to use it , The difference lies in sessionStorage It is cleared after closing the page , and localSt ...

  10. Android studio gradle Configure the full version ( turn )

    Android studio gradle Configure the full version https://my.oschina.net/u/1471093/blog/539075 Android studio Custom packaging apk name - pe ...