前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >常用弹出对话窗集合

常用弹出对话窗集合

作者头像
项勇
发布2019-12-23 17:49:10
8250
发布2019-12-23 17:49:10
举报
文章被收录于专栏:项勇

1./普通对话框/

代码语言:javascript
复制
/**普通对话框**/
private void showNormalDialog(){
    final AlertDialog.Builder normalDialog = new AlertDialog.Builder(this);
    normalDialog.setIcon(R.mipmap.tr_step_left_normal);
    normalDialog.setTitle("DiaLog Title");
    normalDialog.setMessage("DiaLog Message");
    normalDialog.setPositiveButton("Positive",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    showToast("Positive");
                }
            });
    normalDialog.setNegativeButton("Negative",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    showToast("Negative");
                }
            });
    normalDialog.show();
}

2./普通对话框 增加第三个选择按钮/

代码语言:javascript
复制
/**普通对话框 增加第三个选择按钮**/
private void showMultiBtnDialog(){
    AlertDialog.Builder normalDialog =
            new AlertDialog.Builder(MainActivity.this);
    normalDialog.setIcon(R.mipmap.tr_step_left_normal);
    normalDialog.setTitle("DiaLog Title").setMessage("DiaLog Message");
    normalDialog.setPositiveButton("Positive",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    showToast("Positive");
                }
            });
    normalDialog.setNeutralButton("Neutral2",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    showToast("Neutral2");
                }
            });
    normalDialog.setNegativeButton("Negative", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            showToast("Negative");
        }
    });
    // 创建实例并显示
    normalDialog.show();
}

3./列表选择对话框/

代码语言:javascript
复制
/**列表选择对话框**/
private void showListDialog() {
    final String[] items = { "items1","items2","items3","items4" };
    AlertDialog.Builder listDialog =
            new AlertDialog.Builder(MainActivity.this);
    listDialog.setTitle("DiaLog Title");
    listDialog.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            showToast("点击 "+items[which]);
        }
    });
    listDialog.show();
}

4./列表单选对话框/

代码语言:javascript
复制
/**列表单选对话框**/
int mChoice;
private void showSingleChoiceDialog(){
    final String[] items = { "items1","items2","items3","items4" };
    AlertDialog.Builder singleChoiceDialog =
            new AlertDialog.Builder(MainActivity.this);
    singleChoiceDialog.setTitle("DiaLog Title");
    // 第二个参数是默认选项,此处设置为0
    singleChoiceDialog.setSingleChoiceItems(items, mChoice,
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mChoice = which;
                }
            });
    singleChoiceDialog.setPositiveButton("Positive",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (mChoice != -1) {
                        showToast("选择 "+items[mChoice]);
                    }
                }
            });
    singleChoiceDialog.show();
}

5./列表多选对话框/

代码语言:javascript
复制
/**列表多选对话框**/
ArrayList<String> mChoices = new ArrayList<>();
final boolean initChoiceSets[]={false,false,false,false};
private void showMultiChoiceDialog() {
    final String[] items = { "items1","items2","items3","items4" };
    AlertDialog.Builder multiChoiceDialog = new AlertDialog.Builder(this);
    multiChoiceDialog.setTitle("DiaLog Title");
    multiChoiceDialog.setMultiChoiceItems(items, initChoiceSets,
            new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which,
                                    boolean isChecked) {
                    initChoiceSets[which] = isChecked;
                    if (isChecked) {
                        mChoices.add(items[which]);
                    } else {
                        String  s  = items[which];
                        Log.i("md"," findInterIdex(mChoices,s):  "+findInterIdex(mChoices,s));
                        mChoices.remove(findInterIdex(mChoices,s));
                    }
                }
            });
    multiChoiceDialog.setPositiveButton("Positive",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    int size = mChoices.size();
                    String str = "";
                    for (int i = 0; i < size; i++) {
                        str += mChoices.get(i) ;
                    }


                    showToast("选择了 "+str);
                }
            });
    multiChoiceDialog.show();
}
public static int findInterIdex(List<String> nums, String target){
    return Collections.binarySearch(nums,target);
}

6./进度弹出框/

代码语言:javascript
复制
/**进度弹出框**/
private void showProgressDialog() {
    final int MAX_PROGRESS = 100;
    final ProgressDialog progressDialog =
            new ProgressDialog(MainActivity.this);
    progressDialog.setProgress(0);
    progressDialog.setTitle("ProgressDialog Title");
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    progressDialog.setMax(MAX_PROGRESS);
    progressDialog.show();
    /* 模拟进度增加的过程
     * 新开一个线程,每个100ms,进度增加1
     */
    new Thread(new Runnable() {
        @Override
        public void run() {
            int progress= 0;
            while (progress < MAX_PROGRESS){
                try {
                    Thread.sleep(100);
                    progress++;
                    progressDialog.setProgress(progress);
                } catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
            // 进度达到最大值后,窗口消失
            progressDialog.cancel();
        }
    }).start();
}

7./输入对话框/

代码语言:javascript
复制
/**输入对话框**/
private void showInputDialog() {
    final EditText editText = new EditText(MainActivity.this);
    AlertDialog.Builder inputDialog =
            new AlertDialog.Builder(MainActivity.this);
    inputDialog.setTitle("AlertDialog Title").setView(editText);
    inputDialog.setPositiveButton("Positive",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    showToast("输入了 "+editText.getText().toString());
                }
            }).show();
}

8./自定义输入对话框/

代码语言:javascript
复制
/**自定义输入对话框**/
private void showCustomizeDialog() {
    AlertDialog.Builder customizeDialog =
            new AlertDialog.Builder(MainActivity.this);
    final View dialogView = LayoutInflater.from(MainActivity.this)
            .inflate(R.layout.dialog_customize,null);
    customizeDialog.setTitle("自定义AlertDialog Title");
    customizeDialog.setView(dialogView);
    dialogView.findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showToast("输入了 "+((EditText)dialogView.findViewById(R.id.edit_text)).getText().toString());
        }
    });
    customizeDialog.show();
}

分割

代码语言:javascript
复制
<!-- 自定义View -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    >


    <EditText
        android:id="@+id/edit_text"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        />


    <ImageView
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/tr_step_left_normal"
        />
</LinearLayout>
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-12-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 项勇 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1./普通对话框/
  • 2./普通对话框 增加第三个选择按钮/
  • 3./列表选择对话框/
  • 4./列表单选对话框/
  • 5./列表多选对话框/
  • 6./进度弹出框/
  • 7./输入对话框/
  • 8./自定义输入对话框/
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档