我还可以在Android中使用AlertDialog来支持API 17吗?

时间:2017-01-17 12:11:43

标签: android android-alertdialog android-dialogfragment

在以下示例中,我已弃用onCreateDialogshowDialog

package com.dialogtest;

import android.app.Dialog;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    CharSequence[] items = {"Google", "Apple", "Microsoft"};

    // Declare the boolean array of same size as items
    boolean[] itemsChecked = new boolean[items.length];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onClick(View v) {
        showDialog(1);
    }

    @Override
    protected Dialog onCreateDialog(int id) {

        switch (id) {

            case 1:
                return new AlertDialog.Builder(this)
                        //.setIcon(R.drawable.ic_launcher)
                        .setTitle("This is a dialog with some simple text...")
                        .setPositiveButton("OK",
                                new DialogInterface.OnClickListener() {

                                    public void onClick(DialogInterface dialog, int whichButton) {
                                        Toast.makeText(getBaseContext(),
                                                "OK clicked!", Toast.LENGTH_SHORT).show();
                                    }
                                }
                        )

                        .setNegativeButton("Cancel",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int whichButton) {
                                        Toast.makeText(getBaseContext(),
                                                "Cancel clicked!", Toast.LENGTH_SHORT).show();
                                    }
                                }
                        )

                        .setMultiChoiceItems(items, itemsChecked,
                                new DialogInterface.OnMultiChoiceClickListener() {
                                    public void onClick(DialogInterface dialog,
                                                        int which, boolean isChecked) {
                                        Toast.makeText(getBaseContext(),
                                                items[which] + (isChecked ? " checked!" : " unchecked!"),
                                                Toast.LENGTH_SHORT).show();
                                    }
                                }
                        ).create();


        }
        return null;
    }
}

我被建议使用DialogFragment,但我不太确定。

所以我想知道不再可以直接使用AlertDialog了吗?

2 个答案:

答案 0 :(得分:0)

您应该使用appcompact版本的警告对话框点击Here

答案 1 :(得分:0)

根据reference docs AlertDialog是Dialog的子类,可以显示一个,两个或三个按钮。

在您的情况下使用以下代码:

public void onClick(View v) {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(ExampleActivity.this);
            //.setIcon(R.drawable.ic_launcher)
    alertDialog.setTitle("This is a dialog with some simple text...")
    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            Toast.makeText(getBaseContext(),
                                    "OK clicked!", Toast.LENGTH_SHORT).show();
                        }
                    }
            )
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            Toast.makeText(getBaseContext(),
                                    "Cancel clicked!", Toast.LENGTH_SHORT).show();
                        }
                    }
            )
            .setMultiChoiceItems(items, itemsChecked, new DialogInterface.OnMultiChoiceClickListener() {
                        public void onClick(DialogInterface dialog,
                                            int which, boolean isChecked) {
                            Toast.makeText(getBaseContext(),
                                    items[which] + (isChecked ? " checked!" : " unchecked!"),
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
            .show();
}