Androidでアラートダイアログを表示するにはどうすればいいですか? 質問する

Androidでアラートダイアログを表示するにはどうすればいいですか? 質問する

「このエントリを削除してもよろしいですか?」というメッセージをユーザーに表示するダイアログ/ポップアップ ウィンドウと、「削除」というボタンを 1 つ表示します。Deleteタッチするとそのエントリが削除され、それ以外の場合は何も行われません。

これらのボタンのクリック リスナーを作成しましたが、ダイアログやポップアップとその機能を呼び出すにはどうすればよいですか?

ベストアンサー1

これに を使用しAlertDialog、そのBuilderクラスを使用して を構築することができます。以下の例では、Contextダイアログが渡されたコンテキストから適切なテーマを継承するため、 のみを受け入れるデフォルトのコンストラクターを使用していますが、必要に応じて、特定のテーマ リソースを 2 番目のパラメーターとして指定できるコンストラクターもあります。

new AlertDialog.Builder(context)
    .setTitle("Delete entry")
    .setMessage("Are you sure you want to delete this entry?")

    // Specifying a listener allows you to take an action before dismissing the dialog.
    // The dialog is automatically dismissed when a dialog button is clicked.
    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // Continue with delete operation
        }
     })

    // A null listener allows the button to dismiss the dialog and take no further action.
    .setNegativeButton(android.R.string.no, null)
    .setIcon(android.R.drawable.ic_dialog_alert)
    .show();

おすすめ記事