标签:
很多学习Android程序设计的人都会发现每个人对代码的写法都有不同的偏好,比较明显的就是对控件响应事件的写法的不同。因此本文就把这些写法总结一下,比较下各种写法的优劣,希望对大家灵活地选择编码方式可以有一定的参考借鉴价值。
xml文件代码如下:
<
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"
/>
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() {
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() {
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();
}
});
}
}
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());
}
}
//
@Override
//
public
void
onClick(View v) {
<
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"
/>
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
;
}
}
}
一般监听OnClickListener事件,我们都是通过Button button = (Button)findViewById(....);
button.setOClickLisener....这样的方式来实现。
这段时间看各大开放平台的demo,发现其实可以在xml中定义好方法名称,在Activity中实现该方法就可以了。
<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="返回"
android:textColor="#fff"
android:textSize="14sp" />
public void aa(View v) {
/*在这里编写按钮被点击后执行的代码*/
}
在Activity中直接实现该方法即可,也无需定义button,实例化它,跟原先一比方便了很多。
标签:
原文地址:http://www.cnblogs.com/wangxiuheng/p/4484956.html