Many people who study Android programming find that each person has different preferences for how the code is written, and what is more obvious is the difference in how the control responds to events. Therefore this article summarizes these writing, compares each kind of writing the merits and demerits, hoped that everybody flexibly chooses the code way may have the reference value.
The XML file code is as follows:
<
Button
android:id
=
"@+id/button1"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"Button1"
/>
<
Button
android:id
=
"@+id/button2"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"Button2"
/>
The four methods are described as follows: 1. Anonymous inner class:
public
class
TestButtonActivity
extends
Activity {
Button btn1, btn2;
Toast tst;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_button);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn1.setOnClickListener(
new
OnClickListener() {
Or
btn1.setOnClickListener(
new
View.onclicklistener
() {
@Override
public
void
onClick(View v) {
// TODO Auto-generated method stub
Toast tst = Toast.makeText(TestButtonActivity.
this
,
"111111111"
, Toast.LENGTH_SHORT);
tst.show();
}
});
btn2.setOnClickListener(
new
OnClickListener() {
Or
btn2.setOnClickListener(
new
View.onclicklistener
() {
@Override
public
void
onClick(View v) {
// TODO Auto-generated method stub
Toast tst = Toast.makeText(TestButtonActivity.
this
,
"222222222"
, Toast.LENGTH_SHORT);
tst.show();
}
});
}
}
2. Custom Click event Listener class:
public
class
TestButtonActivity
extends
Activity {
Button btn1, btn2;
Toast tst;
class
MyClickListener
implements
OnClickListener {
@Override
public
void
onClick(View v) {
// TODO Auto-generated method stub
switch
(v.getId()) {
case
R.id.button1:
tst = Toast.makeText(TestButtonActivity.
this
,
"111111111"
, Toast.LENGTH_SHORT);
tst.show();
break
;
case
R.id.button2:
tst = Toast.makeText(TestButtonActivity.
this
,
"222222222"
, Toast.LENGTH_SHORT);
tst.show();
break
;
default
:
break
;
}
}
}
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_button);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn1.setOnClickListener(
new
MyClickListener());
btn2.setOnClickListener(
new
MyClickListener());
}
}
Or first define a Onclicklistener object, and implement its onclick (View) method ... Onclicklistener listener = new Onclicklistener () {
//
@Override
//
public
void
onClick(View v) {
/* Write a button here to execute the code *///};//bt.setonclicklistener (listener); 3. This is a notation that I see today, in the XML file, "displays the OnClick property of the specified button, This allows you to invoke the click () method in the corresponding activity using reflection when the button is clicked.
<
Button
android:id
=
"@+id/button1"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:onClick
=
"onClick"
android:text
=
"Button1"
/>
<
Button
android:id
=
"@+id/button2"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:onClick
=
"onClick"
android:text
=
"Button2"
/>
Here in the end of the android: Press alt+/will have the OnClick property prompt, but the input to android:onclick= "place pressed alt+/and did not prompt the OnClick option, let me suddenly feel there seems to be a problem.
public
class
TestButtonActivity
extends
Activity {
Button btn1, btn2;
Toast tst;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_button);
}
// 注意 这里没有 @Override 标签
public
void
onClick(View v) {
// TODO Auto-generated method stub
switch
(v.getId()) {
case
R.id.button1:
tst = Toast.makeText(
this
,
"111111111"
, Toast.LENGTH_SHORT);
tst.show();
break
;
case
R.id.button2:
tst = Toast.makeText(
this
,
"222222222"
, Toast.LENGTH_SHORT);
tst.show();
break
;
default
:
break
;
}
}
}
In this case, the button's Click event can be implemented without declaring the button in the entire code. 4. Similar to the previous approach, but a little more specific
General monitoring Onclicklistener Events, we are all through button button = (button) Findviewbyid (...);
Button.setoclicklisener .... Such a way to achieve.
This period of time to see the major open platform of the demo, found in the XML can actually define a good method name, in the activity to implement the method can be.
<button
Android:layout_width= "70DP"
android:layout_height= "Wrap_content"
Android:layout_centervertical= "true"
android:background= "@drawable/title_btn_back"
android:onclick= "AA"
Android:text= "Back"
Android:textcolor= "#fff"
Android:textsize= "14sp"/>
public void AA (View v) {
/* Write the code that executes when the button is clicked in here */
}
This method can be implemented directly in the activity, and there is no need to define a button to instantiate it, which is much more convenient than the original one.
Android button Common Click event Big Summary