如何使用android studio中的复选框添加记住我的功能? 我创建一个登录和注册活动,我使用json来获取数据, 这是我的登录活动代码。我是Android新手,任何人都可以帮助我
public EditText Username;
public EditText Password;
public TextView RegisterLink;
public Button LoginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Username = (EditText) findViewById(R.id.editUsername);
Password = (EditText) findViewById(R.id.editPassword);
RegisterLink = (TextView) findViewById(R.id.RegisterLink);
LoginButton = (Button) findViewById(R.id.buttonSignIn);
RegisterLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);
LoginActivity.this.startActivity(registerIntent);
}
});
LoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String username = Username.getText().toString();
final String password = Password.getText().toString();
Response.Listener<String> responseListener= new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (username.isEmpty() && password.isEmpty()){
Toast.makeText(getApplicationContext(),
"Username and Password field are empty", Toast.LENGTH_SHORT).show();
}else if (password.isEmpty()){
Toast.makeText(getApplicationContext(),
"Password field is empty", Toast.LENGTH_SHORT).show();
}else if (username.isEmpty()){
Toast.makeText(getApplicationContext(),
"username field is empty", Toast.LENGTH_SHORT).show();
}else {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success) {
String name = jsonResponse.getString("name");
int age = jsonResponse.getInt("age");
Intent intent = new Intent(LoginActivity.this, UserAreaActivity.class);
intent.putExtra("name", name);
intent.putExtra("age", age);
intent.putExtra("username", username);
LoginActivity.this.startActivity(intent);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
builder.setMessage("Incorrect Username and Password")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
};
LoginRequest loginRequest = new LoginRequest(username, password, responseListener);
RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
queue.add(loginRequest);
}
});
}
}
这是我的登录请求活动
private static final String LOGIN_REQUEST_URL = "http://sam.in/FetchUserData.php";
private Map<String, String> params;
public LoginRequest(String username, String password, Response.Listener<String> listener) {
super(Request.Method.POST, LOGIN_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put("username", username);
params.put("password", password);
}
@Override
public Map<String, String> getParams()
{
return params;
}
}