我的xml布局文件中有以下按钮...
<Button
android:layout_width="150dip"
android:id="@+id/button1"
android:layout_height="50dip"
android:text="@string/login"
android:layout_marginRight="10dip">
</Button>
Run Code Online (Sandbox Code Playgroud)
我想以编程方式onclick()在其Java文件中添加一个监听器.我该怎么做?
Ros*_*ick 79
你只需要这样的东西:
Button buttonOne = (Button) findViewById(R.id.button1);
buttonOne.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//Do stuff here
}
});
Run Code Online (Sandbox Code Playgroud)
Sur*_*gch 20
这个答案来自五种方式来连接一个事件监听器.请阅读该博文,以获得作者更全面的解释.请参阅我的其他答案,了解为添加多个onClick侦听器而重做的这五种方法.
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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
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());
}
};
}
Run Code Online (Sandbox Code Playgroud)
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());
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
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());
}
}
Run Code Online (Sandbox Code Playgroud)
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());
}
}
Run Code Online (Sandbox Code Playgroud)
XML:
<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
android:onClick="HandleClick"/>
Run Code Online (Sandbox Code Playgroud)
你可以试试这个.
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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
76691 次 |
| 最近记录: |