android 点击对话框按钮 不关闭按钮,Android在单击PositiveButton后不要关闭AlertDialog...
Android在單擊PositiveButton后不要關(guān)閉AlertDialog
我可以在單擊PositiveButton之后不關(guān)閉我的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();
5個解決方案
65 votes
看完@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();
Chitrang answered 2020-08-11T04:26:26Z
13 votes
基于有關(guān)如何防止單擊按鈕時關(guān)閉對話框的投票最多的答案
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
}
});
}
});
我相信您需要覆蓋肯定按鈕的處理程序。 添加您的邏輯以在滿足特定條件時關(guān)閉對話框。
Little Child answered 2020-08-11T04:26:50Z
10 votes
更簡單:
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
}
});
M. Usman Khan answered 2020-08-11T04:27:10Z
5 votes
在科特林回答:
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
}
}
Steve Lukis answered 2020-08-11T04:27:30Z
4 votes
我這樣做是這樣的:
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();
}
});
Yury Matatov answered 2020-08-11T04:27:50Z
總結(jié)
以上是生活随笔為你收集整理的android 点击对话框按钮 不关闭按钮,Android在单击PositiveButton后不要关闭AlertDialog...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第七章递归知识讲解。
- 下一篇: csv用excel打开后乱码?