How to Build AppCompatDialog From AlertDialog.Builder or Equivalent?

非 Y 不嫁゛ 提交于 2019-12-04 18:10:56

问题


Before this I used a DialogBuilder to create AlertDialog like this

AlertDialog.Builder builder = new AlertDialog.Builder(context);
...
...
AlertDialog dialog = builder.create();

How can I build the new AppCompatDialog from a dialog builder, or is there another new equivalent way to do that?


回答1:


Just found the solution. I should import

import android.support.v7.app.AlertDialog;

and then AppCompatDialog dialog = builder.create() will work.




回答2:


If you would like to use an AlertDialog, just import the new supprt v 22.1 and use a code like this (pay attention to the import):

import android.support.v7.app.AlertDialog

AlertDialog.Builder builder =
       new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);
            builder.setTitle("Dialog");
            builder.setMessage("Lorem ipsum dolor ....");
            builder.setPositiveButton("OK", null);
            builder.setNegativeButton("Cancel", null);
            builder.show();

If




回答3:


android.support.v7.app.AppCompatDialog is direct parent class of android.support.v7.app.AlertDialog, wherever you can use android.support.v7.app.AlertDialog, you can use android.support.v7.app.AppCompatDialog.




回答4:


I've just moved all my android.app.AlertDialog to android.support.v7.app.AlertDialog.

After some testing with 4.X emulators I've found that for a simple dialog it's enough to simply change the import. But for multiple choice dialogs, additionally, you need to do AppCompatDialog alert = builder.create(); to get the Material Design style dialogs (on 4.X).

To be clear, if you have a simple dialog like this one:

import android.support.v7.app.AlertDialog;

AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setIcon(resId)
.setTitle(title)
.setMessage(msg)
.setCancelable(isCalncelable)
.setPositiveButton(btn1, listener1);
AlertDialog alert = builder.create();
alert.show();

Changing the import will suffice :)

But for a multi choice dialog, you need to use AppCompatDialog like this:

import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatDialog;

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Choose something")
.setPositiveButton(...)
.setMultiChoiceItems(mStringArray, mSelectedArray, SomeFragment.this);
AppCompatDialog alert = builder.create();
alert.show();

Then you get the nice Material Design look on 4.X devices.

Now the fun part!

For a multi choice dialog, on a 5.X device, the native version (android.app.AlertDialog) shows the check-boxes at the left, correctly following the Material Design spec. But if you use the support dialogs, then the check-boxes will appear at the right. WTF!

On the long term, as Android 5+ gains market share, you will want to switch back to native dialogs.



来源:https://stackoverflow.com/questions/29786169/how-to-build-appcompatdialog-from-alertdialog-builder-or-equivalent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!