点击PositiveButton后,我可以不解雇我的AlertDialog吗?
我想继续在对话框中显示我的ArrayAdapter listWords上的更新。
这是我的代码。
AlertDialog.Builder sayWindows = new AlertDialog.Builder(MapActivity.this);
final EditText saySomething = new EditText(MapActivity.this);
sayWindows.setPositiveButton("ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
say = userName + " Says: "+saySomething.getText();
showPosition.setText(say);
}
});
sayWindows.setNegativeButton("cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
sayWindows.setAdapter(listWords, null);
sayWindows.setView(saySomething);
sayWindows.create().show();
答案 0 :(得分:62)
在看了@Little Child解决方案之后,我试着做到这一点。如果这对您有用,请告诉我们。
AlertDialog.Builder sayWindows = new AlertDialog.Builder(
MapActivity.this);
final EditText saySomething = new EditText(MapActivity.this);
sayWindows.setPositiveButton("ok", null);
sayWindows.setNegativeButton("cancel", null);
sayWindows.setAdapter(listWords, null);
sayWindows.setView(saySomething);
final AlertDialog mAlertDialog = sayWindows.create();
mAlertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button b = mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO Do something
say = userName + " Says: "+saySomething.getText();
showPosition.setText(say);
}
});
}
});
mAlertDialog.show();
答案 1 :(得分:13)
基于How to prevent a dialog from closing when a button is clicked
的最多投票答案final AlertDialog d = new AlertDialog.Builder(context)
.setView(v)
.setTitle(R.string.my_title)
.setPositiveButton(android.R.string.ok, null) //Set to null. We override the onclick
.setNegativeButton(android.R.string.cancel, null)
.create();
d.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button b = d.getButton(AlertDialog.BUTTON_POSITIVE);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO Do something
}
});
}
});
我相信你需要覆盖正面按钮的处理程序。添加逻辑以在满足特定条件时关闭对话框。
答案 2 :(得分:8)
更简单:
final AlertDialog alertDialog = new AlertDialog.Builder(context).setView(v)
.setPositiveButton(android.R.string.ok, null)
.setNegativeButton(android.R.string.cancel, null)
.show();
Button b = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Do Your thing
}
});
答案 3 :(得分:4)
Kotlin的答案:
val dialog = AlertDialog.Builder(context)
.setView(v)
.setTitle(R.string.my_title)
.setPositiveButton(android.R.string.ok, null)
.setNegativeButton(android.R.string.cancel, null)
.create()
dialog.setOnShowListener {
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
// Apply logic here
}
}
答案 4 :(得分:2)
我这样做:
final AlertDialog dialog = new AlertDialog.Builder(this)
.setCancelable(false)
.setPositiveButton("YES", null)
.setNegativeButton("NO", null)
.show();
Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
positiveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Toast.makeText(SysManagerActivity.this, "dialog is open", Toast.LENGTH_SHORT).show();
}
});