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>
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>
相关文章推荐
- 【Android进度条】三种方式实现自定义圆形进度条ProgressBar
- 【Android进度条】三种方式实现自定义圆形进度条ProgressBar
- 三种方式实现自定义圆形进度条ProgressBar
- Android 基于帧布局实现一个进度条 FrameLayout+ProgressBar
- progressBar和Handler实现进度条的加载 和RatingBar评分控件
- BS网页中实现一个进度条progressBar
- android进度条(ProgressBar)实现动画效果
- BootStrap Progressbar 实现大文件上传的进度条
- 如何实现圆形的进度条(ProgressBar)
- Android 打造形形色色的进度条 实现可以如此简单 无需继承view,直接继承progressbar
- Android UI控件之ProgressBar进度条简单实现
- 使用progressbar实现进度条。
- 【Android进度条】三种方式实现自定义圆形进度条ProgressBar
- Android ProgressBar实现加载进度条
- MVC实现有关时间的进度条,使用jQuery ui的progressbar
- Handler和ProgressBar实现进度条的开始,暂停,停止,后退和循环
- 利用js简单实现进度条功能,替换jquery progressbar
- 【Android进度条】三种方式实现自定义圆形进度条ProgressBar
- Android三种方式实现ProgressBar自定义圆形进度条
- Android基础控件——ProgressBar自定义的介绍、动画效果实现、附加三个漂亮的进度条