2013-12-20 55 views
0

我有一個活動,要求用戶用戶輸入他的用戶名和密碼,系統會比較用戶的輸入是否等於我輸入的用戶名和密碼。如果聲明從未評估爲真

但問題是,即使用戶的輸入是正確的,系統總是顯示一個說用戶名和密碼錯誤的敬酒如何解決這個問題?

任何人都可以幫助我嗎?

SecondActivity.java

public class SecondActivity extends Activity implements OnClickListener { 
    // for test the username and password 

    String username = "georges"; 
    String pass = "password"; 
    String username1; 
    String pass1; 

    Button btn_sign, btn_register; 
    RadioGroup rdg; 
    RadioButton rd_s, rd_o; 
    EditText edit_txt_name, edit_txt_password, edit_txt_o_pass; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_second); 

     edit_txt_name = (EditText) findViewById(R.id.editTxtName); 
     username1 = edit_txt_name.getText().toString(); 
     // Toast.makeText(this, username,Toast.LENGTH_SHORT).show(); 

     edit_txt_password = (EditText) findViewById(R.id.editTxtPass); 
     pass1 = edit_txt_password.getText().toString(); 

     // Toast.makeText(this, pass, Toast.LENGTH_SHORT).show(); 

     edit_txt_o_pass = (EditText) findViewById(R.id.edittxtOPass); 
     btn_register = (Button) findViewById(R.id.btnRegister); 
     btn_register.setOnClickListener(this); 

     btn_sign = (Button) findViewById(R.id.btnSingIN); 
     btn_sign.setOnClickListener(this); 

     rdg = (RadioGroup) findViewById(R.id.rdg); 
     rdg.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(RadioGroup arg0, int arg1) { 
       // TODO Auto-generated method stub 
       if (rd_o.isChecked()) { 
        edit_txt_o_pass.setVisibility(View.VISIBLE); 
       } 

      } 
     }); 

     rd_s = (RadioButton) findViewById(R.id.rdS); 
     rd_o = (RadioButton) findViewById(R.id.rdO); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.second, menu); 
     return true; 
    } 

    /* 
    * /if the user click button sign in and choose o the system will pop 
    * up a edit text that ask the user to enter the key to approve that he is 
    * an o and the the system will display the activities and pages that 
    * belong to the o, else the user choose so and the the system 
    * will display the activities and pages that belong to the so else the 
    * user click the register button and the system display the register 
    * layoyt. 
    */ 
    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 

     if (v.getId() == R.id.btnSingIN) { 

      if (rdg.getCheckedRadioButtonId() == R.id.rdS) { 

       SignIn(username1, pass1); 

       /* 
       * if (username.equals("georges") && pass.equals("password")) { 
       * 
       * Log.e("username and pasword", username + pass); Intent 
       * i_sign_s = new Intent(this, SignSoldgerActivity.class); 
       * startActivity(i_sign_s); 
       * 
       * } else { Toast.makeText(this, username + pass, 
       * Toast.LENGTH_LONG).show(); } 

    } 

    private void SignIn(String username2, String pass2) { // TODO 
     // Auto-generated method stub 

     if (username2.equals(username) && pass2.equals(pass)) { 

      Toast.makeText(this, username1 + "and" + pass1, Toast.LENGTH_LONG) 
        .show(); 

      // Intent i_sign_soldger = new Intent(this, 
      // SignSoldgerActivity.class); 
      // startActivity(i_sign_soldger); 

     } else { 
      Toast.makeText(this, "Error!!!!" + username1 + pass1, Toast.LENGTH_LONG).show(); 
     } 
     Log.e("username and pasword", username + pass); 
    } 

} 
+0

您的代碼是missig關閉註釋語句「SignIn」可用。就目前而言,您的代碼不會編譯。 –

回答

3

的問題是,你存儲在變數的EditTextusername1pass1的文字你的活動開始時。

既然你沒有足夠的時間來填補兩者getText().toString()回報""可能,因此雙方都從未等於"georges""password"

您需要在onClick方法中獲取輸入的兩個文本。

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 

    if (v.getId() == R.id.btnSingIN) { 

     if (rdg.getCheckedRadioButtonId() == R.id.rdS) {  
      String username1 = edit_txt_name.getText().toString(); 
      String pass1 = edit_txt_password.getText().toString(); 
      SignIn(username1, pass1); 
     } 
+0

謝謝你,先生,所以這是採取用戶的輸入爲空,因爲在oncreate方法 – user3006788

+1

@ user3006788是的,你需要獲得用戶名和通過點擊按鈕時。這應該現在工作。 –