添加onclick监听器到预定义的button?

我在我的XML布局文件中有以下button…

<Button android:layout_width="150dip" android:id="@+id/button1" android:layout_height="50dip" android:text="@string/login" android:layout_marginRight="10dip"> </Button> 

我想以编程方式在其Java文件中添加一个onclick()监听器。 我将如何做到这一点?

Solutions Collecting From Web of "添加onclick监听器到预定义的button?"

你只需要这样的东西:

 Button buttonOne = (Button) findViewById(R.id.button1); buttonOne.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { //Do stuff here } }); 

这个答案来自五种方法来连接事件监听器 。 请阅读该博客文章作者的更全面的解释。 看到我的另一个回答这五种方法重新join多个onClick监听器 。

1.会员类别

 public class main extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //attach an instance of HandleClick to the Button findViewById(R.id.button1).setOnClickListener(new HandleClick()); } private class HandleClick implements OnClickListener{ public void onClick(View arg0) { Button btn = (Button)arg0; //cast view to a button // get a reference to the TextView TextView tv = (TextView) findViewById(R.id.textview1); // update the TextView text tv.setText("You pressed " + btn.getText()); } } } 

2.接口types

 public class main extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //use the handleClick variable to attach the event listener findViewById(R.id.button1).setOnClickListener(handleClick); } private OnClickListener handleClick = new OnClickListener(){ public void onClick(View arg0) { Button btn = (Button)arg0; TextView tv = (TextView) findViewById(R.id.textview1); tv.setText("You pressed " + btn.getText()); } }; } 

3.匿名内部类

 public class main extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViewById(R.id.button1).setOnClickListener(new OnClickListener(){ public void onClick(View arg0) { Button btn = (Button)arg0; TextView tv = (TextView) findViewById(R.id.textview1); tv.setText("You pressed " + btn.getText()); } }); } } 

4.活动中的实施

 public class main extends Activity implements OnClickListener{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViewById(R.id.button1).setOnClickListener(this); } public void onClick(View arg0) { Button btn = (Button)arg0; TextView tv = (TextView) findViewById(R.id.textview1); tv.setText("You pressed " + btn.getText()); } } 

5. OnClick事件的视图布局中的属性

 public class main extends Activity{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void HandleClick(View arg0) { Button btn = (Button)arg0; TextView tv = (TextView) findViewById(R.id.textview1); tv.setText("You pressed " + btn.getText()); } } 

XML:

 <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" android:onClick="HandleClick"/> 

你可以试试这个

 public class myNewClass extends Activity implements OnClickListener { ................... ................... Button button = (Button)findViewById(R.id.button1); button.setOnClickListener(this); public void onClick(View v) { Intent i = new Intent(); Bundle extras = new Bundle(); // This will catch the button click // Now do what you wanted to do as a // result of the onClick } } 

1.使用findViewById(R.id.button1).setOnClickListener(this);

2.和活动实现OnClickListener