- 在每次使用时新实现一个OnClickListener接口
public class MainActivity extends Activity {
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btnDoSomething);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doSomething(v);
}
});
}
public void doSomething(View view) {
/*do something here*/
}
}
- 让这个页面类实现OnClickListener接口
public class MainActivity extends Activity implements View.OnClickListener{
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btnDoSomething);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnDoSomething:
doSomething(v);
break;
default:
break;
}
}
public void doSomething(View view) {
/*do something here*/
}
}
- 多组合少继承的原则,把OnClickListener接口作为一个属性放到页面类中
public class MainActivity extends Activity{
private Button btn;
private View.OnClickListener clickListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
clickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
doSomething(v);
}
};
btn = (Button) findViewById(R.id.btnDoSomething);
btn.setOnClickListener(clickListener);
}
public void doSomething(View view) {
/*do something here*/
}
}
- 把类中的点击事件看作是一个对象,新建一个类来实现这些事件
public class ClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
doSomething(v);
}
private void doSomething(View view) {
/*do something here*/
}
}
在使用的时候,新建或者组合使用
public class MainActivity extends Activity{
private Button btn;
private ClickListener clickListener = new ClickListener();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btnDoSomething);
btn.setOnClickListener(clickListener);
}
}
- 在java类中定义好点击事件要执行的方法,在布局文件xml中调用
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.vip.veblonwang.myapplication.MainActivity">
<Button
android:id="@+id/btnDoSomething"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="share"
android:onClick="doSomething"/><!-- 这句是关键,点击时执行MainActivity中的doSomething方法 -->
</LinearLayout>
MainActivity .java
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void doSomething(View view) { //方法必须定义为public
/*do something here*/
}
}
个人理解就上面几种实现方式了~
推荐使用第三、四种方式,其他方式不推荐