您的位置:首页 > 其它

ProgressBar进度条的实现

2016-07-31 21:48 204 查看
各式各样的进度条,这里不演示效果了,大家可以自己写写,动手操作操作,看看效果

MainActivty代码:

package com.example.sixth_progressbar_android;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener{
private ProgressBar press;
private Button add;
private Button reduce;
private Button reset;
private Button show;
private ProgressDialog progressdialog;
private TextView text2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//启用窗口特征,显示带进度和不带进度的进度条
requestWindowFeature(Window.FEATURE_PROGRESS);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

setContentView(R.layout.activity_main);

//带进度进度条 与 不带进度进度条
setProgressBarVisibility(true);

setProgressBarIndeterminateVisibility(true);

setProgress(5000);

intt();

}

private void intt() {
press = (ProgressBar)findViewById(R.id.horiz);
add = (Button) findViewById(R.id.add);
reduce = (Button)findViewById(R.id.reduce);
reset = (Button) findViewById(R.id.reset);
text2 = (TextView)findViewById(R.id.text1);
show = (Button)findViewById(R.id.show);

//获取第一进度
int first = press.getProgress();

//获得第二进度
int second = press.getSecondaryProgress();

//获取最大进度
int max = press.getMax();

text2.setText("第一进度:"+(int)(first/(float)max*100) +"% 第二进度:"+(int)(second/(float)max*100) +"%");

//设置监听器
add.setOnClickListener(this);
reset.setOnClickListener(this);
reduce.setOnClickListener(this);
show.setOnClickListener(this);
}

@Override
public void onClick(View view) {
switch(view.getId())
{
case R.id.add: {
//增加第一进度和第二进度10%
press.incrementProgressBy(10);
press.incrementSecondaryProgressBy(10);

break;
}
case R.id.reduce: {
//减少第一进度和第二进度10%
press.incrementProgressBy(-20);
press.incrementSecondaryProgressBy(-20);

break;
}
case R.id.reset: {
//重置
press.setProgress(50);
press.setSecondaryProgress(80);

break;
}
case R.id.show :
{
/*
*页面显示设置
*/
//页面设置,,新建一个ProgressDialog
progressdialog = new ProgressDialog(MainActivity.this);
//设置显示风格
progressdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//设置标题
progressdialog.setTitle("模拟器");
//设置文本框内容
progressdialog.setMessage("欢迎进入");
//设置图标
progressdialog.setIcon(R.mipmap.ic_launcher);

/*
*关于progressdialog一些属性设置
*/
//设定最大进度
progressdialog.setMax(100);
//初始化进度
progressdialog.incrementProgressBy(50);
//进度条是明确清晰可见
progressdialog.setIndeterminate(false);

/*
*设置一个确定按钮
*/

progressdialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

Toast.makeText(MainActivity.this,"退出窗口",Toast.LENGTH_SHORT).show();

}
});
//是否可以通过返回按钮退出对话框
progressdialog.setCancelable(true);

//显示progressdialog
progressdialog.show();

break;
}

}

text2.setText("第一进度:"+(int)(press.getProgress()/(float)press.getMax()*100)+"% " +
" 第二进度:"+(int)(press.getSecondaryProgress()/(float)press.getMax()*100)+"%");
}
}
xml文件代码:
<?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"
tools:context="com.example.sixth_progressbar_android.MainActivity">

<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar2"
android:layout_below="@+id/progressBar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="35dp" />

<ProgressBar
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar3"
android:layout_below="@+id/progressBar2"
android:layout_marginTop="35dp" />

<ProgressBar
android:progress="80"
android:secondaryProgress="50"
android:max="100"
style="@android:style/Widget.Holo.Light.ProgressBar.Horizontal"
android:progressDrawable="@drawable/progress_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/horiz"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/add"
android:text="增加"
android:layout_below="@+id/horiz" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/reduce"
android:text="减少"
android:layout_below="@+id/add" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/reset"
android:text="重置"
android:layout_below="@+id/reduce" />
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@+id/reset" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示对话框进度条"
android:id="@+id/show"
android:layout_below="@+id/text1"/>

</RelativeLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: