如何在android上比较按钮中的文本

如何在android上比较按钮中的文本,android,Android,我想知道按钮是否有文本startexecutestart-capture 如果按钮上有文本stop执行stop capture if { //button text is start //start capture } else if { //button text is stop // stop capture } 如何在android上比较按钮中的文本 if(button.getText().toString().equalsIgnoreCase("start")){ //st

我想知道按钮是否有文本
start
executestart-capture

如果按钮上有文本
stop
执行stop capture

if { //button text is start
   //start capture
} else if { //button text is stop
   // stop capture
}
如何在android上比较按钮中的文本

if(button.getText().toString().equalsIgnoreCase("start")){
//start capture
}else if (button.getText().toString().equalsIgnoreCase("stop")){
// stop capture
}

试试这个,希望它能有所帮助。对于嵌套的if-else语句,请始终使用switch

switch(button.getText().toString()){
case "start":
//start capture
break;
case "stop":
// stop capture
break;
}
它总是检查嵌套if-else中的所有条件 声明。但在切换情况下,它只会转到相关情况, 不会检查所有条件

-或-

按钮getText().toString().equalsIgnoreCase(“开始”)错误
if(btnText==“开始”)
。为什么设置
==
。??
Button button = (Button) findViewById(R.id.btn);

String btnText = button.getText().toString();

if (btnText.equalsIgnoreCase("start")){

  //something here
}
Button btn = (Button) findViewById(R.id.btn);;

if(btn.getText().toString().equalsIgnoreCase(text)){

   //something here

}
if(button1.getText().toString().equals("Start")){

}else if(button2.getText().toString().equals("Stop")){

}