ITPub博客

首页 > 应用开发 > IT综合 > AlertDialog ProgressDialog

AlertDialog ProgressDialog

原创 IT综合 作者:Diy_os 时间:2016-05-22 01:16:06 0 删除 编辑

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

下面看下具体的代码:

  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:

  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中:

  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非常灵活多变,下面简单介绍一下使用水平进度条风格,也可以使用圆形:

  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开发文档。


-->

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29876893/viewspace-2104491/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 用通道抠图
下一篇: 端午杂谈
请登录后发表评论 登录
全部评论

注册时间:2014-12-11

最新文章