前提・実現したいこと
ダイアログ内のボタンを押されたら、特定の処理の後、ダイアログを閉じたい
発生している問題・エラーメッセージ
ボタンクリックイベント内のdismissまで実行されるが、エラーは出ないがダイアログが開いたままとなる
該当のソースコード
java
1 // カスタムビューを設定 2 LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE); 3 View layout = inflater.inflate(R.layout.time_dialog, (ViewGroup) findViewById(R.id.time_layout)); 4 5 // アラーとダイアログ を生成 6 final AlertDialog.Builder alertDlg = new AlertDialog.Builder(this); 7 // タイトル部分のTextView 8 TextView textView = new TextView(this); 9 // タイトルの背景色 10 textView.setBackgroundColor(ContextCompat.getColor(this, R.color.dialog_background_color)); 11 // タイトルの文字色 12 textView.setTextColor(getResources().getColor(R.color.font_color)); 13 textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 14 int paddingLeftRight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 0, getResources().getDisplayMetrics()); 15 int paddingTopBottom = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics()); 16 textView.setPadding(paddingLeftRight, paddingTopBottom, paddingLeftRight, paddingTopBottom); 17 // タイトルのテキスト設定 18 textView.setText(getString(R.string.time_title)); 19 // テキストサイズ 20 textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20); 21 // タイトル部分にTextViewをセット 22 alertDlg.setCustomTitle(textView); 23 24 alertDlg.setView(layout); 25 26 // 表示 27 alertDlg.show(); 28 29 // ボタンが押されたとき 30 time_button =(Button)layout.findViewById(R.id.time_button); 31 time_button.setOnClickListener(new View.OnClickListener() { 32 @Override 33 public void onClick(View v) { 34 alertDlg.create().dismiss(); 35 } 36 }); 37 38 } 39
試したこと
alertDlg.create().dismiss();
alertDlg.create().cancel();
上記2つは試したがどちらも閉じない。
また、finish();だとダイアログ呼び出し元のActivityまで閉じてしまう。
補足情報(FW/ツールのバージョンなど)
Android Studio3.4
Android対象API14から28
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。