How to Build AppCompatDialog From AlertDialog.Buil

2019-03-25 09:32发布

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?

4条回答
狗以群分
2楼-- · 2019-03-25 09:43

Just found the solution. I should import

import android.support.v7.app.AlertDialog;

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

查看更多
聊天终结者
3楼-- · 2019-03-25 09:45

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

查看更多
Luminary・发光体
4楼-- · 2019-03-25 09:51

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.

查看更多
看我几分像从前
5楼-- · 2019-03-25 09:57

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.

查看更多
登录 后发表回答