Android AlertDialog 各种弹框代码

1、消息框
btn01.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder ab = new AlertDialog.Builder(BtnActivity.this);
ab.setTitle("消息标题");
ab.setMessage("提示内容提示内容提示内容");
ab.setPositiveButton("确定", null);
ab.show();

        }
    });

2、确认框
btn02.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder ab = new AlertDialog.Builder(BtnActivity.this);
ab.setTitle("消息标题");
ab.setMessage("提示内容提示内容提示内容");
ab.setNegativeButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(BtnActivity.this, "点击了确认", Toast.LENGTH_SHORT).show();
}
});
ab.setPositiveButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(BtnActivity.this, "点击了取消", Toast.LENGTH_SHORT).show();
}
});
ab.show();
}
});
3、输入框
btn03.setOnClickListener(new View.OnClickListener() {
EditText editText = new EditText(BtnActivity.this);
String Getstr = "";

        @Override
        public void onClick(View v) {

            editText = new EditText(BtnActivity.this);
            editText.setText(Getstr);
            AlertDialog.Builder ab = new AlertDialog.Builder(BtnActivity.this);
            ab.setTitle("消息标题");
            ab.setIcon(android.R.mipmap.sym_def_app_icon);
            ab.setView(editText);
            ab.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Getstr = editText.getText().toString();
                    Toast.makeText(BtnActivity.this, Getstr, Toast.LENGTH_SHORT).show();
                }
            });
            ab.setNegativeButton("取消", null);
            ab.show();
        }
    });

4、单选框
btn04.setOnClickListener(new View.OnClickListener() {
String GetSelect = "";
int index = 0;
String[] Items = new String[]{"选项1", "选项2", "选项3", "选项4"};

        @Override
        public void onClick(View v) {
            AlertDialog.Builder ab = new AlertDialog.Builder(BtnActivity.this);
            ab.setTitle("单选框");
            ab.setIcon(android.R.mipmap.sym_def_app_icon);
            ab.setSingleChoiceItems(Items, index,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            index = which;
                            Toast.makeText(BtnActivity.this, Items[which].toString(), Toast.LENGTH_SHORT).show();
                            GetSelect = Items[which].toString();

// dialog.dismiss();//关闭对话框
}
}
);
ab.setNegativeButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(BtnActivity.this, "你选择了:" + GetSelect, Toast.LENGTH_SHORT).show();
}
});
ab.show();

        }
    });

5、复选框
btn05.setOnClickListener(new View.OnClickListener() {
String GetSelect = "";
String[] Items = new String[]{"选项1", "选项2", "选项3", "选项4"};

        @Override
        public void onClick(View v) {
            AlertDialog.Builder ab = new AlertDialog.Builder(BtnActivity.this);
            ab.setTitle("复选框");
            ab.setIcon(android.R.mipmap.sym_def_app_icon);
            ab.setMultiChoiceItems(Items, null, new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    if (isChecked) {
                        Toast.makeText(BtnActivity.this, "你选择了:" + Items[which].toString(), Toast.LENGTH_SHORT).show();
                        GetSelect += Items[which].toString() + "-";
                    } else {
                        Toast.makeText(BtnActivity.this, "你取消了:" + Items[which].toString(), Toast.LENGTH_SHORT).show();
                    }
                }
            });
            ab.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(BtnActivity.this, "你选择了:" + GetSelect, Toast.LENGTH_SHORT).show();
                }
            });
            ab.setNegativeButton("取消", null);
            ab.show();

        }
    });

6、列表框
btn06.setOnClickListener(new View.OnClickListener() {
String GetSelect = "";
int index = 0;
String[] Items = new String[]{"列表项1", "列表项2", "列表项3", "列表项4", "列表项4", "列表项4", "列表项4", "列表项2", "列表项3", "列表项4", "列表项2", "列表项3", "列表项4"};

        @Override
        public void onClick(View v) {
            AlertDialog.Builder ab = new AlertDialog.Builder(BtnActivity.this);
            ab.setTitle("列表框");
            ab.setIcon(android.R.mipmap.sym_def_app_icon);
            ab.setItems(Items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    index = which;
                    Toast.makeText(BtnActivity.this, Items[which].toString(), Toast.LENGTH_SHORT).show();
                    GetSelect = Items[which].toString();

                }
            });
           /* ab.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(BtnActivity.this,GetSelect,Toast.LENGTH_SHORT).show();
                }
            });
            ab.setNegativeButton("取消", null);*/
            ab.show();
        }
    });

7、图片框
btn07.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageView img = new ImageView(BtnActivity.this);
img.setImageResource(R.mipmap.ic_launcher);
AlertDialog.Builder ab = new AlertDialog.Builder(BtnActivity.this);
ab.setTitle("图片框");
ab.setIcon(android.R.mipmap.sym_def_app_icon);
ab.setView(img);
ab.setPositiveButton("确定", null);
ab.setNegativeButton("取消", null);
ab.show();
}
});

原文地址:https://www.cnblogs.com/lzsin/p/10363499.html

时间: 2024-11-03 21:13:50

Android AlertDialog 各种弹框代码的相关文章

Android截图截取弹框AlertDialog

1:效果如图 2:权限 <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="21" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permissi

二级弹框代码

using UnityEngine; using System.Collections; /// <summary> /// Pop button. /// </summary> public class BtnBop : MonoBehaviour { //设置对话框 public GameObject mSettingDlg; public POP_DLG_TYPE mCurDlgType=POP_DLG_TYPE.DLG_NONE; //4 pop up button //p

android之自定义弹框

step1 创建窗体 final AlertDialog dialog =new Builder(this).create(); step2 获取View View viewDialog =View.inflate(this, R.layout.userinfo_dialog_edit, null); final EditText txtUserName=(EditText)viewDialog.findViewById(R.id.txtUserName); final EditText txt

弹框代码

package com.edu.demo; import java.util.*;import java.io.*; import javax.swing.JFileChooser;import javax.swing.JOptionPane; public class ReadFileUsingJFileChooser { /**     * @param args     * @throws Exception      */    public static void main(Strin

CSS3新vw, vh单位与纯CSS定位的弹框屏幕居中效果实例页面

在css中vw和vh分别代表所占的百分比,可以设置最外部的容器的宽高,但是都要结合百分比一起使用. 展示 回到相关文章 ? 图片宽度(目前1024像素): 128  1024 点击我出现图片弹框 代码CSS代码:.dialog_container {    display: none;    width: 100%;    width: 100vw;    height: 100%;    height: 100vh;    background-color: rgba(0,0,0,.35); 

android如果给TextView或EditText的email链接加下划线,并在点击在email连接上可以弹框显示

如何把textview的一些文字加上背景色: Spannable str = new SpannableString("#fdsfdfsdfdsfd#"); Matcher matcher = getEmailPattern().matcher((CharSequence) str); while (matcher.find()) { int start = matcher.start(); int end = matcher.end(); str.setSpan(new Foregr

android 三种弹出框之一poprpWindow

poprpWindow 在android的弹出框我目前了解到的是有三种:AlertDialog,poprpWindow,Activity伪弹框, AlertDialog太熟悉了,这里就不介绍了 就先看看poprpWindow API 给出的解释是: 意思就是一个展示view的弹出窗体,这个弹出窗体将会浮动在当前activity的最上层, 它和AlertDialog的区别是:在android中弹出框有两种方式:AlertDialog和PopupWindow,它们的不同点在于:      1.Ale

Android 退出提示框 代码

转自:http://hi.baidu.com/ittdt/item/d932cf37f486f886c3cf29ea new AlertDialog.Builder(MainEngine.context)    //.setTitle("提示")    .setMessage("确定要退出游戏吗?")    .setPositiveButton("确定", new DialogInterface.OnClickListener() {     @

【Android】不弹root请求框检测手机是否root

由于项目需要root安装软件,并且希望在合适的时候引导用户去开启root安装,故需要检测手机是否root. 最基本的判断如下,直接运行一个底层命令.(参考https://github.com/Trinea/android-common/blob/master/src/cn/trinea/android/common/util/ShellUtils.java) 也可参考csdn http://blog.csdn.net/fm9333/article/details/12752415 1 /** 2