TextView始终显示文本

时间:2015-12-18 21:29:37

标签: android

我希望我的文本对象在startGame()函数中完全执行for循环后在TextView中显示字符串,但是一旦它进入startGame()函数就会显示TextView。我不知道这可能是乱序执行还是什么,但有人可以帮帮我吗?

这是我的代码: `     import android.graphics.Color;     import android.os.Bundle;     import android.support.v7.app.AppCompatActivity;     import android.view.View;     import android.widget.Button;     import android.widget.TextView;     import java.util.Random;     import java.util.Timer;     import java.util.TimerTask;

public class EasyGameActivity extends AppCompatActivity
{

private int counter;
private TextView text;
private boolean flag = false;
private Button button = null;

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

startGame();
}

public void startGame()
{
counter = 3;
int temp;
int i;
int level = 1;
long nanoSec = 0;
final Random rand = new Random();
Timer timer = new Timer();


while(true)
{
BinaryTree binaryTree = new BinaryTree(counter);
nanoSec = 2000;

for(i = 0; i<counter; ++i)
{
temp = rand.nextInt(4);

switch(temp)
{
case 0:
button = (Button) findViewById(R.id.button5);   break;

case 1:
button = (Button) findViewById(R.id.button6);   break;

case 2:
button = (Button) findViewById(R.id.button7);   break;

case 3:
button = (Button) findViewById(R.id.button8);   break;
}

timer.schedule(new MyTask(button, temp), nanoSec);
timer.schedule(new MyTask(button, -1), nanoSec + 500);

nanoSec += 2000;
binaryTree.insert(temp);

}

text = (TextView) findViewById(R.id.levelText);
text.setText("Level: " + level);

break;
}


}



class MyTask extends TimerTask
{
private Button buttonId;
private int buttonNumb;

public MyTask(Button buttonId, int temp)
{
super();
this.buttonId = buttonId;
buttonNumb = temp;
}

@Override
public void run()
{
runOnUiThread(new Runnable() {

@Override
public void run()
{
if(buttonNumb == -1)
{
buttonId.setBackgroundColor(Color.BLACK);
}

else if(buttonNumb == 0)
{
buttonId.setBackgroundColor(Color.YELLOW);
}


else if(buttonNumb == 1)
{
buttonId.setBackgroundColor(Color.BLUE);

}

else if(buttonNumb == 2)
{
buttonId.setBackgroundColor(Color.RED);
}


else if(buttonNumb == 3)
{
buttonId.setBackgroundColor(Color.GREEN);
}
}
});
}
}

}

`

这是我的xml文件: `     

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<TextView
   android:id="@+id/levelText"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:textStyle="bold"
   android:textSize="50dp"
   android:textColor="#FFFFFF"
   android:text = "" />

<TextView
    android:id="@+id/countDnText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:textSize="100dp"
    android:textStyle="bold"
    android:textColor="#FFFFFF"
    android:text=""
    />


<Button
    android:layout_width="160dp"
    android:layout_height="200dp"
    android:background="#000000"
    android:id="@+id/button5"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="79dp" />

<Button
    android:layout_width="160dp"
    android:layout_height="200dp"
    android:background="#000000"
    android:id="@+id/button6"
    android:layout_alignTop="@+id/button5"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="160dp"
    android:layout_height="200dp"
    android:background="#000000"
    android:id="@+id/button7"
    android:layout_below="@+id/button5"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="10dp" />

<Button
    android:layout_width="160dp"
    android:layout_height="200dp"
    android:background="#000000"
    android:id="@+id/button8"
    android:layout_alignTop="@+id/button7"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

</RelativeLayout>

`

1 个答案:

答案 0 :(得分:3)

在设置textview的文本之前,你的代码不会等待计时器完成,所以它的工作方式是这样的(任意ms值可以说明我的观点):

  • 0ms - 运行For循环,排队计时器 - 运行第一个计时器(@ nanoSecond = 0)
  • 10ms - 设置TextView文本
  • 500ms - 第二个任务触发(@ nanoSecond = 500)
  • 等。用于其他For循环项目。

您可以简单地将编辑文本设置为在您拥有每项任务(@nanoSecond)的计时器上进行,并且它将在其他任务之后排队。这样的东西应该可以工作(替换你当前的TextView代码)。

final TextView textView = (TextView) findViewById(R.id.levelText);
Handler handler = new Handler();
handler.postDelayed(new Runnable{
    @Override
    public void run(){
        textView.setText("Level: " + level);
    }
}, nanoSec);