在这段代码中,我想让按钮的可见性消失,如果按钮的文字是"":
if (button TEXT IS "")
{
button.setVisibility(View.GONE);
}
else
{
button.setVisibility(View.VISIBLE);
}
如何形成if-Statement以获得结果? 谢谢!
答案 0 :(得分:1)
Button b = (Button)findViewByID("your button id");
String buttonText = b.getText().toString();
if (buttonText.equals("Your Text"))
b.setVisibility(View.GONE);
else
b.setVisibility(View.VISIBLE);
如果您想检查是否为空,可以使用
if(buttonText.isEmpty())
b.setVisibility(View.GONE);
else
b.setVisibility(View.VISIBLE)
答案 1 :(得分:1)
问题的答案是,在Empty()
方法中尝试检查字符串中字符的长度是0还是更大:
if(text.isEmpty()){
//String is empty
}else{
//String includes characters
}
答案 2 :(得分:0)
if(button.getText() == null)
{
button.setVisibility(View.GONE);
}
else
{
button.setVisibility(View.VISIBLE);
}