リクエストごとにテキストが動的に変更される Android AlertDialog
-
11-09-2019 - |
質問
リクエストごとに変更されるオプションを 1 つ含む AlertDialog を表示したいと考えています。たとえば、ある時は「連絡先に追加」というオプションを表示したいのですが、別の時には「連絡先から削除」というオプションを表示する必要があります。
私のコードは初回は機能しますが、Android は AlertDialog をキャッシュしているようで、次回は onCreateDialog が実行されません。したがって、オプションはもう変更されません。このキャッシュを防ぐことはできますか? それともオプションを変更する別の方法はありますか?
SDK 1.5 で作業していますが、1.1 を使用しています。
@Override
protected Dialog onCreateDialog(final int id) {
...
String add_remove_contact = res.getString(R.string.profile_add_to_contacts);
if (user.getContacts().contains(profileID)) {
add_remove_contact = res.getString(R.string.profile_remove_from_contacts);
// TODO: this string is not changed when contact status changes
}
final CharSequence[] items = {res.getString(R.string.view_profile),
res.getString(R.string.profile_send_message),
add_remove_contact};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
...
return builder.create();
}
解決
また、活動のの removeDialog(int型)の機能を使用することができます。ダイアログが却下された場合、活動は基本的には、ダイアログ(パフォーマンス上の理由から、私は想像)の状態を保存します。呼び出しの removeDialog(int型)のダイアログでは、ダイアログのすべての参照をアンロードするための活動を強制し、それが表示されていた場合、画面からそれを否定しています。
他のヒント
を見てみましょうダイアログが表示される前に呼び出されますonPrepareDialog の方法。そこには、要求のタイプに基づいて必要な値を変更することができます。
の日付ピッカーは、実施例の
@Override
protected Dialog onCreateDialog(final int id) {
switch (id) {
case DIALOG_DATE_ID:
final Calendar c = Calendar.getInstance();
return new DatePickerDialog(this, this, c.get(Calendar.YEAR),
c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH));
default:
return super.onCreateDialog(id);
}
}
@Override
protected void onPrepareDialog(final int id, final Dialog dialog) {
switch (id) {
case DIALOG_DATE_ID:
//update to current time
final Calendar c = Calendar.getInstance();
((DatePickerDialog) dialog).updateDate(c.get(Calendar.YEAR),
c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH));
break;
}
}
これは、この質問のDUPは次のとおりです。 アンドロイド:テキストはAlertDialog のに表示されて変更できません
また、このようにそれを行うことができます。 http://andmobidev.blogspot.com/2010/ 03 /修正-アラートダイアログのリスト-items.htmlする
しかしlongpressメニューの表示を遅くするように見えるん...
私は、私は上記の一貫性のない動作のための修正があると思います。最初にダイアログ(それはまだAlertDialog.Builderだとき)、あなたは初期状態(NOT NULL)にメッセージを設定する必要がありますかonPrepareDialogを作成するときに意図した値でそれを上書きしません。ダイアログを作成しているときに、常にメッセージ内の非NULL値を持つように、このような何かを行います。私は、この日のために苦労し、事故によって、この解決策を見つけます:
AlertDialog.Builder resultAlert = new AlertDialog.Builder(context);
if ( message == null ) {
resultAlert.setMessage("");
} else {
resultAlert.setMessage(message);
}
アクティビティ管理ダイアログを使用するパフォーマンス上の理由は理解していますが、単純な場合を除いては使用しないことをお勧めします。その理由は次のとおりです。
Bundle 引数は API レベル 8 でのみ追加されたため、下位互換性のために採用することはできません。これは事実上、「onPrepareDialog」が状態の違いについて非ローカル変数に依存する必要があることを意味します。
実践では、「onPrepareDialog」の本体で行われたダイアログの変更に応じた動作が不適切で一貫性がないことが示されています。
ダイアログをサブクラス化し、必要に応じて作成すれば、これらの問題は発生しません。必要に応じて「setOwnerActivity」を呼び出すことができます。
カスタムダイアログを持っている場合は、あなたがdialog.getWindowを(使用してカスタム項目を変更することができます)。findViewById(...)
この例を示し、最後のテキストを保存して、再度ダイアログを表示し、次の時間を表示します。
// custom dialog
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.customized);
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
EditText dialogText = (EditText)dialog.getWindow().findViewById(R.id.customText);
savedText = dialogText.getText();
}
});
dialog.show();
EditText dialogText = (EditText)dialog.getWindow().findViewById(R.id.customText);
dialogText.setText(savedText);
カスタマイズされたダイアログのXMLます:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/buttonOK"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="OK"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="16dp" />
<EditText
android:id="@+id/customText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="19dp"
android:hint="Message"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
そして、私はとても良いアイデアを得たではなく、ユーザーがかなり頻繁にダイアログを使用していないとき* のこれが使用されているの*ソリューション:。!まず、あなたは(変数を宣言する必要がありますint型)とprivate int i=0;
など0.suchとしてデフォルト値を作ります
あなたが活動のShowDialogのメソッドを使用する前にして、int型変数iを増やし、ShowDialogメソッドとして、パラメータとして値iを投稿してください。
コードはこれを気に入るかもしれ
private int i=0;
//before you show the dialog
this.i++;
this.showDialog(this.i);
正確に。 Builder.create()
/ wが作成されたAlertDialog、のために、onPrepareDialog()
は無用です。ダイアログが作成されると、Builderはその中で一方通行である、あなたは更新できません。私は意味緩く、私はあなたがビューへのハンドルを取得し、手動でそれをすべて行うことができます確信しているが、それは最初の場所でビルダーを使用してのポイントを破ることはできません。
私が見つけた唯一の解決策は、手動/ショーを作成するために/代わりなどonCreateDialog()
、showDialog()
を、使用してのダイアログを閉じた私はremoveDialog()
を呼び出してみましたが、それは動作していないようでした。