Four ways in the development of Android click event

Label AndroidcontroloperationAndroid DevelopmentXML
1174 people read comment(1) Collection report
Classification:

In the development of Android, click on the event is very basic, a lot of control can be clicked, the most common is the Button. Today, we'll look at how to write click events.

First create a new Android project, add a button in the MainActivity, in the XML to find the control.

One method

First step:
Bt.setOnClickListener (NewMyOnClick ());
Second step:
    Private Class MyOnClick Implements OnClickListener{

        @Override
        Public Void OnClick(arg0 View) {
            Auto-generated method stub / / TODO
System.out.println ("1");
}
}
Complete these two steps, click the button to print out the console "One"

Method two

Anonymous inner class implementation

Bt.setOnClickListener (NewOnClickListener () {

@Override
            Public Void OnClick(arg0 View) {
System.Out.println ("2");

}
});
Click on the button to print out the console"Two"

Method three

Let MainActivity to implement the onclick method

Bt.setOnClickListener (This);
Public Class MainActivity Extends Activity Implements OnClickListener{

    PrivateBT Button;
    @Override
    Protected Void OnCreate(savedInstanceState Bundle) {
        Super.onCreate (savedInstanceState);
SetContentView (R.layout.activity_main);

BT = (Button) findViewById (R.id.bt);

}
    @Override
    Public Void OnClick(arg0 View) {
        Auto-generated method stub / / TODO
System.out.println ("3");
}
}
Click on the button to print out the console"Three"

Method four

In Button android:onClick= XML "click" attribute
Add in MainActivity:

Public Void Click(V View) {
System.Out.println ("4");
}
Click on the button to print out the console"Four"

summary

The first three methods are basically the same, the difference is that the implementation of the OnClickListener interface is not the same, the fourth methods are the most simple.
But in the real project development, using the framework, looking for controls and other operations are completed by the framework, you can speed up the development of speed. (we first learn the basis of knowledge, the use of the framework of the latter to introduce to you)
@OnClick (ID= R.id.bt)
VoidClick (){
......
}
top
Zero
step on
Zero
Guess you're looking for
View comments
* the above user comments only represent their personal views, does not represent the views or position of the CSDN website
    personal data
    • Visit1327 times
    • Integral:Forty-three
    • Grade
    • Rank:Thousands of miles away
    • Original3
    • Reproduced:0
    • Translation:0
    • Comments:2
    Classification of articles
    Article Archive