首页 > 代码库 > android_社交软件4_按钮事件

android_社交软件4_按钮事件

1

package com.lzy.ButtonTestR;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity {    public Button btn1=null;    public Button btn2=null;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        btn2=(Button)findViewById(R.id.button2);        btn2.setOnClickListener(listener);        btn1=(Button)findViewById(R.id.button1);        btn1.setOnClickListener(listener);                   }    private OnClickListener listener =new OnClickListener() {    @Override  public void onClick(View v)  {   // TODO Auto-generated method stub   Button btn=(Button)v;   switch (btn.getId())   {    case R.id.button1:     Toast.makeText(MainActivity.this, "按钮1被单击", Toast.LENGTH_LONG).show();     break;    case R.id.button2:     Toast.makeText(MainActivity.this, "按钮2被单击", Toast.LENGTH_LONG).show();     break;    default:     break;   }     } };}

2

 

android_社交软件4_按钮事件