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();
@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();
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
}
});
}
});
正のボタンのハンドラをオーバーライドする必要があると思います。特定の条件が満たされたときにダイアログを閉じるロジックを追加します。
さらに簡単:
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
}
});
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
}
}
私はこのようにします:
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();
}
});