如果声明为Android TextView消息显示

时间:2015-08-17 22:45:04

标签: java android if-statement textview settext

如何为Android if命令创建setText()语句?

例如,如果我有类似的东西:

Button button = (Button) findViewById(R.id.button);
TextView messageBox = (TextView)findViewById(R.id.message);
phrase[0] = "Hello World!";  //
phrase[1] = "Toast";         // array is declared earlier in code
Random r = new Random();
int  n = r.nextInt(1);
messageBox.setText(phrase[n]);

if(/*condition when phrase[1] is displayed in messageBox*/){
   // do stuff
}

我的想法是,我想构建一个if语句,用于监视我的{{​​1}}对象中何时显示某条消息。

3 个答案:

答案 0 :(得分:2)

试试这个:

 Button button = (Button) findViewById(R.id.button);
    TextView messageBox = (TextView)findViewById(R.id.message);
    phrase[0] = "Hello World!";  //
    phrase[1] = "Toast";         // array is declared earlier in code
    Random r = new Random();
    int  n = r.nextInt(1);
    messageBox.setText(phrase[0]);

    if(messageBox.getText().toString().equals("Toast")){
       // do stuff
    }

答案 1 :(得分:1)

试试这个:

if(phrase[n].equals(messageBox.getText().toString())){

}

答案 2 :(得分:0)

这样做:

Button button = (Button) findViewById(R.id.button);
TextView messageBox = (TextView) findViewById(R.id.message);
phrase[0] = "Hello World!";
phrase[1] = "Toast";
Random r = new Random();
int n = r.nextInt(1);
messageBox.setText(phrase[0]);

if(messageBox.getText().toString().equals("Toast")){
   //Write your code here when the result is Toast
} else {
   //Write your code here when the result is Hello World!
}