您好我的简单应用程序始终返回错误消息而不是成功消息。
RegisterActivity.class
Intent intent=new Intent(getApplicationContext(),LoginActivity.class);
intent.putExtra ( "user", username.getText().toString() );
intent.putExtra ( "pass", password.getText().toString() );
Toast.makeText(getApplicationContext(), Submit, Toast.LENGTH_SHORT).show();
startActivity(intent);
LoginActivity.class
private EditText username,password;
private Button ok,clear;
//String Array
String LoginSucc = "Login Success";
String LoginFailed= "Login Failed";
String ClearFields = "Text Fields Cleared";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = findViewById(R.id.txtUsername);
password = findViewById(R.id.txtPassword);
ok = findViewById(R.id.Ok);
clear = findViewById(R.id.Clear);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle bb;
bb=getIntent().getExtras();
if(username.equals(bb.getString("user"))&& password.equals(bb.getString("pass")))
{
Toast.makeText(getApplicationContext(), LoginSucc, Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getApplicationContext(), LoginFailed, Toast.LENGTH_SHORT).show();
}
}
});
总是返回LoginFailed
消息,即使我把正确的登录信息。
你需要这样做:
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String userName = username.getText().toString();
String passWord = password.getText().toString();
Bundle bb;
bb=getIntent().getExtras();
if(userName.equals(bb.getString("user"))&& passWord.equals(bb.getString("pass")))
{
Toast.makeText(getApplicationContext(), LoginSucc, Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getApplicationContext(), LoginFailed, Toast.LENGTH_SHORT).show();
}
}
})
;
我相信你忘了在第一个活动中创建一个Bundle对象。
RegisterActivity.class
Intent intent = new Intent(getActivity(),LoginActivity.class);
String username = username.getText().toString();
String password = password.getText().toString();
Bundle bb = new Bundle();
bb.putString("user", username);
bb.putString("pass", password);
intent.putExtras(bb);
startActivity(intent);
LoginActivity.class
private Bundle bb;
...
bb = intent.getExtras();
String getUsername = extras.getString("user");
String getPassword = extras.getString("pass");