一、给Button设置监听
1、单个button
Button buttontest; buttontest = (Button) findViewById(R.id.button1); buttontest.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Log.i("TEST", "button onClick"); } });
2、为多个按钮设置监听
(1)调用接口
public class NewText extends Activity implements View.OnClickListener{ private Button btn1,btn2;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_new_text); btn1=(Button)findViewById(R.id.button1); btn1.setOnClickListener(this); btn2=(Button)findViewById(R.id.button2); btn2.setOnClickListener(this); } public void onClick(View v) { switch (v.getId()) { case R.id.button1: // break; case R.id.button2: // break; default: break; } }
(2)不用调用接口
{
/*.... */
bt1.setOnClickListener(l); bt2.setOnClickListener(l); } private OnClickListener l = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()) { case R.id.button1: //break; case R.id.button2: //break; } } };
二、给TextView设置监听
{...
Title_Name.setClickable(true); Title_Name.setFocusable(true); Title_Name.setOnClickListener(l);
}
private OnClickListener l = new OnClickListener() { @Override public void onClick(View v) { } };