AlertDialog ProgressDialog

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

下面看下具体的代码:

   new AlertDialog.Builder(MainActivity.this).setTitle("你好").setIcon(R.drawable.d_doge).setMessage("练习使用AlertDialog").setNegativeButton("你好", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i)  {
                        Toast.makeText(MainActivity.this,"你好",Toast.LENGTH_LONG).show();
                    }
                }).setPositiveButton(R.string.lios, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,"app_name",Toast.LENGTH_LONG).show();
                    }
                }).setNeutralButton("one", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,"one",Toast.LENGTH_LONG).show();
                    }
                }).show();

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

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/line"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_height="match_parent">
 <TextView
     android:id="@+id/text"
     android:text="你好"
     android:layout_width="match_parent"
     android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/d_doge"
        />
</LinearLayout>
在代码中获得这个布局文件,然后添加到 AlertDialog 中:

 View vie = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout,null);
                new AlertDialog.Builder(MainActivity.this).setView(vie).show();

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


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

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

   ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
                progressDialog.setMax(1000);
                //progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //可以设置水平进度条,默认圆形或者自定义
                progressDialog.setTitle("ProgressDialog");
                progressDialog.setMessage("正在下载");
                progressDialog.show();

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

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

Handler handler = new Handler(){
    public void handleMessage(Message msg){
     switch (msg.what){
         case  1:
             progressDialog.setProgress(progress);
     }
    }
};

  progressDialog = new ProgressDialog(MainActivity.this);
              progressDialog.setMessage("正在加载");
              progressDialog.setTitle("你好");
              progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
              progressDialog.show();
              Message msg = new Message();
             
              for(int i =1;i<10000;i+=1){
                  progress = i/100 +1;
                  handler.sendEmptyMessageDelayed(1,1999);
              }

代码比较简单,这里就不贴出全部代码了。以上只是简单的使用这两种控件,更多请参考API开发文档。




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值