文本出现在进度条后 - Android

时间:2014-03-10 18:58:24

标签: java android

我正在玩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;
    }

}

1 个答案:

答案 0 :(得分:1)

直接在实例化线程下的代码不会等待线程在执行之前完成其runnable。

相反,重新进入新线程runnable末尾的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();