这里为啥要用nit()方法写按钮的findViewById,而不是写进OnCreate方法里面??
package com.example.progressbar1015;
import android.app.Activity;
import android.os.Bundle;
import android.os.Process;
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;
public class MainActivity extends Activity implements OnClickListener {
private Button add;
private Button reduce;
private Button reset;
private TextView textView;
private ProgressBar progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 启用窗口特征,启用带进度和不带进度的进度条
requestWindowFeature(Window.FEATURE_PROGRESS);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
// 显示两种进度条
setProgressBarVisibility(true);
setProgressBarIndeterminate(false);
// 进度Max=10000;
setProgress(999);// 设置进度条的当前进度。
}
private void init() {
progress = (ProgressBar) findViewById(R.id.horiz);
add = (Button) findViewById(R.id.add);
reduce = (Button) findViewById(R.id.reduce);
reset = (Button) findViewById(R.id.reset);
textView = (TextView) findViewById(R.id.textView);
int first = progress.getProgress();
int second = progress.getSecondaryProgress();
// 获取进度条的最大长度
int max = progress.getMax();
textView.setText("第一进度条百分比:" + (int) (first / (float) max * 100) + "%"
+ " " + " " + "第二进度条百分比:"
+ (int) (second / (float) max * 100) + "%");
add.setOnClickListener(this);
reduce.setOnClickListener(this);
reset.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.add: {
progress.incrementProgressBy(10);
progress.incrementSecondaryProgressBy(10);
break;
}
case R.id.reduce: {
progress.incrementProgressBy(-10);
progress.incrementSecondaryProgressBy(-10);
break;
}
case R.id.reset: {
progress.setProgress(50);
progress.setSecondaryProgress(80);
}
}
textView.setText("第一进度条百分比:"
+ (int) (progress.getProgress() / (float) progress.getMax() * 100)
+ "%"
+ " "
+ " "
+ "第二进度条百分比:"
+ (int) (progress.getSecondaryProgress()
/ (float) progress.getMax() * 100) + "%");
}
}