我从android的Dialog派生了一个对话框。并使用 setContentView()
为对话框设置布局。我的布局中有按钮,我需要关闭该按钮单击的对话框。现在我点击按钮点击Toast并正常工作。这该怎么做?以下是我到目前为止编写的代码。
public class ErrorAlertDialog extends Dialog {
private TextView _errormessagetitle, _errormessagedescription;
private String _title, _description;
public BSFErrorAlertDialog(Context context, String title, String description) {
super(context);
_title = title;
_description = description;
}
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.bsfalerterror);
final Button button1 = (Button) findViewById(R.id.btnerrordialogok);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "button1 click",Toast.LENGTH_LONG).show();
}
});
_errormessagetitle = (TextView) findViewById(R.id.bsftxterrorheading);
_errormessagetitle.setText(_title);
_errormessagedescription = (TextView)findViewById(R.id.bsftxterrormessage);
_errormessagedescription.setText(_description);
}
}
答案 0 :(得分:0)
getDialog().dismiss(); //or getDialog().cancel();
用它代替吐司
答案 1 :(得分:0)
这样写:
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Toast.makeText(getContext(), "button1 click",Toast.LENGTH_LONG).show();
ErrorAlertDialog.this.dismiss();
}
});