AlertDialog(对话框)详解

很多时候,我们在开发Android时,弹出几个对话框对于用户来说就比较容易了解这个App了。
现在开始总结一下,最近在做的模拟项目上用到的AlertDialog对话框,网上有很多的相关例子,一下这几种方法,只是在这是的项目里用到的,这个其实不难主要你要用到什么地方而已。

好啦!咱们接下来就来实现一下的几个简单的对话框吧!

最简单的对话框
如下运行效果:

第一步,activity_main布局文件中设置一个需要点击的控件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.li_mf.alertdialog.MainActivity">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Hello"
android:text="点击弹出对话框"
android:layout_centerHorizontal="true"
android:layout_marginTop="200dp"/>

</RelativeLayout>



首先在MainActivity.java里设置点击事件:
//点击事件点击Button弹出对话框
Hello.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showAlertDialog1();
}
});
下一步:
//简单的对话框
private void showAlertDialog1(){
new AlertDialog.Builder(this)
.setTitle("标题")
.setMessage("您已成功弹出对话框")
.setPositiveButton("确定", null)
.show();
}
加载对话框
运行效果:

1.圆形进度条对话框
代码:
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("正在加载中");
dialog.show();
2.水平进度条并显示当前进度,只需要把样式设置为 ProgressDialog.STYLE_HORIZONTAL即可。
运行效果:

//水平进度条对话框
private void showAlertDialog3(){
new AlertDialog.Builder(this);
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMessage("正在处理...");
dialog.setMax(100);
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int progress = 0;

@Override
public void run() {
dialog.setProgress(progress += 5);
if (progress == 100) {
timer.cancel();
}
}
}, 0, 1000);
dialog.show();

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值