Progress进度条的实现
扫描二维码
随时随地手机看文章
package com.example.administrator.progressbar; import android.app.ActionBar; import android.app.Activity; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.util.Property; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.ProgressBar; import android.widget.Toast; public class MainActivity extends Activity implements Button.OnClickListener{ private ProgressBar progressBar; private Button btn_add; private Button btn_sub; private Button btn_reset; private Button btn_dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /** * actionBar 进度条 */ // 1、设置窗口特征 requestWindowFeature(Window.FEATURE_PROGRESS); //进度条 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); //loading setContentView(R.layout.activity_main); // 2、显示进度条 setProgressBarVisibility(true); // 2、显示loading setProgressBarIndeterminateVisibility(true); setProgress(Window.PROGRESS_END / 2); //进度条长度为50% 最大为 10000 /** * 进度条组件 */ // 1、 progressBar = (ProgressBar)findViewById(R.id.progressBar4); btn_add = (Button)findViewById(R.id.btn_add); btn_sub = (Button)findViewById(R.id.btn_sub); btn_reset = (Button)findViewById(R.id.btn_reset); btn_add.setOnClickListener(this); btn_sub.setOnClickListener(this); btn_reset.setOnClickListener(this); /** * 进度条对话框 */ btn_dialog = (Button)findViewById(R.id.btn_dialog); btn_dialog.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ProgressDialog progressBar_dialog = new ProgressDialog(MainActivity.this); progressBar_dialog.setTitle("进度条对话框"); progressBar_dialog.setMessage("进度条对话框描述"); progressBar_dialog.setIcon(R.mipmap.ic_launcher); progressBar_dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //这里不使用 setProgress、setSecondaryProgress 因为只会改变进度条 不会改变进度条下面的百分比文字 progressBar_dialog.incrementProgressBy(50); progressBar_dialog.incrementSecondaryProgressBy(80); progressBar_dialog.setMax(100); progressBar_dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "点击了确定", Toast.LENGTH_SHORT).show(); } }); //不允许通过返回键关闭dialog 默认为 true progressBar_dialog.setCancelable(false); progressBar_dialog.show(); } }); } @Override public void onClick(View v) { int _progress = progressBar.getProgress(); int _second_progress = progressBar.getSecondaryProgress(); switch (v.getId()){ case R.id.btn_add: progressBar.incrementProgressBy(10); progressBar.incrementSecondaryProgressBy(10); break; case R.id.btn_sub: progressBar.setProgress(_progress - 10); progressBar.setSecondaryProgress(_second_progress - 10); break; case R.id.btn_reset: progressBar.setProgress(50); progressBar.setSecondaryProgress(80); break; } } }
ActivityMain.xml
Progress_style.xml