Android入门之ProgressBar的使用教程

  package org.mk.android.demo.demoprogressbar;

  import androidx.appcompat.app.AppCompatActivity;

  import android.view.View;

  import android.os.Bundle;

  import android.widget.Button;

  import android.widget.ProgressBar;

  import android.widget.TextView;

  public class MainActivity extends AppCompatActivity {

  private ProgressBar btnProgressBar;

  private ProgressBar pgStyleHorizontal;

  private TextView txtPgValueShow;

  @Override

  protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

  txtPgValueShow = (TextView) findViewById(R.id.pgValueTextView);

  //横向进度条,点一下加5

  pgStyleHorizontal = (ProgressBar) findViewById(R.id.pgBarHorizontal);

  pgStyleHorizontal.setOnClickListener(new View.OnClickListener() {

  @Override

  public void onClick(View v) {

  switch (v.getId()) {

  case R.id.pgBarHorizontal:

  //每点击一次按钮就可以获取进度条当前的进度

  int progress = pgStyleHorizontal.getProgress();

  progress = progress + 5;

  pgStyleHorizontal.setProgress(progress);

  StringBuffer currentValue = new StringBuffer();

  currentValue.append("当前进度为: ").append(String.valueOf(progress));

  txtPgValueShow.setText(currentValue.toString());

  break;

  default:

  break;

  }

  }

  });

  //点一下显示点一下隐藏圆形进度条

  Button button = (Button) findViewById(R.id.displayBtn);

  btnProgressBar = (ProgressBar) findViewById(R.id.pgBar);

  button.setOnClickListener(new View.OnClickListener() {

  @Override

  public void onClick(View v) {

  switch (v.getId()) {

  case R.id.displayBtn:

  if (btnProgressBar.getVisibility() == View.GONE) {

  btnProgressBar.setVisibility(View.VISIBLE);

  } else {

  btnProgressBar.setVisibility(View.GONE);

  }

  break;

  default:

  break;

  }

  }

  });

  }

  }