Android TextView 消息显示的 If 语句
If Statement for Android TextView Message Display
如何为 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
}
试试这个:
if(phrase[n].equals(messageBox.getText().toString())){
}
这样做:
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!
}
如何为 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
}
试试这个:
if(phrase[n].equals(messageBox.getText().toString())){
}
这样做:
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!
}