gpt4 book ai didi

java - 文本出现在进度条之后 - Android

转载 作者:行者123 更新时间:2023-12-01 13:18:03 24 4
gpt4 key购买 nike

我正在玩弄 android 中的进度条,并希望我的文本在进度条运行后出现。但我的文字同时出现。明显是放错地方了?但它应该在哪里呢?

package com.example.bmi;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends Activity {
private ProgressBar progressBar;
private int progressStatus = 0;
private Handler handler = new Handler ();
//private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


}


public void calculateClickHandler(View view) {

if (view.getId() == R.id.button1) {

progressBar = (ProgressBar) findViewById(R.id.progressBar1);
//textView = (TextView) findViewById(R.id.textView1);
// Start long running operation in a background thread
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 1;
// Update the progress bar and display the
//current value in the text view
handler.post(new Runnable() {


public void run() {
progressBar.setProgress(progressStatus);
//textView.setText(progressStatus+"/"+progressBar.getMax());
}
});



try {
// Sleep for 200 milliseconds.
//Just to display the progress slowly
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}


}
}).start();

EditText weightText = (EditText) findViewById(R.id.editText1);
EditText heightText = (EditText) findViewById(R.id.editText2);
TextView result = (TextView) findViewById(R.id.textView4);

float weight = Float.parseFloat(weightText.getText().toString());
float height = Float.parseFloat(heightText.getText().toString());

float bmi = calcBMI(weight, height);
float roundedbmi = (float) (Math.round(bmi*100.0)/100.0);

String yoFat = fat(roundedbmi);

result.setText("Your BMI is: " + roundedbmi + "\n" + yoFat);


}

}


private float calcBMI(float weight, float height) {
// TODO Auto-generated method stub
return (float) (weight / (height * height));
}


private String fat (float bmi) {
if (bmi < 16) {
return "Yo Skinny ass needs some cake!";
}
else if (bmi < 18.5) {
return "Stop eating them salads!";
}
else if (bmi < 25) {
return "Grrrr, Just the way I like it!";
}
else if (bmi < 30) {
return "Put down that cupcake!";
}
else {
return "Ohh you Fat, You love the cake!";
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

最佳答案

实例化线程正下方的代码在执行之前不会等待线程完成其可运行状态。

相反,在新线程的可运行末尾重新进入 UI 线程并设置文本:

    new Thread(new Runnable() {
@Override
public void run() {
while (progressStatus < 100) {
// Progress update...
}

// Progress finished, re-enter UI thread and set text
handler.post(new Runnable() {
@Override
public void run() {
EditText weightText = (EditText) findViewById(R.id.editText1);
EditText heightText = (EditText) findViewById(R.id.editText2);
TextView result = (TextView) findViewById(R.id.textView4);

float weight = Float.parseFloat(weightText.getText().toString());
float height = Float.parseFloat(heightText.getText().toString());

float bmi = calcBMI(weight, height);
float roundedbmi = (float) (Math.round(bmi*100.0)/100.0);

String yoFat = fat(roundedbmi);

result.setText("Your BMI is: " + roundedbmi + "\n" + yoFat);
}
});
}
}).start();

关于java - 文本出现在进度条之后 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22308922/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com