.java代码:
package com.example.day1;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
public class progress extends Activity implements OnClickListener{
private Button one;
private Button two;
private ProgressBar progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress);
one=(Button) findViewById(R.id.one);
two=(Button) findViewById(R.id.two);
progress=(ProgressBar) findViewById(R.id.progress);
//点击事件
one.setOnClickListener(this);
two.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.one:
int currentprogress1=progress.getProgress(); // 获取当前进度值
int prog1=(int) (currentprogress1*1.2); // 设置增加的进度值
progress.setProgress(prog1);
break;
case R.id.two:
int currentprogress2=progress.getProgress();
int prog2=(int) (currentprogress2*0.5);
progress.setProgress(prog2); // 设置减少的进度值
break;
}
}
}
.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:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleSmall"
/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleLarge"/>
<ProgressBar
android:id="@+id/progress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="30"
android:layout_marginTop="50dip"
style="?android:attr/progressBarStyleHorizontal"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/one"
android:text="增加"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/two"
android:text="减少"/>
</LinearLayout>
</LinearLayout>