Bri*_*ide 4 android dialog click listener
这是我的代码:
myButton.setOnClickListener( new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
});
Run Code Online (Sandbox Code Playgroud)
这给了我这个错误:类型为View的setOnClickListener(View.OnClickListener)方法不适用于参数(new DialogInterface.OnClickListener(){})
我导入了DialogInterface和DialogInterface.OnClickListener。“ myButton”在对话框中。
这是我当前的设置。button1启动对话框,而button2包含在对话框中。
private void setOnClickListeners() {
button1.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View arg0) {
//set up dialog
Dialog dialog = new Dialog(WorkScreen.this);
dialog.setContentView(R.layout.dialog_layout);
dialog.setTitle("My Dialog");
dialog.setCancelable(true);
dialog.show();
}
});
button2.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
}
Run Code Online (Sandbox Code Playgroud)
活动加载时崩溃。我还尝试将Dialog声明移到类的顶部,并在onCreate上对其进行初始化,但它没有做任何更改。有想法吗?
当您只需要处理Dialogs中的按钮,而View.onclickListener可以处理Activiy中的视图时,请使用DialogInterface.OnClickListener。
class MyDialog extends Dialog {
Context c ;
public MyDialog(Context context) {
super(context);
// TODO Auto-generated constructor stub
c = context ;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_layout) ;
Button b3 =(Button) findViewById(R.id.button3);
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(c,"Button 3 has been clicked ",Toast.LENGTH_LONG).show();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
我创建我的自定义对话框并处理此类中对话框中的按钮
| 归档时间: |
|
| 查看次数: |
4528 次 |
| 最近记录: |