Android开发求助:如何在ProgressBar完成后重新运行它?
解决方案
核心问题分析
你的代码存在两个关键问题导致无法重新运行ProgressBar:
- 进度变量
i在第一次运行后停留在100,再次点击按钮时while(i<100)条件不成立,线程循环直接跳过,ProgressBar无更新。 - 计算逻辑的多个
if条件重叠,会导致结果被错误覆盖(比如体重过轻的结果会被“正常体重”的语句覆盖)。
另外,你尝试用Intent跳转页面完全没必要,当前页面即可重置状态后重新执行计算和进度动画。
具体修复步骤
1. 点击按钮时重置进度状态
每次点击按钮时,先将进度相关变量和控件重置为初始状态:
- 重置
i为0 - 设置ProgressBar进度为0
- 重置提示文本
txt内容
2. 修复计算逻辑的条件判断
把多个独立的if改成else if,确保每个BMI区间只会触发一次判断,避免结果被覆盖。
3. 仅在输入验证通过后启动线程
将线程启动代码放到输入验证的else分支里,避免空输入时执行无效线程。
修改后的完整代码
private TextView textResultado; private TextView editpeso; private TextView editaltura; private Button botao; private ProgressBar pb; private TextView txt; int i = 0; Handler handler = new Handler(); String[] mensagens = {"Preencha todos os campos!"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getSupportActionBar().hide(); iniciar(); botao.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 重置进度相关状态 i = 0; pb.setProgress(0); txt.setText("0% Verificando..."); pb.setVisibility(View.VISIBLE); String peso = editpeso.getText().toString(); String altura = editaltura.getText().toString(); if (peso.isEmpty() || altura.isEmpty()) { Snackbar snackbar = Snackbar.make(view, mensagens[0], Snackbar.LENGTH_SHORT); snackbar.setBackgroundTint(getResources().getColor(R.color.white)); snackbar.setTextColor(getResources().getColor(R.color.black)); snackbar.show(); // 输入为空时终止后续逻辑 return; } new Thread(new Runnable() { @Override public void run() { while (i < 100) { i += 1; try { Thread.sleep(60); } catch (InterruptedException e) { e.printStackTrace(); } handler.post(new Runnable() { @Override public void run() { pb.setProgress(i); txt.setText(i + "% Verificando..."); if (i == pb.getMax()) { Toast.makeText(MainActivity.this, "Completo!", Toast.LENGTH_SHORT).show(); // 进度完成后再执行计算,符合加载感知 calculadoramm(view); } } }); } } }).start(); } }); } private void iniciar() { textResultado = findViewById(R.id.textResultado); editpeso = findViewById(R.id.editpeso); editaltura = findViewById(R.id.editaltura); botao = findViewById(R.id.botao); pb = findViewById(R.id.pb); txt = findViewById(R.id.txt); } public void calculadoramm(View view) { double peso = Double.parseDouble(editpeso.getText().toString()); double altura = Double.parseDouble(editaltura.getText().toString()); double resultado = peso / (altura * altura); // 使用else if确保每个区间只触发一次判断 if (resultado <= 18.5) { textResultado.setText("Abaixo do Peso!"); textResultado.setTextColor(getResources().getColor(android.R.color.holo_blue_bright)); } else if (resultado <= 24.9) { textResultado.setText("Peso normal!"); textResultado.setTextColor(getResources().getColor(android.R.color.holo_green_light)); } else if (resultado <= 29.9) { textResultado.setText("Excesso de peso!"); textResultado.setTextColor(getResources().getColor(android.R.color.holo_green_light)); } else if (resultado <= 34.9) { textResultado.setText("Obesidade nivel I!"); textResultado.setTextColor(getResources().getColor(android.R.color.holo_orange_light)); } else if (resultado <= 39.9) { textResultado.setText("Obesidade nivel II!"); textResultado.setTextColor(getResources().getColor(android.R.color.holo_red_light)); } else { textResultado.setText("Obesidade nivel III ou Morbida!"); textResultado.setTextColor(getResources().getColor(android.R.color.holo_red_light)); } }
额外优化说明
- 将计算逻辑放到ProgressBar完成后执行,符合用户对“加载中”的感知,避免进度未走完结果就出现的违和感。
- 添加
return语句,在输入为空时直接终止点击事件后续逻辑,避免无效线程启动。
内容的提问来源于stack exchange,提问作者Arlexx Junior
相关产品推荐
相关产品推荐

