首先看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>
- 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开发文档。
转载于:http://blog.itpub.net/29876893/viewspace-2104491/