Android HELP我的if语句不起作用

时间:2014-07-26 13:06:47

标签: android

您好我正在制作一个标志测验游戏,而我的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;

    }
}

3 个答案:

答案 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”)使用它进行字符串比较。