Android开发中AlertDialog提示框的使用(超详细)

今天,统计记录一下AlertDialog提示框的常见用法:

1、标准提示框

2、下拉列表提示框

3、下拉单选框提示框

4、下拉多选框提示框

5、自定义提示框(重要)

效果如下:

Android开发中AlertDialog提示框的使用(超详细)_第1张图片

Android开发中AlertDialog提示框的使用(超详细)_第2张图片


activity_alertdialog.xml 布局文件如下:




    

自定义提示框布局文件item_customalertdialog.xml如下:



    

    

AlertDialogDemo.java的代码如下:

public class AlertDialogDemo extends AppCompatActivity implements View.OnClickListener {

    public String[] cities = {"广州", "上海", "北京", "香港", "澳门"};
    public String toastText = "";
    public HashSet hashSet;

    public Button btnStandardAlertDialog, btnListAlertDialog, btnRadioButtonListAlertDialog
            , btnCheckBoxListAlertDialog, btnCustomAlertDialog;

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

        componentInit();
        componentSetOnClickListener();
    }

    public void componentInit(){
        btnStandardAlertDialog = findViewById(R.id.alertdialog_btnStandardAlertDialog);
        btnListAlertDialog = findViewById(R.id.alertdialog_btnListAlertDialog);
        btnRadioButtonListAlertDialog = findViewById(R.id.alertdialog_btnRadioButtonListAlertDialog);
        btnCheckBoxListAlertDialog = findViewById(R.id.alertdialog_btnCheckBoxListAlertDialog);
        btnCustomAlertDialog = findViewById(R.id.alertdialog_btnCustomAlertDialog);
    }

    public void componentSetOnClickListener(){
        btnStandardAlertDialog.setOnClickListener(this);
        btnListAlertDialog.setOnClickListener(this);
        btnRadioButtonListAlertDialog.setOnClickListener(this);
        btnCheckBoxListAlertDialog.setOnClickListener(this);
        btnCustomAlertDialog.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.alertdialog_btnStandardAlertDialog:{
                AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogDemo.this);
                builder.setTitle("提示");
                builder.setIcon(R.drawable.ic_launcher_foreground);
                builder.setMessage("这是一个标准提示框!");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.setNeutralButton("忽略", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.show();
                break;
            }
            case R.id.alertdialog_btnListAlertDialog:{
                AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogDemo.this);
                builder.setTitle("提示");
                builder.setIcon(R.mipmap.ic_launcher);
                builder.setItems(cities, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(AlertDialogDemo.this, cities[which], Toast.LENGTH_SHORT).show();
                    }
                });
                builder.show();
                break;
            }
            case R.id.alertdialog_btnRadioButtonListAlertDialog:{
                AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogDemo.this);
                builder.setTitle("提示");
                builder.setIcon(R.drawable.ic_launcher_foreground);
                builder.setSingleChoiceItems(cities, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        toastText = cities[which];
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(AlertDialogDemo.this, "您选择了:" + toastText, Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.show();
                break;
            }
            case R.id.alertdialog_btnCheckBoxListAlertDialog:{
                hashSet = new HashSet<>();
                AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogDemo.this);
                builder.setTitle("提示");
                builder.setIcon(R.drawable.ic_launcher_foreground);

                builder.setMultiChoiceItems(cities, null, new DialogInterface.OnMultiChoiceClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        if (isChecked){
                            hashSet.add(cities[which]);
                        } else {
                            hashSet.remove(cities[which]);
                        }
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        StringBuilder stringBuilder = new StringBuilder();
                        for (String str : hashSet){
                            stringBuilder.append(str);
                        }
                        Toast.makeText(AlertDialogDemo.this, "您选择了:" + String.valueOf(stringBuilder), Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.show();
                break;
            }
            case R.id.alertdialog_btnCustomAlertDialog:{
                AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogDemo.this);
                builder.setTitle("提示");
                builder.setIcon(R.drawable.ic_launcher_foreground);
                View view = LayoutInflater.from(this).inflate(R.layout.item_customalertdialog, null);
                builder.setView(view);
                final EditText etUserName = view.findViewById(R.id.editText);
                final EditText etPassword = view.findViewById(R.id.editText2);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String userName = etUserName.getText().toString().trim();
                        String Password = etPassword.getText().toString().trim();
                        Toast.makeText(AlertDialogDemo.this, "用户名:" + userName + "\r\n"
                                + "密码:" + Password, Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.show();
                break;
            }
        }

    }
}

你可能感兴趣的:(Android开发中AlertDialog提示框的使用(超详细))