Android经常使用的五种弹出对话框[通俗易懂]

Android经常使用的五种弹出对话框

大家好,又见面了,我是全栈君。

  一个Android开发中经常使用对话框的小样例,共同拥有五种对话框:普通弹出对话框,单选对话框,多选对话框,输入对话框及进度条样式对话框:

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
    xmlns:tools=”http://schemas.android.com/tools”
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:orientation=”vertical” >

    <Button
        android:id=”@+id/common_dialog”
        android:layout_width=”match_parent”
        android:layout_height=”40dp”
        android:text=”普通对话框”
        android:textSize=”16sp”
        android:layout_marginTop=”10dp” />

    <Button
        android:id=”@+id/radio_dialog”
        android:layout_width=”match_parent”
        android:layout_height=”40dp”
        android:text=”单选对话框”
        android:textSize=”16sp”
        android:layout_marginTop=”10dp”  />

    <Button
        android:id=”@+id/check_dialog”
        android:layout_width=”match_parent”
        android:layout_height=”40dp”
        android:text=”多选对话框” 
        android:textSize=”16sp”
        android:layout_marginTop=”10dp” />

    <Button
        android:id=”@+id/input_dialog”
        android:layout_width=”match_parent”
        android:layout_height=”40dp”
        android:text=”输入文字对话框” 
        android:textSize=”16sp”
        android:layout_marginTop=”10dp” />

    <Button
        android:id=”@+id/progress_dialog”
        android:layout_width=”match_parent”
        android:layout_height=”40dp”
        android:text=”进度条对话框” 
        android:textSize=”16sp”
        android:layout_marginTop=”10dp” />

</LinearLayout>

以下是输入内容的简单布局activity_input.xml

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
    xmlns:tools=”http://schemas.android.com/tools”
    android:id=”@+id/LinearLayout1″
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:orientation=”vertical” >

    <TextView
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:text=”@string/hello_world” />

    <EditText
        android:id=”@+id/uname”
        android:layout_width=”fill_parent”
        android:layout_height=”wrap_content” />

    <TextView
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:text=”@string/hello_world” />

    <EditText
        android:id=”@+id/upass”
        android:layout_width=”fill_parent”
        android:layout_height=”wrap_content” />

</LinearLayout>

代码及凝视:

public class MainActivity extends Activity implements OnClickListener {
/**单选框模拟标题 大学*/
private final static int CHECKED_ENU = 0;
/**单选框模拟标题  高中*/
private final static int CHECKED_SEL = 1;
/**单选框模拟标题  初中*/
private final static int CHECKED_CHU = 2;
/**复选button状态为全选 */
private boolean[] checked = { true, true, true, false };
/**模拟的进度值 */
private int progressNumber;
/**进度对话框 */
private ProgressDialog progressDialog;
/**相应button*/
private Button commonBtn, radioBtn, checkBtn, inputBtn, progressBtn;

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

/**初始化UI控件*/

private void initViews() {
this.commonBtn = (Button) findViewById(R.id.common_dialog);
this.radioBtn = (Button) findViewById(R.id.radio_dialog);
this.checkBtn = (Button) findViewById(R.id.check_dialog);
this.inputBtn = (Button) findViewById(R.id.input_dialog);
this.progressBtn = (Button) findViewById(R.id.progress_dialog);
}

/**注冊button监听事件*/
private void initListeners() {
this.commonBtn.setOnClickListener(this);
this.radioBtn.setOnClickListener(this);
this.checkBtn.setOnClickListener(this);
this.inputBtn.setOnClickListener(this);
this.progressBtn.setOnClickListener(this);
}

/**普通对话框 */
private Dialog buildAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle(“对话框”);
builder.setMessage(“您的password不正确!!”);

ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.mm1);
/**设置背景图片*/
builder.setView(imageView);
/**左边button*/
builder.setPositiveButton(“确定”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle(“您点击的是左边确定button!”);
}
});
/**中间button*/
builder.setNeutralButton(“详情”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle(“您点击的是中间详情button!”);
}
});
/**右边button*/
builder.setNegativeButton(“取消”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
setTitle(“您点击的是右边取消button!”);
}
});
return builder.create();
}

/**单选button弹出框 */
private Dialog buildAlertDialog_radio() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle(“对话框”);
/**单选button,默认高中被选中*/
builder.setSingleChoiceItems(new String[] { “大学”, “高中”, “初中”, “小学” }, 1, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
switch (which) {
case CHECKED_ENU:
setTitle(“大学”);
break;
case CHECKED_SEL:
setTitle(“高中”);
break;
case CHECKED_CHU:
setTitle(“初中”);
break;
default:
setTitle(“小学”);
break;
}
}
});

builder.setPositiveButton(“确定”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle(“您点击的是左边确定button!”);
}
});
builder.setNegativeButton(“取消”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle(“您点击的是右边取消button!”);
}
});
return builder.create();
}

/**能够多选button弹出框 */
private Dialog buildAlertDialog_checkbox() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle(“对话框”);
/**复选button*/
builder.setMultiChoiceItems(new String[] { “大学”, “高中”, “初中”, “小学” }, checked, new DialogInterface.OnMultiChoiceClickListener() {

@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
setTitle(“which=” + which + “—–” + “isChecked=” + isChecked);
}
});

builder.setPositiveButton(“确定”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle(“您点击了确定button!”);
}
});
builder.setNegativeButton(“取消”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
setTitle(“您点击的是了取消button!”);
}
});
return builder.create();
}

/**含能够输入文本的弹出框 */
private Dialog buildAlertDialog_input() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle(“对话框”);
LayoutInflater inflater = LayoutInflater.from(this);
builder.setView(inflater.inflate(R.layout.activity_input, null));
builder.setPositiveButton(“确定”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle(“您点击的是确定button!”);
}
});
builder.setNegativeButton(“取消”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle(“您点击的是取消button!”);
}
});
return builder.create();
}

/**进度对话框 */
private Dialog buildAlertDialog_progress() {
progressDialog = new ProgressDialog(this);
progressDialog.setTitle(“进度条”);
progressDialog.setMessage(“正在下载………..”);
/**进度条样式 */
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
/**模糊效果 */
progressDialog.setIndeterminate(false);
return progressDialog;
}

/**每隔0.3秒更新一次进度 */
public void updateProgress() {
new Thread() {
@Override
public void run() {
try {
while (progressNumber <= 100) {
progressDialog.setProgress(progressNumber++);
Thread.sleep(300);
super.run();
}
/**下载完后,关闭下载框 */
progressDialog.cancel();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.common_dialog:
buildAlertDialog().show();
break;
case R.id.radio_dialog:
buildAlertDialog_radio().show();
break;
case R.id.check_dialog:
buildAlertDialog_checkbox().show();
break;
case R.id.input_dialog:
buildAlertDialog_input().show();
break;
case R.id.progress_dialog:
buildAlertDialog_progress().show();
updateProgress();
break;
default:
break;
}
}
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115694.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • wangeditor富文本编辑器_vue使用富文本编辑器

    wangeditor富文本编辑器_vue使用富文本编辑器一、导入kindeditor文件,并删除不用的服务器版本,这里选用jsp修改文件修改第16行代码uploadJson=K.undef(self.uploadJson,self.basePath+’jsp/upload_json.jsp’),修改图片上传路径//文件保存目录路径StringsavePath=pageContext.getServletContext().g

    2022年10月12日
    5
  • 单片机控制步进电机-AVR详细程序

    单片机控制步进电机-AVR详细程序单片机控制步进电机-单片机程序(avr)硬件线路连接图见上一篇文章软件:ICCV7FORAVR-写程序Progisp-烧程序速度S曲线生成器(后续后单独讲解)-生成S曲线数组代码硬件:Atmega16ASP下载线杜邦线控制原理:利用单片机定时器控制IO口高低电平产生脉冲,通过定时器控制每个脉冲的时间,以及脉冲的个数,从而控制步进电机速度以及转动角度,实现步进电机开环控制能…

    2022年5月6日
    64
  • PyCharm使用技巧:查找 替换[通俗易懂]

    PyCharm使用技巧:查找 替换[通俗易懂]文件内部的字符的查抄替换。首先ctrl+F:然后ctrl+R:点击后就能把文件里包含的都替换掉。另外,这个能让你只选择单词。

    2022年8月25日
    8
  • C语言socket实现文件下载[通俗易懂]

    C语言socket实现文件下载[通俗易懂]是网络编程的作业,我比较菜。。。写到定位输出,做百分比出现了问题,不显示0到100的,直接从0跳到了100。请教了下大佬。改了过来。原来是类型的问题,做出来的运算应该是float,但是我都定义的int,输出也是,大佬指出后,我好尴尬。。。犯了小错误。。。但是在这次过程也学习了一波。要加油了!!!代码写的这么乱!!!写的是带颜色的版本,,颜色有可能会觉得妖艳,,,好吧。编译平台是vc++6.0

    2022年7月14日
    15
  • 配置管理项的定义

    配置管理项的定义项目立项后 无论项目规模如何 基于管理需要 我们都会建立配置管理计划 配合项目的有效管理 根据项目特点及管理模式 项目配置项的建立各不相同 可是 无论怎么改变 最终的目标都是一个 方便项目管理 便于资源共享 nbsp nbsp nbsp 最近一个项目中 我在配置项中采取如下思想进行配置计划 nbsp nbsp nbsp 一级目录 以 trunk branches tags 为起点 考虑到配置管理工具 我们选择流行的 svn 基线 trun

    2025年7月2日
    1
  • 这2个PDF转Word免费不限页数工具很多人没用过

    这2个PDF转Word免费不限页数工具很多人没用过很多人在搜索下载过PDF转换器的小伙伴都会有一个灵魂拷问:难道就没有免费还没页数限制的PDF转Word的工具吗?小编经过不断的对比和试用,找到以下两款好用免费的工具,相信总有一个你能用上。一、PDF转换器相信了解PDF这种文档格式设计由来的人对于Adobe肯定不陌生,所以首先要说的PDF转换工具就是AdobePDF,下载安装后打开软件,直接将PDF拖到软件页面打开即可,然后点击左上角“文件”中的“另存为其他”,选择我们需要转换成的Word格式就可以了。或者点击右侧“工具”选项中的“将文件导出为”并

    2022年4月27日
    97

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号