<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.progressbar.MainActivity" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true" />
<ProgressBar
android:id="@+id/hor"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/progressBar3"
android:layout_marginTop="14dp"
android:max="100"
android:progress="50"
android:secondaryProgress="80" />
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/progressBar1"
android:layout_below="@+id/hor"
android:layout_marginTop="21dp"
android:text="@string/add" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_centerVertical="true"
android:text="@string/reduce" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_alignTop="@+id/text"
android:layout_marginTop="14dp"
android:text="@string/reset" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_below="@+id/button3"
android:layout_marginTop="14dp"
android:text="@string/show" />
<ProgressBar
android:id="@+id/progressBar3"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/progressBar2"
android:layout_alignRight="@+id/hor"
android:layout_marginRight="30dp" />
<ProgressBar
android:id="@+id/progressBar2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/progressBar1"
android:layout_toRightOf="@+id/text" />
</RelativeLayout>
package com.example.progressbar;
import android.R.integer;
import android.app.Activity;
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 Activity {
private ProgressBar progressBar;
private Button addButton;
private Button reduceButton;
private Button resetButton;
private TextView textView;
private ProgressDialog progressDialog;
private Button button;
@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(600);
progressBar=(ProgressBar) findViewById(R.id.hor);
addButton=(Button) findViewById(R.id.button1);
reduceButton=(Button) findViewById(R.id.button2);
resetButton=(Button) findViewById(R.id.button3);
textView=(TextView) findViewById(R.id.text);
button=(Button) findViewById(R.id.button4);
int first=progressBar.getProgress();
int second=progressBar.getSecondaryProgress();
int max=progressBar.getMax();
textView.setText("第一进度:"+(int)((first/(float)max)*100)+"\n"+"第二进度:"+(int)((second/(float)max)*100));
addButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
progressBar.incrementProgressBy(10);
progressBar.incrementSecondaryProgressBy(10);
textView.setText("第一进度:"+(int)((progressBar.getProgress()/(float)progressBar.getMax())*100)+"\n"+"第二进度:"+(int)((progressBar.getSecondaryProgress()/(float)progressBar.getMax())*100));
}
});
reduceButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
progressBar.incrementProgressBy(-10);
progressBar.incrementSecondaryProgressBy(-10);
textView.setText("第一进度:"+(int)((progressBar.getProgress()/(float)progressBar.getMax())*100)+"\n"+"第二进度:"+(int)((progressBar.getSecondaryProgress()/(float)progressBar.getMax())*100));
}
});
resetButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
progressBar.setProgress(50);
progressBar.setSecondaryProgress(80);
textView.setText("第一进度:"+(int)((progressBar.getProgress()/(float)progressBar.getMax())*100)+"\n"+"第二进度:"+(int)((progressBar.getSecondaryProgress()/(float)progressBar.getMax())*100));
}
});
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
progressDialog=new ProgressDialog(MainActivity.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setTitle("慕课网");
progressDialog.setMessage("欢迎大家支持慕课网!");
progressDialog.setIcon(R.drawable.ic_launcher);
progressDialog.setMax(100);
progressDialog.incrementProgressBy(50);
progressDialog.setIndeterminate(false);
progressDialog.setButton(DialogInterface.BUTTON_POSITIVE,"确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"欢迎大家支持慕课网",Toast.LENGTH_SHORT).show();
}
});
progressDialog.setCancelable(true);
progressDialog.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}