Android UI 之 ProgressBar、ProgressDialog、AlertDialog
参考:
http://www.cnblogs.com/plokmju/p/android_ProgressBar.html
http://www.oschina.net/question/157182_37992
《第一行代码》P100
一、ProgressBar
ProgressBar: 进度条
内置的进度条样式有两种写法,以水平进度条为例:
style="?android:attr/progressBarStyleHorizontal" style="@android:style/Widget.ProgressBar.Horizontal"
效果如下:
常用属性:
android:max:设置进度的最大值。 android:progress:设置当前第一进度值。 android:secondaryProgress:设置当前第二进度值。 android:visibility:设置是否显示,默认显示。visible可见,invisible不可见但占据屏幕空间,gone不可见且不占用屏幕空间
设置第二进度后的效果如下:
动态控制进度条:
setProgress(int):设置当前进度值 incrementProgressBy(int):设置当前进度值的增量 setSecondaryProgress(int):与上类似 incrementSecondaryProgressBy(int):与上类似
设置可见性:
setVisibility(),一共三个参数,View.VISIBLE, View.INVISIBLE, View.GONE。对应XML的三个属性
其他样式都是圆形动画,无法控制进度。样式大同小异,如下:
二、ProgressDialog
简易用法:
private ProgressDialog progressDialog; private void showProgressDialog() { if (progressDialog == null) { progressDialog = new ProgressDialog(context); progressDialog.setMessage("正在加载"); progressDialog.setCanceledOnTouchOutside(false); } progressDialog.show(); } private void closeProgressDialog() { if (progressDialog != null) { progressDialog.dismiss(); } }
三、AlertDialog
简易用法:
/** 创建AlertDialog并显示 **/ private void showAlertDialog() { // 下面的设置项都是可选的 AlertDialog.Builder alertD = new AlertDialog.Builder(this) .setTitle("这里是标题") .setIcon(R.drawable.ic_launcher) .setMessage("这里是提示信息") .setView(myLayout) // 要在AlertDialog中显示的自定义View对象 .setCancelable(false) // 不能按手机的返回键取消 .setPositiveButton("PositiveButton显示进度值", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // do something... } }) .setNegativeButton("NegativeButton取消", null); alertD.create(); // This allows the user to do any extra processing before displaying the dialog. // Use show() if you don't have any other processing to do and want this to be created and displayed. alertD.show(); }
四、Demo
进度条相关的ProgressDialog、AlertDialog的Demo
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="圆进度条@android:style/Widget.ProgressBar" /> <ProgressBar android:layout_width="match_parent" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="圆进度条Style:?android:attr/progressBarStyle" /> <ProgressBar android:layout_width="match_parent" android:layout_height="wrap_content" style="?android:attr/progressBarStyle" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="长进度条@android:style/Widget.ProgressBar.Horizontal" /> <ProgressBar android:id="@+id/progressbar_horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Horizontal" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/button_Add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+" /> <Button android:id="@+id/button_Reduce" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" android:layout_marginLeft="30dp" /> <Button android:id="@+id/button_visible" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显/隐进度条" android:layout_marginLeft="30dp" /> </LinearLayout> <Button android:id="@+id/button_pd_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="圆形进度条的ProgressDialog" /> <Button android:id="@+id/button_ad_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="长方形进度条的AlertDialog" /> </LinearLayout>
my_view.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:gravity="center_horizontal" > <Button android:id="@+id/myView_reduce" android:layout_width="50dp" android:layout_height="wrap_content" android:text="-" /> <ProgressBar android:id="@+id/myView_progressBar" android:layout_width="178dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" style="@android:style/Widget.ProgressBar.Horizontal" android:progress="57" /> <Button android:id="@+id/myView_add" android:layout_height="wrap_content" android:layout_width="50dp" android:text="+" /> </LinearLayout>
MainActivity.java:
package com.example.test; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener{ private Button btnAdd, btnReduce, btnVisible, btnPb1, btnAd1, myUp, myDown; private ProgressBar pbHor, myPb; private AlertDialog.Builder alertD; private LayoutInflater layoutInflater; private LinearLayout myLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setProgressBarVisibility(true); setContentView(R.layout.activity_main); btnAdd = (Button) findViewById(R.id.button_Add); btnReduce = (Button) findViewById(R.id.button_Reduce); btnVisible = (Button) findViewById(R.id.button_visible); btnPb1 = (Button) findViewById(R.id.button_pd_1); btnAd1 = (Button) findViewById(R.id.button_ad_1); pbHor = (ProgressBar) findViewById(R.id.progressbar_horizontal); btnAdd.setOnClickListener(this); btnReduce.setOnClickListener(this); btnVisible.setOnClickListener(this); btnPb1.setOnClickListener(this); btnAd1.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button_Add: if (pbHor.getProgress() <= 90) { pbHor.setProgress(pbHor.getProgress() + 10); } break; case R.id.button_Reduce: if (pbHor.getProgress() >= 10) { pbHor.setProgress(pbHor.getProgress() - 10); } break; case R.id.button_visible: if (pbHor.getVisibility() == View.VISIBLE) { pbHor.setVisibility(View.GONE); } else { pbHor.setVisibility(View.VISIBLE); } break; case R.id.button_pd_1: // 创建一个ProgressDialog,不能写成如下一大串 ProgressDialog mypDialog = new ProgressDialog(this); // 默认为圆形风格,STYLE_HORIZONTAL则为长方形风格 // mypDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // 标题 mypDialog.setTitle("KingLearnJava"); // 提示信息 mypDialog.setMessage(getResources().getString(R.string.pd_message)); // 标题图标 mypDialog.setIcon(R.drawable.ic_launcher); // 第一个按钮 mypDialog.setButton("第一个按钮", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // 第二个按钮 mypDialog.setButton2("第二个按钮", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // 进度条是否不明确 mypDialog.setIndeterminate(false); // 是否可以按返回键取消 mypDialog.setCancelable(true); // 显示 mypDialog.show(); break; case R.id.button_ad_1: // 这里用另一种方法来载入布局 // 获得系统的布局过滤服务 layoutInflater = (LayoutInflater) getSystemService(this.LAYOUT_INFLATER_SERVICE); // 得到事先设计好的布局 myLayout = (LinearLayout) layoutInflater.inflate(R.layout.my_view, null); // 获得自定义View里的控件 myUp = (Button) myLayout.findViewById(R.id.myView_add); myDown = (Button) myLayout.findViewById(R.id.myView_reduce); myPb = (ProgressBar) myLayout.findViewById(R.id.myView_progressBar); myUp.setOnClickListener(this); myDown.setOnClickListener(this); // 设置初始进度值 myPb.setProgress(50); alertD = new AlertDialog.Builder(this) .setTitle("这里是标题") .setIcon(R.drawable.ic_launcher) .setMessage("这里是提示信息") .setView(myLayout) .setCancelable(false) // 不能按手机的返回键取消 .setPositiveButton("PositiveButton显示进度值", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "现在的进度值是" + myPb.getProgress() , Toast.LENGTH_LONG).show(); } }) .setNegativeButton("NegativeButton取消", null); alertD.show(); break; case R.id.myView_add: myPb.incrementProgressBy(1); break; case R.id.myView_reduce: myPb.incrementProgressBy(-1); break; default: break; } } }
版权声明:本文为博主原创文章,未经博主允许不得转载。