您需要在此活动中使用Theme.AppCompat主题(或子代).在数据适配器中



我试图在RecyclerView中使用AlertDialog,但遇到了以下问题:您需要在此活动中使用Theme.AppCompat主题(或子代(Activity类中的相同AlertDialog可以正常工作,但在DataAdapter中没有。

样式

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.simple">
<uses-permission 
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

数据适配器

private Context mCtx;

holder.moreOptions.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(mCtx,holder.moreOptions);
popupMenu.inflate(R.menu.card_popup_menu);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.delete_note:

AlertDialog.Builder alertDialog = new AlertDialog.Builder(mCtx);
alertDialog.setTitle("Notice");
alertDialog.setMessage("Launching this missile will destroy the entire universe. Is this what you intended to do?");
// add the buttons
alertDialog.setPositiveButton("Launch missile", null);
alertDialog.setNeutralButton("Remind me later", null);
alertDialog.setNegativeButton("Cancel", null);
// create and show the alert dialog
AlertDialog dialog = alertDialog.create();
dialog.show();

您还必须传递一个主题id和Context,如下所示。如果它对你有帮助,那么接受它作为答案。

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this, R.style.Theme_AppCompat_DayNight_Dialog_MinWidth);

此处:

switch (item.getItemId()){
case R.id.delete_note:
//send message to Handler inside MainActivity:
android.os.Message message = new Message();
message.what = MainActivity.SHOW_MY_DIALOG;
MainActivity.myHandler.sendMessage(message);

/* AlertDialog.Builder alertDialog = new AlertDialog.Builder(mCtx);
alertDialog.setTitle("Notice");
alertDialog.setMessage("Launching this missile will destroy the entire universe. Is this what you intended to do?");
// add the buttons
alertDialog.setPositiveButton("Launch missile", null);
alertDialog.setNeutralButton("Remind me later", null);
alertDialog.setNegativeButton("Cancel", null);
// create and show the alert dialog
AlertDialog dialog = alertDialog.create();
dialog.show();
*/
//Now, inside MainActivity, inside onCreate:
myHandler = new Handler(){
@Override
public void handleMessage(Message msg){
super.handleMessage(msg);

if(msg.what == SHOW_MY_DIALOG)
CallMethodToShowDialog(); //this method is inside MainActivity
}
//outside of onCreate in MainActivity:
public void CallMethodToShowDialog(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this/*mCtx*/); //LOOK HERE!!!!
alertDialog.setTitle("Notice");
alertDialog.setMessage("Launching this missile will destroy the entire universe. Is this what you intended to do?");
// add the buttons
alertDialog.setPositiveButton("Launch missile", null);
alertDialog.setNeutralButton("Remind me later", null);
alertDialog.setNegativeButton("Cancel", null);
// create and show the alert dialog
AlertDialog dialog = alertDialog.create();
dialog.show();
}

相关内容

最新更新