我已將我的項目遷移到 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.
添加回答
舉報
0/150
提交
取消