Button点击事件:大概可以分为以下几种:
首先我们简单地定义一个带Button的xml布局文件
activity_main.xml:
<Button android:id="@+id/bt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击事件" />
然后再写Java代码
MainActivity.java:
1.匿名内部类
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt = (Button) findViewById(R.id.bt1); //1.匿名内部类 bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Log.i("匿名内部类", "点击事件"); } }); }
public class MainActivity extends Activity{ private Button bt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt = (Button) findViewById(R.id.bt); bt.setOnClickListener(new MyListener()); } //定义内部类,实现OnClickListene接口 class MyListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub Log.i("定义内部类,实现OnClickListene接口", "点击事件"); } } }
public class MainActivity extends Activity{ private Button bt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt = (Button) findViewById(R.id.bt); myListener(); } //定义构造方法 private void myListener() { // TODO Auto-generated method stub bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Log.i("定义构造方法", "点击事件"); } }); } }
public class MainActivity extends Activity implements OnClickListener { private Button bt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt = (Button) findViewById(R.id.bt); bt.setOnClickListener(this); } //用Activity实现OnClickListener接口 @Override public void onClick(View v) { // TODO Auto-generated method stub Log.i("用Activity实现OnClickListener接口", "点击事件"); } }
先在layout文件中指定onClick属性,然后到Activity中实现这个onButtonClick方法
布局文件:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click" android:text="点击事件" />
Java代码:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //5指定onClick属性方式 public void click(View v) { // TODO Auto-generated method stub Log.i("指定onClick属性方式","点击事件"); } }
布局文件:
<Button android:id="@+id/bt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click" android:text="点击事件1" /> <Button android:id="@+id/bt2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click" android:text="点击事件2" /> <Button android:id="@+id/bt3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click" android:text="点击事件3" />/> <Button android:id="@+id/bt4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click" android:text="点击事件4" />
Java代码:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //指定onClick属性方式 //传人的View对象,就是用户按下的那个按钮对象 public void click(View v) { // TODO Auto-generated method stub //获取组件的资源id int id = v.getId(); switch (id) { case R.id.bt1: Log.i("指定onClick属性方式","bt1点击事件"); break; case R.id.bt2: Log.i("指定onClick属性方式","bt2点击事件"); break; case R.id.bt3: Log.i("指定onClick属性方式","bt3点击事件"); break; case R.id.bt4: Log.i("指定onClick属性方式","bt4点击事件"); break; default: break; } } }
效果如图: