if("a"=="a")不起作用

Sam*_*uel 2 android if-statement

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.beta);

    ed_code = (EditText) findViewById(R.id.et_beta_01);
    bu_ok = (Button) findViewById(R.id.bu_beta);

    bu_ok.setOnClickListener(new OnClickListener() {    
        @Override
        public void onClick(View v) {
                // TODO Auto-generated method stub
            String code = ed_code.getText().toString();
            String target = "vsi8";

            Log.v(TAG, code+"="+target);

            if(code == target){
                    Intent intent = new Intent(BetaCheck.this, AppMenu.class);
                startActivity(intent);
            }
            else {
                    Toast.makeText(getApplicationContext(), "wrong", Toast.LENGTH_SHORT).show();
                    ed_code.setText("");
            }
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

似乎if语句不理解2个值是相等的.

谢谢您的帮助

aio*_*obe 14

字符串,应该使用.equals和不比较==.(==检查引用相等性而不是内容相等性.)

也就是说,改变

if(code == target)
Run Code Online (Sandbox Code Playgroud)

if(code.equals(target))
Run Code Online (Sandbox Code Playgroud)

相关问题: