标签:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ProgressBar android:id="@+id/progressBar1" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ProgressBar android:id="@+id/progressBar2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ProgressBar android:id="@+id/progressBar3" style="?android:attr/progressBarStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ProgressBar android:max="100" android:progress="50" android:secondaryProgress="80" android:id="@+id/horizi" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="增加" /> <Button android:id="@+id/reduce" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="减少" /> <Button android:id="@+id/reset" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="重置" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> <Button android:id="@+id/show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示进度条对话框" /> </LinearLayout>
MainActivity.java
package com.example.hello; import android.support.v7.app.ActionBarActivity; import android.app.Dialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends ActionBarActivity implements OnClickListener{ private ProgressBar progress; private Button add; private Button reduce; private Button reset; private TextView text; private ProgressDialog prodialog; private Button show; @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); //Max = 10000 setProgress(6590); init(); } private void init() { progress = (ProgressBar) findViewById(R.id.horizi); add = (Button) findViewById(R.id.add); reduce = (Button) findViewById(R.id.reduce); reset = (Button) findViewById(R.id.reset); text = (TextView) findViewById(R.id.text); show = (Button) findViewById(R.id.show); //获取第一进度条的进度 int first = progress.getProgress(); //获取第二进度条的进度 int second = progress.getSecondaryProgress(); //获取最大进度条的进度 int max = progress.getMax(); text.setText("第一进度条的百分比:"+(int)first/(float)max*100+"% 第一进度条的百分比:"+(int)second/(float)max*100+"%"); add.setOnClickListener(this); reduce.setOnClickListener(this); reset.setOnClickListener(this); show.setOnClickListener(this); } /* (non-Javadoc) * @see android.view.View.OnClickListener#onClick(android.view.View) */ @Override public void onClick(View v) { switch(v.getId()){ case R.id.add: //增加第一进度和第二进度10个刻度 progress.incrementProgressBy(10); progress.incrementSecondaryProgressBy(10); break; case R.id.reduce: //减少第一进度和第二进度10个刻度 progress.incrementProgressBy(-10); progress.incrementSecondaryProgressBy(-10); break; case R.id.reset: progress.setProgress(50); progress.setSecondaryProgress(80); break; case R.id.show: //新建对象 prodialog = new ProgressDialog(this); //设置显示风格 prodialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //设置标题 prodialog.setTitle("慕课网"); //设置对话框的内容 prodialog.setMessage("欢迎大家支持慕课网"); //设置图标 prodialog.setIcon(R.drawable.ic_launcher); //设置最大进度 prodialog.setMax(100); prodialog.incrementProgressBy(50); prodialog.setIndeterminate(false); prodialog.setButton(DialogInterface.BUTTON_POSITIVE, "SURE",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "欢迎大家支持慕课网", Toast.LENGTH_SHORT); } }); //是否可以通过返回按钮退出对话框 prodialog.setCancelable(true); //显示 prodialog.show(); break; } text.setText("第一进度条的百分比:"+(int)(progress.getProgress()/(float)progress.getMax()*100)+"% 第一进度条的百分比:"+(int)(progress.getSecondaryProgress()/(float)progress.getMax()*100)+"%"); } }
标签:
原文地址:http://www.cnblogs.com/LoveJulin/p/4990539.html