AlertDialog ProgressDialog

首先看AlertDialog详细了解可以阅读官方文档。可以弹出一个对话框,可以设置三个Button,实现不同的业务逻辑,而且还可以设置View,如果你对所提供的样式不喜欢,完全可以自定义自己需要的布局,是不是很nice!

下面看下具体的代码:

[java] view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. new AlertDialog.Builder(MainActivity.this).setTitle("你好").setIcon(R.drawable.d_doge).setMessage("练习使用AlertDialog").setNegativeButton("你好"new DialogInterface.OnClickListener() {  
  2.                  @Override  
  3.                  public void onClick(DialogInterface dialogInterface, int i)  {  
  4.                      Toast.makeText(MainActivity.this,"你好",Toast.LENGTH_LONG).show();  
  5.                  }  
  6.              }).setPositiveButton(R.string.lios, new DialogInterface.OnClickListener() {  
  7.                  @Override  
  8.                  public void onClick(DialogInterface dialogInterface, int i) {  
  9.                      Toast.makeText(MainActivity.this,"app_name",Toast.LENGTH_LONG).show();  
  10.                  }  
  11.              }).setNeutralButton("one"new DialogInterface.OnClickListener() {  
  12.                  @Override  
  13.                  public void onClick(DialogInterface dialogInterface, int i) {  
  14.                      Toast.makeText(MainActivity.this,"one",Toast.LENGTH_LONG).show();  
  15.                  }  
  16.              }).show();  

使用起来很简单,下面看下如何在其中添加自己的View:

非常简单,首先自定义一个布局文件layout.xml:

[html] view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:id="@+id/line"  
  3.     android:layout_width="match_parent"  
  4.     android:orientation="vertical"  
  5.     android:gravity="center_horizontal"  
  6.     android:layout_height="match_parent">  
  7.  <TextView  
  8.      android:id="@+id/text"  
  9.      android:text="你好"  
  10.      android:layout_width="match_parent"  
  11.      android:layout_height="wrap_content" />  
  12.     <ImageView  
  13.         android:id="@+id/image"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:src="@drawable/d_doge"  
  17.         />  
  18. <!--LinearLayout>  
在代码中获得这个布局文件,然后添加到 AlertDialog 中:

[java] view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. View vie = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout,null);  
  2.                new AlertDialog.Builder(MainActivity.this).setView(vie).show();  

说到这里,发挥想象力,定制自己喜欢的style吧! AlertDialog说到这里,当然用法不仅仅这些。

ProgressDialog和上面的AlertDialog有点类似,但是ProgressDialog是进度条,说到这里,你会想到ProgressBar,

使用ProgressDialog非常灵活多变,下面简单介绍一下使用水平进度条风格,也可以使用圆形:

[java] view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);  
  2.              progressDialog.setMax(1000);  
  3.              //progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //可以设置水平进度条,默认圆形或者自定义  
  4.              progressDialog.setTitle("ProgressDialog");  
  5.              progressDialog.setMessage("正在下载");  
  6.              progressDialog.show();  


注意如果设置setCancelable(false),必须控制任务完成时,设置dismiss()取消,否则按back键都无法退出,注意!

下面通过模拟从网上下载任务,然后通过水平进度条显示下载进度:

点击(此处)折叠或打开

  1. Handler handler = new Handler(){
  2.     public void handleMessage(Message msg){
  3.      switch (msg.what){
  4.          case 1:
  5.              progressDialog.setProgress(progress);
  6.      }
  7.     }
  8. };
  9.   
  10.   progressDialog = new ProgressDialog(MainActivity.this);
  11.               progressDialog.setMessage("正在加载");
  12.               progressDialog.setTitle("你好");
  13.               progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  14.               progressDialog.show();
  15.               Message msg = new Message();
  16.                
  17.               for(int i =1;i<10000;i+=1){
  18.                   progress = i/100 +1;
  19.                   handler.sendEmptyMessageDelayed(1,1999);
  20.               }
 代码比较简单,这里就不贴出全部代码了。以上只是简单的使用这两种控件,更多请参考API开发文档。


-->

转载于:http://blog.itpub.net/29876893/viewspace-2104491/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值