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;
}
}
}

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • AD15中PCB设置NET CLASS规则

    AD15中PCB设置NET CLASS规则1,执行菜单命令设计-类或者快捷命令DC计入如下界面2,右键新建一个类类)X*O-K,M9}0C6v3,吧电源和GND网络都加载到右边,PWR类创建完毕 4,这时候在规则里面就可对这个类进行调用和约束…

  • 在线navicat16 激活码 键[在线序列号]

    在线navicat16 激活码 键[在线序列号],https://javaforall.cn/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

  • pycharm配置svn有什么用_SVN安装配置

    pycharm配置svn有什么用_SVN安装配置PyCharm是一款非常优秀的PythonIDE,以前用Editplus,用惯了感觉还行。用了PyCharm后被它丰富的功能吸引了。无论是普通python脚本、Django框架项目、还是GoogleAppEngine项目,它都能完美运行。不过设置起来比较麻烦,比如Subversion的用法我就一直没参透,我总是写完代码后出去用小乌龟提交。今天google一下,终于搞定了。现在写完代码后直接在…

  • Ubuntu16.04安装_vs安装路径

    Ubuntu16.04安装_vs安装路径TableofContents一、前言二、安装过程1、下载VSCode2、安装过程3、下载C++模块4、汉化5、常用快捷键一、前言因为要用到在ubuntu系统中使用VSCode来编写C++代码,在此分享VSCode的安装过程。之前我们讲了如何制作U盘启动盘,如何安装双系统,如何安装谷歌浏览器等,如果不了解的同学请看我的分类[操作系统]:操…

  • npm设置淘宝镜像源「建议收藏」

    npmconfigsetregistryhttps://registry.npm.taobao.org/

  • 使用@Profiled注解或自定义AOP拦截打印日志json序列化失败

    使用@Profiled注解或自定义AOP拦截打印日志json序列化失败项目中使用@Profiled注解方式进行统一日志打印输出fastjson踩坑记录一下1:@Profiled注解方式如上图:方法上使用注解@Profiled,因为我的入参有HttpServletResponse,日志打印时会对所有入参进行序列化操作,所对以HttpServletResponse进行JSON.toJSONString()转换会抛出以上异常,此时要么干掉HttpServletResponse,或者换一种方式手动注入HttpServletResponse即可解决以上异常,如下图:

发表回复

您的电子邮箱地址不会被公开。

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