进度条继续运行

时间:2013-03-11 11:23:38

标签: android

我用进度条写了game play screen。我只希望进度条运行4秒,但它不会停止。

我用MainActivity写了一个gamelayout课程。在此布局中,我想添加progress bar

为什么进度条会继续运行?

public class MainActivity extends Activity {
private static final String TAG = null;
public int ROWS = 10;
public int COLS = 10;
public String a = "";
public String x = "";
public static int textSize;
public static int wordListTextSize;
public boolean gameFinish = false;
public Timer timer;
public Vector<Integer> coordinate_X1 = new Vector<Integer>();
public Vector<Integer> coordinate_Y1 = new Vector<Integer>();
public Vector<Integer> coordinate_X2 = new Vector<Integer>();
public Vector<Integer> coordinate_Y2 = new Vector<Integer>();
RelativeLayout winPopUp = null;
public int cellWidth = 0;
public int cellHeight = 0;
public TextView tv1;
public char[][] charArray;
public Vector<String> wordList;
public String[] tempWordList;
public RelativeLayout gameLayout;
//public RelativeLayout mainLayout;
public int colorCounter = 0;
public int noOfSelectedWords = 0;
public int totalNoOfWords = 0;

public int sec = 0;
public int hour = 0;
public int min = 0;
boolean gameStarted = false;

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



    // here is a relative layout , we get id of this layout and  add progress bar 
  //  to this reletive layout 

    final RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.main);
    mainLayout.post(new Runnable() {

        public void run() {
            // TODO Auto-generated method stub
            getLayoutInflater().inflate(R.layout.loading, mainLayout);
            ProgressBar spinner = (ProgressBar) findViewById(R.id.progressBar1);
            spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000,
                    android.graphics.PorterDuff.Mode.MULTIPLY);
        new DrawGame().execute(MainActivity.this);
        }
    });//here is code of add progress bar how we stop progress bar which is running 
                  //continue

}

public void initPlay() {
    ROWS = ScreenConstants.ROWS;
    cellWidth = cellHeight = (((RelativeLayout) findViewById(R.id.gameLayoutBg))
            .getHeight() * 98 / 100) / ROWS;
    System.out.println("Height "
            + findViewById(R.id.gameLayoutBg).getHeight());
    cellHeight = cellHeight - cellHeight / ROWS;
    cellWidth = 200;
    COLS = ScreenConstants.SCREENWIDTH / cellWidth;
    COLS = 10;
    gamePlay();
}

public void gamePlay() {
    Game game = new Game();
    game.getGame(ROWS, COLS, this, ScreenConstants.MAX_LENGTH);
    gameLayout = (RelativeLayout) findViewById(R.id.gameLayoutBg);
    RelativeLayout.LayoutParams rlparams = new RelativeLayout.LayoutParams(
            280, 300);
    rlparams.topMargin = 30;
    tv1 = new TextView(this);
    // tv1.setLayoutParams(rlparams);
    tv1.setId(1);

    charArray = new char[ROWS][COLS];
    int index = 0;
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            charArray[i][j] = game.charArray[index];
            x = x + "  " + charArray[i][j];
            tv1.setText(x);
            tv1.setTextSize(20);
            // tv1.setText(""+charArray[i][j]);
            Log.d(TAG, "VALUE OF TEXT BOX " + tv1);
            gameLayout.removeAllViews();
            gameLayout.addView(tv1, rlparams);
            index++;
        }
    }

    wordList = game.wordList;
    TextView tv = (TextView) findViewById(R.id.wordListLayoutBg1);

    tempWordList = Utility.getStringArray(wordList);
    for (int i = 0; i < tempWordList.length; i++) {

        a = a + "   " + tempWordList[i];
    }
    tv.setText(a);
    tv.setTextColor(getResources().getColor(R.color.black));
    // }
    totalNoOfWords = wordList.size();
    noOfSelectedWords = 0;

}


  private class DrawGame extends AsyncTask<MainActivity, Void, Boolean> {

  @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
    }


@Override
    protected void onProgressUpdate(Void... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
        initPlay();
    }


@Override
    protected void onPostExecute(Boolean result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
    //initPlay();

    }


@Override protected Boolean doInBackground(MainActivity... obj)
{

    //initPlay();
    try {
        publishProgress();
        Thread.sleep(3000);
    } catch (Exception e) {

    }


      return true;
  }
  }

2 个答案:

答案 0 :(得分:0)

假设R.id.progressBar1是您要处理的进度条,当您准备好继续时,只需:

ProgressBar spinner = (ProgressBar) findViewById(R.id.progressBar1);
spenner.setVisibility(View.INVISIBLE);

答案 1 :(得分:0)

使用onPreExecute展示ProgressBar并在onPostExecute内将其解除}将AsyncTask更改为:

 private class DrawGame extends AsyncTask<MainActivity, Void, Boolean> {
 ProgressBar spinner;
  @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        Show Progress Bar here...
          spinner = (ProgressBar)MainActivity.this. 
                                   findViewById(R.id.progressBar1);
        spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000,
                    android.graphics.PorterDuff.Mode.MULTIPLY);

          spinner.setVisibality(0); //<<< make  Visible here
        super.onPreExecute();
    }
 @Override
    protected void onPostExecute(Boolean result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
         spinner.setVisibility(8);  //<<< make in-Visible here
    //initPlay();

    }
//your code here...