Edittext.getText()的toString();是没有工作的android

时间:2014-07-31 10:01:00

标签: android android-edittext tostring

我是android开发的新手。我首先开始登录屏幕,但输入的文字无效。我不知道为什么。我一直在使用日志和吐司,但它到这里是我的代码。 Toast显示为空,标签甚至不会出现在调试窗口上,是的,我在布局文件中有这些字段。

public class MyActivity extends ActionBarActivity {
private static final String TAG = "LoginInfo";
EditText txtusername, txtpassword;
Button button;
String username, password;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    txtusername = (EditText) findViewById(R.id.txtUserName);
    txtpassword = (EditText) findViewById(R.id.txtPassword);
    button = (Button) findViewById(R.id.btnLogin);
    username = txtusername.getText().toString();
    password = txtpassword.getText().toString();
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), username, Toast.LENGTH_SHORT).show();
            Log.d(TAG, username + password);
            if ((!username.isEmpty()) & (!password.isEmpty())) {
                SharedPreferences sharedpreferences = getSharedPreferences("Login_Info", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedpreferences.edit();
                editor.putString("USERNAME", username);
                editor.putString("PASSWORD", password);
                editor.commit();
                Log.d(TAG, username + password);
            } else {
                Toast.makeText(getApplicationContext(), "Entered Login Info is Incorrect", Toast.LENGTH_SHORT).show();
            }
        }
    });
}

6 个答案:

答案 0 :(得分:3)

你需要移动

 username = txtusername.getText().toString();
 password = txtpassword.getText().toString();

onClick()内,因为它的值为空。创建视图时。

答案 1 :(得分:0)

如下所示更改您的代码,onCreate上的getText由于此处没有文本而返回空白。

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        username = txtusername.getText().toString();
        password = txtpassword.getText().toString();
        Toast.makeText(getApplicationContext(), username, Toast.LENGTH_SHORT).show();
        Log.d(TAG, username + password);
        if ((!username.isEmpty()) & (!password.isEmpty())) {
            SharedPreferences sharedpreferences = getSharedPreferences("Login_Info", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedpreferences.edit();
            editor.putString("USERNAME", username);
            editor.putString("PASSWORD", password);
            editor.commit();
            Log.d(TAG, username + password);
        } else {
            Toast.makeText(getApplicationContext(), "Entered Login Info is Incorrect", Toast.LENGTH_SHORT).show();
        }
    }
});

答案 2 :(得分:0)

public class MyActivity extends ActionBarActivity {
private static final String TAG = "LoginInfo";
EditText txtusername, txtpassword;
Button button;
String username, password;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    txtusername = (EditText) findViewById(R.id.txtUserName);
    txtpassword = (EditText) findViewById(R.id.txtPassword);
    button = (Button) findViewById(R.id.btnLogin);

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        username = txtusername.getText().toString();
        password = txtpassword.getText().toString();
        Toast.makeText(getApplicationContext(), username, Toast.LENGTH_SHORT).show();
        Log.d(TAG, username + password);
        if ((!username.isEmpty()) & (!password.isEmpty())) {
            SharedPreferences sharedpreferences = getSharedPreferences("Login_Info", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedpreferences.edit();
            editor.putString("USERNAME", username);
            editor.putString("PASSWORD", password);
            editor.commit();
            Log.d(TAG, username + password);
        } else {
            Toast.makeText(getApplicationContext(), "Entered Login Info is Incorrect", Toast.LENGTH_SHORT).show();
        }
    }
});

答案 3 :(得分:0)

在创建视图时调用onCreate()时,不应在EditText.getText().toString();中执行此操作,因此用户仍未输入任何数据。您应该将它们移到onClick()方法。

答案 4 :(得分:0)

将这些行放在onClick()

username = txtusername.getText().toString();
password = txtpassword.getText().toString();

每次在edittext中输入文字时,您都可以获得值onClick()

希望得到这个帮助。

答案 5 :(得分:0)

username = txtusername.getText().toString();
password = txtpassword.getText().toString();

onCreate()中不应包含这些行。因为最初,EditTexts是空的。 当用户提供输入时,只有用户才会按下登录按钮。

所以将上面提到的行放在按钮的onClick()事件中。

如果在按钮的onClick()事件中发现editTexts为空,则应显示Toast消息,如:

  

"请提供登录凭据!"