亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 androidX 中實現警報對話框

如何在 androidX 中實現警報對話框

侃侃無極 2023-08-04 15:01:15
我已將我的項目遷移到 androidX,并且我想實現一個帶有用戶正面和負面反饋的警報對話框。我正在使用這段代碼:AlertDialog.Builder builder1 = new AlertDialog.Builder(getApplicationContext());               builder1.setMessage("Write your message here.");               builder1.setCancelable(true);               builder1.setPositiveButton(                       "Yes",                       new DialogInterface.OnClickListener() {                           public void onClick(DialogInterface dialog, int id) {                               Log.d("MSG", "onClick: YES");                           }                       });               builder1.setNegativeButton(                       "No",                       new DialogInterface.OnClickListener() {                           public void onClick(DialogInterface dialog, int id) {                               dialog.cancel();                               Log.d("MSG", "onClick: No");                           }                       });               AlertDialog alert11 = builder1.create();               alert11.show();但我在運行應用程序時收到此錯誤:java.lang.IllegalStateException:您需要在此活動中使用 Theme.AppCompat 主題(或后代)。
查看完整描述

1 回答

?
小唯快跑啊

TA貢獻1863條經驗 獲得超2個贊

您可以使用Material Components 庫MaterialAlertDialogBuilder提供的。

只需使用:

new MaterialAlertDialogBuilder(context)

? ? ? ? ? ? .setTitle("Dialog")

? ? ? ? ? ? .setMessage("Write your message here. ....")

? ? ? ? ? ? .setPositiveButton("Ok", /* listener = */ null)

? ? ? ? ? ? .setNegativeButton("Cancel", /* listener = */ null)

? ? ? ? ? ? .show();

需要MaterialAlertDialogBuilder一個 Material 主題并將生成一個androidx.appcompat.app.AlertDialog.


查看完整回答
反對 回復 2023-08-04
  • 1 回答
  • 0 關注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號