我是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();
}
}
});
}
您需要移动
username = txtusername.getText().toString();
password = txtpassword.getText().toString();
在onClick()
内部,因为它的值为空。创建视图时。
更改代码如下,onCreate上的text返回空白,因为其中没有文本。
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();
}
}
});
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();
}
}
});
您不应该在onCreate()
中执行此操作,因为在创建视图时调用EditText.getText().toString();
,因此用户仍然没有输入任何数据。您应该将它们移动到onClick()
方法。
username = txtusername.getText().toString();
password = txtpassword.getText().toString();
这些行不应该出现在onCreate((中。因为最初,EditText是空的。当用户输入时,只有用户会按下登录按钮。
因此,将上面提到的行放在按钮的onClick((事件中。
如果在按钮的onClick((事件中发现editText为空,则应该显示Toast消息,如:
"请提供登录凭据!">
将这些行放入onClick()
中
username = txtusername.getText().toString();
password = txtpassword.getText().toString();
每次在edittext中输入文本时,都可以获得值onClick()
。
希望得到帮助。