android的进度条主要有两种,一种是长时间操作,无法确认时间长短的,我们叫它不确定进度条,一种是短时间操作,可以确定时间的,我们叫它确定进度条,
android开发团队为我们提供了两种风格的进度条:一种是水平式,另外一种是圆形的:
新建android项目,修改默认的activity_mian.xml文件的布局为:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/nosure" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/sure" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/nosure"/> </RelativeLayout>
修改MainActivity.java文件为:
package com.example.demo; import android.app.Activity; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.os.Message; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button bt1; private Button bt2; private ProgressDialog pd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt1=(Button) this.findViewById(R.id.nosure); bt2=(Button) this.findViewById(R.id.sure); bt1.setText("不确定时间"); bt2.setText("确定时间"); bt1.setOnClickListener(new OnClickListener() { @SuppressWarnings("deprecation") @Override public void onClick(View v) { // TODO Auto-generated method stub pd=new ProgressDialog(MainActivity.this); //设置进度条风格为圆形 pd.setProgressStyle(ProgressDialog.STYLE_SPINNER); pd.setTitle("这是标题"); pd.setMessage("圆形进度条"); pd.setIcon(android.R.drawable.alert_dark_frame); pd.setIndeterminate(true); //设置进度条的进度是否不确定 pd.setCancelable(true); pd.setCanceledOnTouchOutside(false); //点击对话框外面仍然可以关闭进度条对话框 pd.setButton("关闭", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub pd.dismiss(); } }); pd.show(); } }); bt2.setOnClickListener(new OnClickListener() { @SuppressWarnings("deprecation") @Override public void onClick(View v) { // TODO Auto-generated method stub pd=new ProgressDialog(MainActivity.this); //设置进度条风格为圆形 pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.setTitle("这是标题"); pd.setMessage("垂直进度条"); pd.setIcon(android.R.drawable.alert_dark_frame); pd.setIndeterminate(false); //设置进度条的进度是否不确定 pd.setMax(100); pd.setSecondaryProgress(50); pd.setCancelable(true); pd.setCanceledOnTouchOutside(true); pd.setButton("关闭", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub pd.dismiss(); } }); pd.show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
运行结果如下: