好,那么现在我来讲讲关于Button的几种基本触发方式,这篇文章看懂了,或许会让你对编程有一定认识,以及可能会加强你对学习编程的信心。所以萌新请务必看完。
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" />
< Button
android:id = "@+id/button3"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "Button3" />
//先关联button
Button bt1 = (Button) findViewById(R.id.button1);
Button bt2 = (Button) findViewById(R.id.button2);
Button bt3 = (Button) findViewById(R.id.button3);
(1)匿名内部类:
//事件监听
bt1.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
// 事件
Toast.makeText(MainActivity.this,"点击了Button1",Toast.LENGTH_LONG).show();
}
});
(2)外部类实现 :
bt2.setOnClickListener(new MyOnClickListener(){
@Override
public void onClick(View v){
super.onClick(v);
v.setAlpha(0.5f);
}
});
class MyOnClickListener implements OnClickListener{
@SuppressLint("ShowToast")
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"点击了Button2",Toast.LENGTH_LONG).show();
}
}
(3)接口实现
public class MainActivity extends ActionBarActivity implements OnClickListener
bt3.setOnClickListener(this);
(4)自定义单击事件监听类
public class MainActivity extends Activity {
Button bt1, bt2;
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 , "点击了Button1" , Toast.LENGTH_SHORT);
tst.show();
break ;
case R.id.button2:
tst = Toast.makeText(TestButtonActivity. this , "点击了Button2" , 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());
}
}
(5)在xml布局文件中设置onClick回调方法名
xml如下:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="事件"/>
java代码如下:
public void 事件(View view){
Toast.makeText(MainActivity.this,"点击了Button",Toast.LENGTH_LONG).show();
}
(6)在XML文件中显示指定按钮的onClick属性,这样点击按钮时会利用反射的方式调用对应Activity中的click()方法
xml如下:
< 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" />
java代码如下:
//先注册
Button btn1, btn2;
Toast tst;
// 注意 这里没有 @Override 标签
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
tst = Toast.makeText(MainActivity.this , "点击了Button1" , Toast.LENGTH_SHORT);
tst.show();
break ;
case R.id.button2:
tst = Toast.makeText(MainActivity.this , "点击了Button2" , Toast.LENGTH_SHORT);
tst.show();
break ;
default :
break ;
}
}
现在来简单总结一下
第一种和第五种比方法较方便简单,适合Button事件比较少的场景,第五种方法在这个场景最方便,不过android4.4及以下的设备不支持。
第五种和第六种方法,适合Button事件比较多的场景,当然第6种在这个场景最方便快捷。
Comments NOTHING