我创建AlertDialog包含4个按钮
OptionDialog = new AlertDialog.Builder(this);
OptionDialog.setTitle("Options");
LayoutInflater li = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.options, null, false);
background = (Button) v.findViewById(R.id.bkgSpinnerLabel);
SoundVib = (Button) v.findViewById(R.id.SoundVibSpinnerLabel);
OptionDialog.setView(v);
OptionDialog.setCancelable(true);
OptionDialog.setNeutralButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
}
});
background.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SetBackground();
// here I want to dismiss it after SetBackground() method
}
});
SoundVib.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent soundVibIntent = new Intent(SebhaActivity.this, EditPreferences.class);
startActivity(soundVibIntent);
}
});
OptionDialog.show();
我想以后的setBackground()方法来关闭它,我该怎么办呢? 提前致谢。
实际上没有任何cancel()
或dismiss()
从AlertDialog.Builder类方法。
因此,而不是AlertDialog.Builder optionDialog
使用AlertDialog
实例。
喜欢,
AlertDialog optionDialog = new AlertDialog.Builder(this).create();
现在,只需拨打optionDialog.dismiss();
background.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SetBackground();
// here I want to dismiss it after SetBackground() method
optionDialog.dismiss();
}
});
我觉得有一个简单的解决方案:只需使用DialogInterface
传递给参数onClick
方法。
AlertDialog.Builder db = new AlertDialog.Builder(context);
db.setNegativeButton("cancel", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface d, int arg1) {
d.cancel();
};
});
见,例如, http://www.mkyong.com/android/android-alert-dialog-example 。
试试这个:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
AlertDialog OptionDialog = builder.create();
background.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SetBackground();
OptionDialog .dismiss();
}
});
要关闭或取消AlertDialog.Builder
dialog.setNegativeButton("إلغاء", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
你打电话解雇()的对话框界面
有关闭一个警告对话框的方法有两种。
选项1:
AlertDialog#create().dismiss();
选项2:
The DialogInterface#dismiss();
开箱即用,框架调用DialogInterface#dismiss();
当你定义的事件侦听器的按钮:
-
AlertDialog#setNegativeButton();
-
AlertDialog#setPositiveButton();
-
AlertDialog#setNeutralButton();
为警告对话框。
这里是我结束我的alertDialog
lv_three.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
GetTalebeDataUser clickedObj = (GetTalebeDataUser) parent.getItemAtPosition(position);
alertDialog.setTitle(clickedObj.getAd());
alertDialog.setMessage("Öğrenci Bilgileri Güncelle?");
alertDialog.setIcon(R.drawable.ic_info);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User pressed YES button. Write Logic Here
}
});
alertDialog.setNegativeButton("İptal", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//alertDialog.
alertDialog.setCancelable(true); // HERE
}
});
alertDialog.show();
return true;
}
});
刚刚成立的观点为空,将关闭AlertDialog简单。