• Android-原生对话框


    package liudeli.ui.all;
    
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.ProgressDialog;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.os.SystemClock;
    import android.view.View;
    import android.widget.Toast;
    
    public class DialogActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_dialog);
        }
    
        /**
         * 普通对话框
         * @param view
         */
        public void commonDialog(View view) {
    
            // AlertDialog.Builder 使用了构建器模式(相当于你把所有材料给工人,工人把制作好的房子给你)
            AlertDialog.Builder builder =
                    new AlertDialog.Builder(DialogActivity.this); // 注意:这里只能使用当前Activity的this,
                                                                          // 不能使用:getApplicationContext(); getBaseContext(); 否则会有异常
    
            builder.setCancelable(false); // 设置点击Dialog其他区域不隐藏对话框,默认是true
    
            builder.setTitle("警告");
            builder.setMessage("你确定要清空所有资料吗 ?");
    
            // PositiveButton 右边的按钮
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    alertToast("点击了确定");
                }
            });
    
            // 左边的按钮
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    alertToast("点击了取消");
                }
            });
    
            // 最左边的按钮
            builder.setNeutralButton("明天提醒我", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    alertToast("明天这个时候通知你");
                }
            });
    
    
            AlertDialog alertDialog = builder.create(); // 工人把制作好的房子给你
            alertDialog.show();
        }
    
        /**
         * 列表对话框(列表条目对话框,列表单选对话框,列表多选对话框)
         * @param view
         */
        public void listDialog(View view) {
            // AlertDialog.Builder 使用了构建器模式(相当于你把所有材料给工人,工人把制作好的房子给你)
            final AlertDialog.Builder builder =
                    new AlertDialog.Builder(DialogActivity.this); // 注意:这里只能使用当前Activity的this,
                                                                          // 不能使用:getApplicationContext(); getBaseContext(); 否则会有异常
    
            builder.setCancelable(false); // 设置点击Dialog其他区域不隐藏对话框,默认是true
    
            builder.setTitle("选择");
    
            /**
             * ⚠️注意:列表对话框,千万不能设置setMessage,否则列表数据不能显示
             */
            // builder.setMessage("请选择以下编程语言");
    
            builder.setIcon(R.mipmap.ic_launcher);
    
            /**
             * 只要Dialog隐藏了就是调用这个监听
             */
            builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    alertToast("Dismiss");
                }
            });
    
            /**
             * 可以设置适配器,可以当作ListView来使用
             */
            // builder.setAdapter();
    
            String[] items = new String[]{"Java", "Android", "iOS"};
    
            /**
             * 设置列表数据 列表条目形式
             */
            /*builder.setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    alertToast(which + "");
                }
            });*/
    
            /**
             * 设置列表数据 列表多选形式
             */
            /*builder.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    alertToast(which + "");
                }
            });*/
    
            /**
             * 设置列表数据 列表单选形式
             */
            builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    alertToast(which + "");
                    dialog.dismiss();
                }
            });
    
            // 必须放在后面 最后才能制作好
            AlertDialog alertDialog = builder.create(); // 工人把制作好的房子给你
    
            alertDialog.show();
        }
    
        /**
         * 进度条对话框(默认是不确定进度条,可以设置样式为确定进度条)
         * @param view
         */
        public void progressDialog(View view) {
            final ProgressDialog progressDialog = new ProgressDialog(this); // 注意:这里只能使用当前Activity的this,
                                                                              // 不能使用:getApplicationContext(); getBaseContext(); 否则会有异常
            progressDialog.setTitle("提示");
            progressDialog.setTitle("正在加载中,可能要等待大半天");
            progressDialog.setCancelable(true); // 默认就是true
    
            // 设置为 确定进度条 (默认是不确定进度条)
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDialog.setMax(100);
    
            new Thread(){
                @Override
                public void run() {
                    super.run();
                    for(int i=0; i< progressDialog.getMax(); i++) {
                        // 使用Android提供的睡眠
                        SystemClock.sleep(300);
                        progressDialog.setProgress(i + 1);
                    }
                }
            }.start();
    
            progressDialog.show();
        }
    
        private void alertToast(String text) {
            Toast.makeText(DialogActivity.this, text, Toast.LENGTH_LONG).show();
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
        }
    }

    Layout:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="普通对话框"
            android:onClick="commonDialog"
            />
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="列表对话框"
            android:onClick="listDialog"
            />
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="进度条对话框"
            android:onClick="progressDialog"
            />
    
    
    </LinearLayout>

  • 相关阅读:
    将Vim改造为强大的IDE
    Ubuntu首次开启root用户
    ssh-keygen实现免密码登陆
    Ubuntu下配置samba服务器实现文件共享
    Ubuntu下Apache+php+mysql网站架设详解
    IIS支持PHP
    详解C/C++预处理器
    学C++之感悟
    return *this和return this的区别
    const 的全面总结
  • 原文地址:https://www.cnblogs.com/android-deli/p/10098883.html
Copyright © 2020-2023  润新知