当按钮text =“de”时必须显示“Correct”但是当我点击按钮时它显示错误但我的按钮文字是“de”。尽管按钮文本是“de”,为什么(if)语句不起作用?
public void bot1(View v) {
Button choice1 = (Button) findViewById(R.id.button1);
if(choice1.getText() == "de")
Toast.makeText(this, "Correct", Toast.LENGTH_SHORT).show();
else
Toast.makeText(this, "Wrong", Toast.LENGTH_SHORT).show();
}
答案 0 :(得分:2)
你必须使用equals,而不是==
public void bot1(View v){
Button choice1 = (Button) findViewById(R.id.button1);
if(choice1.getText().toString().equals("de"))
Toast.makeText(this,"Correct",Toast.LENGTH_SHORT).show();
else
Toast.makeText(this,"Wrong",Toast.LENGTH_SHORT).show();
}