Python中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在单击后更改图像和文本

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

这是我目前掌握的代码:

package com.example.testapp;

import 安卓.app.Activity;
import 安卓.app.ProgressDialog;
import 安卓.os.Bundle;
import 安卓.os.Handler;
import 安卓.widget.Button;
import 安卓.widget.ImageView;
import 安卓.widget.TextView;
import 安卓.view.View;
import 安卓.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;

    }

}

共 (0) 个答案