安卓学习——采用onClickListener监听器,界面按钮响应
(一)定义监听,绑定组件
自定义一个类,用来筛选监听的指令
class MyClickedHandle implements View.OnClickListener
{
public void onClick(View v)
{
switch(v.getId())
{
case R.id.button1:
System.out.println("按键一被按下");
// Toast.makeText(this,"按键一启动", 0).show();
break;
case R.id.button2:
System.out.println("按键二被按下");
// Toast.makeText(this,"按键二启动", 0).show();
break;
}
}
}
public class MainActivity extends Activity {
Button btn1;
Button btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.button1); // 通过 R 文件里头 组件的ID寻找组件
btn2 = (Button) findViewById(R.id.button2