java - Android TextView消息显示的If语句

标签 java android if-statement textview settext

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

例如,如果我有类似的内容:

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 语句来监视特定消息何时显示在我的 messageBox 对象中。

最佳答案

试试这个:

 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
    }

关于java - Android TextView消息显示的If语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32060891/

相关文章:

java - 允许通知 Android webview

java - INSTALL_FAILED_MISSING_SHARED_LIBRARY 和意外的顶级异常

c# - Android/Java对应C# Stream对象方法Write()的方法是什么?

Android应用程序不退出

android - 当我添加工具时发生了什么 :replace ="android:appComponentFactory"

javascript if 或语句

c++ - 代码占用了我一半的输入

java - Android Bump Api Network 主线程异常

iOS内联if else编译错误: "Expected : "; "Expected expression"

JAVA图形用户界面 : Can someone help me check why my button doesn't respond to it being clicked?