2017.10.04
今天学习了ProgressBar和ProgressDialog
//增加的进度条
incrementProgressBy
//设置确定按钮的属性
dialog.setButton
//是否可以通过返回按钮返回对话框
dialog.setCancelable(true);
public class MainActivity extends AppCompatActivity implements View.OnClickListener,DialogInterface.OnClickListener{
private Button add;
private Button reduce;
private Button reset;
private Button hty;
private TextView textView;
private ProgressBar progressBar;
private ProgressDialog dialog;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//启用窗口特征,启用带进度和不带进度的进度条
setContentView(R.layout.activity_main);
init();
}
private void init() {
add = (Button) findViewById(R.id.button);
reduce = (Button) findViewById(R.id.button2);
reset = (Button) findViewById(R.id.button3);
hty= (Button) findViewById(R.id.button4);
textView = (TextView) findViewById(R.id.textView);
progressBar= (ProgressBar) findViewById(R.id.progressBar2);
//获取第一进度条的进度
int first = progressBar.getProgress();
//获取第二进度条的进度
int second = progressBar.getSecondaryProgress();
//获取最大进度
int max=progressBar.getMax();
textView.setText("第一进度条"+(int)(first/(float)max*100)+"%"+"第二进度条"+(int)(second/(float)max*100)+"%");
add.setOnClickListener(this);
reduce.setOnClickListener(this);
reset.setOnClickListener(this);
hty.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.button: {
//增加进度条
progressBar.incrementProgressBy(10);
progressBar.incrementSecondaryProgressBy(10);
break;
}
case R.id.button2: {
//减少进度条
progressBar.incrementProgressBy(-10);
progressBar.incrementSecondaryProgressBy(-10);
break;
}
case R.id.button3: {
//设置进度条的进度
progressBar.setProgress(50);
progressBar.setSecondaryProgress(70);
break;
}
case R.id.button4:{
//新建一个Dialog对象
dialog=new ProgressDialog(MainActivity.this);
//设置显示风格
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//设置标题栏
dialog.setTitle("欢迎皇天雨大魔王");
//设置文字信息
dialog.setMessage("6666666666");
//设置图标
dialog.setIcon(R.mipmap.ic_launcher);
/**
* 设置dialog的一些基础属性
*/
//设置最大值
dialog.setMax(100);
//设置已经增长到的进度
dialog.incrementSecondaryProgressBy(50);
//进度条是明显显示进度的
dialog.setIndeterminate(false);
//设置确定按钮的属性
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this,"我是你大爷",Toast.LENGTH_SHORT).show();
}
});
//是否可以通过返回按钮返回对话框
dialog.setCancelable(true);
//显示对话框
dialog.show();
break;
}
}
textView.setText("第一进度条"+(int)(progressBar.getProgress()/(float)progressBar.getMax()*100)+"%"+"第二进度条"+(int)(progressBar.getSecondaryProgress()/(float)progressBar.getMax()*100)+"%");
}
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
}