本文主要是介绍2.14 ProgressDialog 进度条对话框的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、布局文件
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.yuancan.test102301.MainActivity"><Button
android:id="@+id/btn_1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="1、普通的圆形进度条对话框"android:layout_marginTop="20dp"/><Button
android:id="@+id/btn_2"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="2、条形进度条"android:layout_marginTop="70dp"/><Button
android:id="@+id/btn_3"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="120dp"android:text="3、更新条形进度条"/></RelativeLayout>
二、Java代码
package com.example.yuancan.test102301;import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;public class MainActivity extends AppCompatActivity implements View.OnClickListener {private Button btn_one;private Button btn_two;private Button btn_three;private ProgressDialog pd1;private ProgressDialog pd2;private final static int MAXVALUE = 100;private int progressStart = 0;private int add = 0;private Context mContext;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mContext = MainActivity.this;btn_one = (Button)findViewById(R.id.btn_1);btn_two = (Button)findViewById(R.id.btn_2);btn_three = (Button)findViewById(R.id.btn_3);btn_one.setOnClickListener(this);btn_two.setOnClickListener(this);btn_three.setOnClickListener(this);}final Handler hand = new Handler() {@Overridepublic void handleMessage(Message msg) {if(msg.what == 123) {pd2.setProgress(progressStart);}if (progressStart >= MAXVALUE) {pd2.dismiss();}}};@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn_1:ProgressDialog.show(MainActivity.this,"资源加载中","资源加载中,请稍后...",false,true);break;case R.id.btn_2:pd1 = new ProgressDialog(mContext);pd1.setTitle("软件更新中");pd1.setMessage("软件正在更新,请稍后...");pd1.setCancelable(true);pd1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);pd1.setIndeterminate(true);pd1.show();break;case R.id.btn_3:progressStart = 0;add = 0;pd2 = new ProgressDialog(MainActivity.this);pd2.setMax(MAXVALUE);pd2.setTitle("文件读取中");pd2.setMessage("文件加载中,请稍后...");pd2.setCancelable(false);pd2.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);pd2.setIndeterminate(false);pd2.show();new Thread() {public void run() {while (progressStart < MAXVALUE) {progressStart = 2 * usetime();hand.sendEmptyMessage(123);}}}.start();break;}}public int usetime() {add++;try {Thread.sleep(100);}catch (InterruptedException e) {e.printStackTrace();}return add;}
}
这篇关于2.14 ProgressDialog 进度条对话框的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!