android--Button控件简介
Button事件监听器
private Button btnButton = null;
private Button btnButton2 = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnButton = (Button) findViewById(R.id.button1);
btnButton2 = (Button) findViewById(R.id.button2);
// 方法三:
btnButton.setOnClickListener(onListener);
btnButton2.setOnClickListener(onListener);
}
private OnClickListener onListener=new OnClickListener()
{
@Override
public void onClick(View v)
{
Button btnButton=(Button)v;
switch (btnButton.getId())
{
case R.id.button1:
Toast.makeText(MainActivity.this, "您点击了button1", Toast.LENGTH_LONG).show();
break;
case R.id.button2:
Toast.makeText(MainActivity.this, "您点击 了button2", Toast.LENGTH_LONG).show();
default:
break;
}
}
};