Android入门笔记 - 界面开发 - ProgressDialog

ProgressDialog 就是一个对话框,然后里面有一个Progress,ProgressDilaog来自于AlertDialog,为了方便开发,android自己将Progress设计进去了。

为了方便小伙伴们查看,现在起每写一个效果,都会把图贴上大笑

    

好了,来看代码:

1. res/layout/activity_main.xml

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请点击以下按钮,弹出对话框" />
    
    <Button 
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="圆形进度条"/>
    
    <Button 
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="水平进度条"/>
    

</LinearLayout>
2. src/MainActivity.java

package com.example.demo_ui6_progressdialog;

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
	private Context ctx = this;
	private ProgressDialog mPD;
	private Button btn1;
	private Button btn2;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn1 = (Button) findViewById(R.id.btn1);
		btn2 = (Button) findViewById(R.id.btn2);
		btn1.setOnClickListener(btn1Listener);
		btn2.setOnClickListener(btn2Listener);
		
	}
	
	private OnClickListener btn1Listener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			mPD = new ProgressDialog(ctx);
			mPD.setTitle("圆形进度条Dialog");
			mPD.setMessage("通知消息");
			mPD.setIcon(R.drawable.ic_launcher);
			mPD.setCancelable(true);
			mPD.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			mPD.setIndeterminate(false);
			mPD.setButton("关闭", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					mPD.dismiss();
				}
			});
			mPD.show();
		}
	};
	
	private OnClickListener btn2Listener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			mPD = new ProgressDialog(ctx);
			mPD.setTitle("水平进度条Dialog");
			mPD.setMessage("通知消息");
			mPD.setIcon(R.drawable.ic_launcher);
			mPD.setCancelable(true);
			mPD.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			mPD.setButton("关闭", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					mPD.dismiss();
				}
			});
			mPD.show();
			
			Thread tread = new Thread(){
				public void run(){
					int count = 0;
					try{
						while( count <= 100 ){
							count++;
							mPD.setProgress(count);
							sleep(500);
						}
						mPD.dismiss();
					}catch(Exception ex){
						ex.printStackTrace();
					}
				}
			};
			tread.start();
		}
	};

	@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;
	}

}


结合代码我们看到了两个button,点击之后会启动不同的进度条对话框,样式不同,一个是水平的,一个是圆形的,通过mPD.setProgressStyle(ProgressDialog. ...)来设置。

然后其中还有一些setIcon(), setTitle(), setMessage() ...  就和AlertDialog作用是相同的,这里就不一一说明了额。


你可能感兴趣的:(android,progress,ProgressDialog)