1
public class MainActivity extends Activity {

    EditText t1,t2;
    TextView v1,v2;
    String username1 = "admin";
    String password1 = "12345";
    Button b1;
    String user;
    Context c;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        c = getApplicationContext();
        t1 = (EditText) findViewById(R.id.editText1);
        t2 = (EditText) findViewById(R.id.editText2);
        b1 = (Button) findViewById(R.id.button1);
        v1 = (TextView) findViewById(R.id.textView1);
        v2 = (TextView) findViewById(R.id.textView2);
        v1.setTextColor(Color.WHITE);
        v2.setTextColor(Color.WHITE);
        button();
    }

    public void button()
    {
         b1.setOnClickListener(new OnClickListener() {
             public void onClick(View v) {
                 user = t1.getText().toString();
                 String pass = t2.getText().toString();

                 if(user == username1 && pass == password1)
                 {
                     Intent aa = new Intent(MainActivity.this, admin.class);
                     startActivity(aa);
                 }
                 else
                 {
                     Toast.makeText(c, "username or password is incorrect",Toast.LENGTH_LONG).show(); 
                 }

             }
         });
    }

}

每次 else 块都会执行,如果 codition 是对还是错,这个程序有什么问题,请告诉我答案...

4

2 回答 2

0
if(user == username1 && pass == password1) 

应该

if(user.equals(username1) && pass.equals(password1))

这是因为您正在比较String对象

于 2013-10-27T10:30:53.117 回答
0

字符串比较由equals运算符完成。试试下面的代码

 if(user.equals(username1) && pass.equals(password1))
于 2013-10-27T10:31:04.250 回答