if语句从未评估为true

时间:2013-12-20 12:42:28

标签: java android android-intent if-statement

我有一项活动要求用户用户输入他的用户名和密码,系统将比较用户的输入是否等于我输入的用户名和密码。

但问题是,即使用户的输入正确如何解决此问题,系统也会始终显示一个说出用户名和密码错误的Toast?

任何人都可以帮助我吗?

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);
    }

}

1 个答案:

答案 0 :(得分:3)

问题是,当您的活动开始时,您将EditText的文字存储在变量username1pass1中。

由于您没有时间填写这两者,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);
        }