首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >文本出现在进度栏- Android之后。

文本出现在进度栏- Android之后。
EN

Stack Overflow用户
提问于 2014-03-10 10:58:24
回答 1查看 405关注 0票数 0

我正在使用android中的进度条,并希望在进度条运行后出现我的文本。但我的短信同时出现。显然是在错误的地方?但它应该在哪里呢?

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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;
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-10 12:26:15

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

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22308922

复制
相关文章

相似问题

添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文