Android中Button的使用

Android常用Button属性以及监听方法:

一、button在xml中常用属性:

        android:layout_width=""     //宽
        android:layout_height=""   //高

        android:text=""                   //按钮上显示的文本

       android:background=""       //背景颜色

       android:drawableTop=""     //在button的文字上方显示图片

       android:drawableLeft=""     //在button的文字左显示图片

      android:drawableBottom=""  //在button的文字下方显示图片

      android:drawableRight=""     //在button的文字右显示图片

      android:textColor=""              //文字的颜色

      android:textSize=""                //文字的大小



二、button的各种点击监听:

1、使用id进行点击:

(1).在布局中为Button控件设置id

    <Button
        android:id="@+id/bt"  
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击" />


(2.)找到控件的id设置点击监听

@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//找到button控件的id
		Button btn=(Button) findViewById(R.id.bt);
		//进行点击事件
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				//点击按钮弹出提示
				Toast.makeText(MainActivity.this,"点击了butotn按钮", Toast.LENGTH_SHORT).show();
			}
		});
	}



2、使用onClick进行点击:

(1).在button中加入onclick属性:

    <Button
        android:onClick="click"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击" />


(2).在activity中使用onclick
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
 
	}
	//注:该方法必须是public,方法名click要和button控件中的android:onClick相同
	public void click(View view){
		Toast.makeText(MainActivity.this, "点击了click", Toast.LENGTH_SHORT).show();
	}
注意:如果多个button都设置了onclick,那么都会走onclick方法。



3、多个button控件使用id分别点击:

(1).这里我们设置多个button,都加入id(每个id名要不同)

  <Button
        android:id="@+id/btn_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击1" />

    <Button
        android:id="@+id/btn_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="点击2" />

    <Button
        android:id="@+id/btn_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="点击3" />


(2).在Activity使用

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//获取不同button的id
		Button btn1=(Button) findViewById(R.id.btn_1);
		Button btn2=(Button) findViewById(R.id.btn_2);
		Button btn3=(Button) findViewById(R.id.btn_3);
		//调用button的点击监听
		btn1.setOnClickListener(lin);
		btn2.setOnClickListener(lin);
		btn3.setOnClickListener(lin);

	}
	//button的监听事件
	private OnClickListener lin=new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			//找到第一个button的id
			case R.id.btn_1:
				//点击按钮1弹出
				Toast.makeText(MainActivity.this, "点击了按钮1", Toast.LENGTH_SHORT).show();
				break;
				//找到第二个button的id
			case R.id.btn_2:
				//点击按钮2弹出
				Toast.makeText(MainActivity.this, "点击了按钮2", Toast.LENGTH_SHORT).show();

				break;
				//找到第三个button的id
			case R.id.btn_3:
				//点击按钮2弹出
				Toast.makeText(MainActivity.this, "点击了按钮3", Toast.LENGTH_SHORT).show();

				break;

			}

		}
	};



4、使用onclick进行多个button点击:

(1).button控件中我们加入属性onclick(id不要删,onclick设置的名称要相同)

    <Button
        android:id="@+id/btn_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="点击1" />

    <Button
        android:id="@+id/btn_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="点击2" />

    <Button
        android:id="@+id/btn_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="点击3" />

(2).在Activity中使用:

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	//注:该方法必须是public,方法名click要和button控件中的android:onClick相同
	public void click(View view){
		switch (view.getId()) {
		//找到第一个button的id
		case R.id.btn_1:
			//点击按钮1弹出
			Toast.makeText(MainActivity.this, "点击了按钮1", Toast.LENGTH_SHORT).show();
			break;
			//找到第二个button的id
		case R.id.btn_2:
			//点击按钮2弹出
			Toast.makeText(MainActivity.this, "点击了按钮2", Toast.LENGTH_SHORT).show();

			break;
			//找到第三个button的id
		case R.id.btn_3:
			//点击按钮2弹出
			Toast.makeText(MainActivity.this, "点击了按钮3", Toast.LENGTH_SHORT).show();
			break;

		}

	}




5、使用点击事件的实现接口进行点击:

(1).设置布局中的button

<Button
        android:id="@+id/btn_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击1" />

    <Button
        android:id="@+id/btn_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击2" />

    <Button
        android:id="@+id/btn_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击3" />


(2).使用点击事件接口

//实现点击事件的接口重写onclick方法
public class MainActivity extends Activity  implements OnClickListener{

	private Button bt1,bt2,bt3;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		bt1=(Button) findViewById(R.id.btn_1);
		bt2=(Button) findViewById(R.id.btn_2);
		bt3=(Button) findViewById(R.id.btn_3);
		bt1.setOnClickListener(this);
		bt2.setOnClickListener(this);
		bt3.setOnClickListener(this);
	}
	//接口重写的方法
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_1:
			Toast.makeText(MainActivity.this, "点击了1", Toast.LENGTH_LONG).show();
			break;
		case R.id.btn_2:
			Toast.makeText(MainActivity.this, "点击了2", Toast.LENGTH_LONG).show();
			break;
		case R.id.btn_3:
			Toast.makeText(MainActivity.this, "点击了3", Toast.LENGTH_LONG).show();
			break;


		}
	}
}


注:转载请注明出处http://blog.csdn.net/qq_26650589/article/details/53739729








     

 



  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值