为什么这个if语句不起作用?

时间:2012-02-28 16:54:31

标签: java android if-statement android-edittext

我正在尝试创建一个if语句,例如,如果用户没有在两个编辑文本中键入任何内容,那么它会显示一个toast并重置所有内容。但由于某种原因,我的代码完全忽略了这一点,而只是转到else语句。

有人可以帮忙吗?

我的代码: 包com.gta5news.bananaphone;

import java.io.File;

import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

 public class LogIn extends Activity implements OnClickListener {
Button send;
EditText user;
EditText pass;
CheckBox staySignedIn;
SharedPreferences shareduser;
SharedPreferences sharedpass;
public static String settings = "MySharerdUser";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
    send = (Button) findViewById(R.id.bLogIn);
    user = (EditText) findViewById(R.id.eTuser);
    pass = (EditText) findViewById(R.id.eTpassword);
    staySignedIn = (CheckBox) findViewById(R.id.Cbstay);
    send.setOnClickListener(this);
    SharedPreferences settings = getPreferences(MODE_PRIVATE);
    String name = settings.getString("name", "");
    String password = settings.getString("pwd", "");
    user.setText(name);
    pass.setText(password);

    if (staySignedIn.isChecked()) {
        SharedPreferences MySharedUser = getPreferences(MODE_PRIVATE);
        settings.edit().putString("name", user.getText().toString())
                .putString("pwd", pass.getText().toString()).commit();
    }

    if (user.length() > 0)
        ;

}

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.bLogIn:
        if (user.length() < 0)  
    Toast.makeText(this,
            "Try to type in your username and password again!",
            Toast.LENGTH_LONG).show(); 
    else {
        String u = user.getText().toString();
        String p = pass.getText().toString();
        Bundle send = new Bundle();
        send.putString("key", u);
        send.putString("key", p);
        Intent a = new Intent(LogIn.this, FTPClient.class);
        startActivity(a);
        Toast.makeText(this, "Yay, you signed in!", Toast.LENGTH_LONG)
                .show();
        break;
    }
    }

}

}

3 个答案:

答案 0 :(得分:3)

 if (user.length() == 0)  

尝试它,因为user.length()永远不会进入小于Zero

答案 1 :(得分:0)

你必须使用

user.getText().toString().length()

答案 2 :(得分:0)

你需要一个onClickListener复选框???

checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
    // Perform action on clicks, depending on whether it's now checked
    if (((CheckBox) v).isChecked()) {
        Toast.makeText(HelloFormStuff.this, "Selected", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(HelloFormStuff.this, "Not selected", Toast.LENGTH_SHORT).show();
    }
}

});