单击后更改图像和文本

时间:2014-05-04 18:21:38

标签: java android image

大家好我正在研究简单的应用程序,它基本上有一个按钮功能,按下该功能后,显示进度条,然后切换图像。我有问题,这个代码在点击按钮后立即切换图像。我需要在进度条的末尾切换图像。

这是我到目前为止的代码:

package com.example.testapp;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

    ImageView image;
    Button btnStartProgress;
    ProgressDialog progressBar;
    private int progressBarStatus = 0;
    private Handler progressBarHandler = new Handler();

    private long fileSize = 0;

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

        addListenerOnButton();

    }

    public void addListenerOnButton() {

        image = (ImageView) findViewById(R.id.imageView1);
        btnStartProgress = (Button) findViewById(R.id.button1);
        btnStartProgress.setOnClickListener(
                 new OnClickListener() {

           @Override
           public void onClick(View v) {



            progressBar = new ProgressDialog(v.getContext());
            progressBar.setCancelable(true);
            progressBar.setMessage("Scanning ...");
            progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressBar.setProgress(0);
            progressBar.setMax(100);
            progressBar.show();


            progressBarStatus = 0;


            fileSize = 0;

            new Thread(new Runnable() {
              public void run() {

                while (progressBarStatus < 100) {


                  progressBarStatus = doSomeTasks();


                  try {
                    Thread.sleep(1000);
                  } catch (InterruptedException e) {
                    e.printStackTrace();
                  }

                  // Update the progress bar
                  progressBarHandler.post(new Runnable() {
                    public void run() {
                      TextView textView = (TextView) findViewById(R.id.textView1);
                      textView.setText("Completed!");
                      textView.setTextColor(0xFF00FF00);
                      image.setImageResource(R.drawable.yes_small);                     
                      progressBar.setProgress(progressBarStatus);
                    }
                  });
                }


                if (progressBarStatus >= 100) {

                                            try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }


                    progressBar.dismiss();

                }

              }
               }).start();

               }

                });

        }


    public int doSomeTasks() {

        while (fileSize <= 1000000) {

            fileSize++;

            if (fileSize == 100000) {
                return 10;
            } else if (fileSize == 200000) {
                return 20;
            } else if (fileSize == 300000) {
                return 30;
            } else if (fileSize == 400000) {
                return 40;
            } else if (fileSize == 500000) {
                return 50;
            } else if (fileSize == 600000) {
                return 60;
            } else if (fileSize == 700000) {
                return 70;
            } else if (fileSize == 800000) {
                return 80;
            } else if (fileSize == 900000) {
                return 90;
            }


        }

        return 100;

    }

}

2 个答案:

答案 0 :(得分:0)

我怀疑这个问题符合doSomeTasks()的逻辑。您正在寻找一些非常具体的尺寸。如果尺寸fileSizeif-else阶梯中的任何值不完全匹配,该怎么办?

尝试更改那里的逻辑,看看是否有所不同:)

答案 1 :(得分:0)

textView.set...代码移至if (progressBarStatus >= 100)块。您当前正在将文本更新为“已完成!”每次进度条更新为任何值。