您好我正在制作一个标志测验游戏,而我的if语句对我的输入没有反应我做了一个textview并且我完成了输入并且它是yahoo(我在编辑文本中输入了yahoo并且如果语句检查是否我判断是否这是雅虎... ...
package com.example.logogame;
imports....
public class Yahoo extends Activity implements OnClickListener{
EditText et;
Button check;
TextView test;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.yahoo);
et = (EditText) findViewById(R.id.editTextAnswer);
check = (Button) findViewById(R.id.buttonCheck);
test = (TextView) findViewById(R.id.test);
check.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.buttonCheck:
String content = et.getText().toString();
//its always wrong here...
if(content == "yahoo"){
Toast toast = Toast.makeText(this, "Correct! Good work", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}else{
test.setText(content);
Toast toast = Toast.makeText(this, "Wrong...", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
break;
}
}
答案 0 :(得分:1)
比较Strings
使用.equals()
。
所以改变
if(content == "yahoo"){
到
if(content.equals("yahoo")){
有关详细信息,请参阅How do I compare strings in Java?
答案 1 :(得分:1)
用于.equals()
比较的String
方法
if(content.equals("yahoo"))
答案 2 :(得分:0)
.equals(“String name”)使用它进行字符串比较。