我正在创建一个允许用户登录到他们创建的个人资料的应用程序,我只是想知道我如何获得正确的用户名和密码条目以将用户带到另一项 Activity ,因为目前我只是收到错误如果我尝试使用 intent 和 startactivity。
public class Login extends Activity implements OnClickListener{
/** Called when the activity is first created. */
private EditText etUsername;
private EditText etPassword;
private Button btnLogin;
//private Button btnRegister;
private TextView lblResult;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// Get the EditText and Button References
etUsername = (EditText)findViewById(R.id.EditUsername);
etPassword = (EditText)findViewById(R.id.EditPassword);
btnLogin = (Button)findViewById(R.id.login);
//btnRegister = (Button)findViewById(R.id.btnRegister);
lblResult = (TextView)findViewById(R.id.lblmsg);
// Button btnArrival = (Button) findViewById(R.id.btnRegister);
//btnArrival.setOnClickListener(this);
// Set Click Listener
btnLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Check Login
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
if(username.equals("User") && password.equals("user")){
Intent i = new Intent();
startActivity(i);
} else {
lblResult.setText("Login failed. Username and/or password doesn't match.");
}
}
});
}
public void onClick(View v)
{
Intent intent = new Intent(this, UsersDbAdapter.class);
startActivity(intent);
}
}
最佳答案
我假设您只是在测试,因为您将用户和密码都与“用户”进行了比较。但如果它们相等,则此代码运行:
Intent i = new Intent();
startActivity(i);
这是一个相当空洞的 Intent ,不是吗?为什么会有 Activity 开始? 稍微低一点,您正在开始这样的 Activity :
Intent intent = new Intent(this, UsersDbAdapter.class);
startActivity(intent);
看起来好多了。它有一个上下文和一个类,所以 startActivity 知道该做什么:)
关于安卓用户登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4934305/