«

Android 四种点击事件

时间:2024-3-2 19:08     作者:韩俊     分类: Android


Android 四种点击事件

xml文件代码如下:(1--3 均可用)

<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" />
  1. 匿名内部类:
    public class ButtonActivity extends Activity {   Button btn1, btn2;  Toast tst;  @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 OnClickListener() {       @Override      public void onClick(View v) {        // TODO Auto-generated method stub        Toast tst = Toast.makeText(ButtonActivity.this, "匿名内部类1", Toast.LENGTH_SHORT);        tst.show();       }    });     btn2.setOnClickListener(new OnClickListener() {       @Override      public void onClick(View v) {        // TODO Auto-generated method stub        Toast tst = Toast.makeText(ButtonActivity.this, "匿名内部类2", Toast.LENGTH_SHORT);        tst.show();      }    });  }}
  2. 自定义单击事件监听类:
    public class ButtonActivity extends Activity {   Button btn1, btn2;  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(ButtonActivity.this, "自定义单击1", Toast.LENGTH_SHORT);        tst.show();        break;      case R.id.button2:        tst = Toast.makeText(ButtonActivity.this, "自定义单击2", 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());  }}
  3. Activity继承View.OnClickListener

    由Activity实现OnClick(View view)方法,在OnClick(View view)方法中用switch-case对不同id代表的button进行相应的处理

    public class ButtonActivity extends Activity implements OnClickListener {   Button btn1, btn2;  Toast tst;   @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(this);    btn2.setOnClickListener(this);  }   @Override  public void onClick(View v) {    // TODO Auto-generated method stub    switch (v.getId()) {    case R.id.button1:      tst = Toast.makeText(this, "继承View.OnClickListener1", Toast.LENGTH_SHORT);      tst.show();      break;    case R.id.button2:      tst = Toast.makeText(this, "继承View.OnClickListener2", Toast.LENGTH_SHORT);      tst.show();      break;    default:      break;    }  }}
  4. 在布局文件Button属性中添加 android:onClick=" 方法名"

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" />

程序代码

public class ButtonActivity extends Activity { Button btn1, btn2;Toast tst; @Overrideprotected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_test_button);} // 注意 这里没有 @Override 标签public void onClick(View v) {  // TODO Auto-generated method stub  switch (v.getId()) {  case R.id.button1:    tst = Toast.makeText(this, "布局文件1", Toast.LENGTH_SHORT);    tst.show();    break;  case R.id.button2:    tst = Toast.makeText(this, "布局文件2", Toast.LENGTH_SHORT);    tst.show();    break;  default:    break;  }}}

标签: android

热门推荐