1.匿名类实现
public class MainActivity extends Activity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button =(Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v){ //在此处添加逻辑 } }); ....... }
2.实现接口注册实现
public class MainActivity extends Activity implements OnClickListener{ private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button =(Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener()) } @Override public void onClick(View v){ switch (v.getId()){ case R.id.buttpn: //在此处添加逻辑 break; default: break; } } ....... }
没有评论:
发表评论