要处理事件,有两种方法:
实现回调接口,例如
public class A implements View.OnClickListener { public void onClick(View v) { .... } @Override protected void onCreate(Bundle savedInstanceState) { ... aboutLayout = (LinearLayout) findViewById(R.id.aboutLayout); aboutLayout.setOnClickListener(this); } }
创建实现回调接口的内部类
public class ActivityAbout { private class ViewClickListener implements View.OnClickListener { public void onClick(View v) { .. } } @Override protected void onCreate(Bundle savedInstanceState) { ... ViewClickListener listener = new ViewClickListener(); aboutLayout = (LinearLayout) findViewById(R.id.aboutLayout); aboutLayout.setOnClickListener(listener); } }
哪一个更好?