2013-10-27 73 views
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是真的還是錯的,什麼這個程序的問題,請告訴我回答...如果按鈕點擊收聽的Android條件錯誤

回答

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

應該

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

這是因爲你比較String對象

+0

謝謝先生......幫助我很好的答案.. – Alamgir

0

字符串比較由equals運算符完成。嘗試下面的代碼

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