Android UI 之 ProgressBar、ProgressDialog、AlertDialog
2015-06-27 17:00
441 查看
参考:
http://www.cnblogs.com/plokmju/p/android_ProgressBar.html http://www.oschina.net/question/157182_37992
《第一行代码》P100
内置的进度条样式有两种写法,以水平进度条为例:
效果如下:
![](http://img.blog.csdn.net/20150629152724603?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2luZ2xlYXJuamF2YQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
常用属性:
设置第二进度后的效果如下:
![](http://img.blog.csdn.net/20150629153552560?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2luZ2xlYXJuamF2YQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
动态控制进度条:
设置可见性:
其他样式都是圆形动画,无法控制进度。样式大同小异,如下:
![](http://img.blog.csdn.net/20150629154550621?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2luZ2xlYXJuamF2YQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
my_view.xml:
MainActivity.java:
效果如图:
![](http://img.blog.csdn.net/20150707155130267?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20150707155145582?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
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; } } }
效果如图:
相关文章推荐
- valueForKeyPath 方法的使用
- PowerBuilder9.0自动检测注册ASA8.0
- 下载:Windows 10 Build 10154默认新壁纸
- Builder模式在Java中的应用(转)
- UIImageView
- UIButton 导航栏属性
- UIView的layoutSubviews和drawRect方法何时调用
- 如何自己编写一个easyui插件续
- UIImageView UIButton之间的区别
- xcode7 UI 测试
- 求出最长非降子序列的长度--LIS:longest increasing subsequence
- UIView的属性方法解释(全)
- iOS开发之使用UIView-Positioning简化页面布局
- 【后缀自动机】 CodeForces 235C Cyclical Quest
- AndroidのBuild工具之Ant动手实践
- AndroidのUI体验之上拉下拉
- AndroidのUI体验之ImmersiveMode沉浸模式
- AndroidのUI布局之layout weight
- STM32F030系列UID
- iOS开发之使用UIView-Positioning简化页面布局