我想在登录阶段添加一个复选框,当点击它时,应用程序也会记住用户退出时没有注销, 但是当没有点击该复选框时,用户可以访问多个活动,但是当他退出而没有注销时,仍然会注销。 这是我的代码:
public class MainActivity extends Activity implements OnClickListener{
private EditText username=null;
private EditText password=null;
private Button login, registration;
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String user = "nameKey";
public static final String pass = "passwordKey";
SharedPreferences sharedpreferences;
public boolean isChecked = false;
public boolean isunChecked = true;
private CheckBox check;
String a,b, result,z, clic;
Class<?> activityClass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
username = (EditText)findViewById(R.id.editText1);
password = (EditText)findViewById(R.id.editText2);
registration = (Button)findViewById(R.id.button2);
login = (Button)findViewById(R.id.button1);
TextView text2 = (TextView) findViewById(R.id.tv);
login.setOnClickListener(this);
registration.setOnClickListener(this);
check = (CheckBox) findViewById(R.id.checkBox);
check.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1 : {
login.setEnabled(false);
registra.setEnabled(true);
registra.setClickable(false);
username.setFocusable(false);
password.setFocusable(false);
if (check.isChecked()) {
clic = "Checked";
System.out.println("CheckBox is checked");
}
else{
clic = "no_Checked";
System.out.println("CheckBox is unchecked");
}
LoginTask task = new LoginTask(username,password);
task.execute(null,null,null);
break;
}
case R.id.button2: {
registration.setEnabled(false);
login.setEnabled(true);
login.setClickable(false);
registration(v);
break;
}
}
}
public class LoginTask extends AsyncTask<Void, Void, String> {
public LoginTask(EditText username2, EditText password2) {
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(result.equals("ok")){
Toast.makeText(MainActivity.this, "ok", Toast.LENGTH_SHORT).show();
}
else {
login.setEnabled(true);
login.setClickable(true);
registration.setEnabled(true);
registration.setClickable(true);
username.setFocusableInTouchMode(true);
password.setFocusableInTouchMode(true);
}
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected String doInBackground(Void... params) {
String result = null;
Editor editor = sharedpreferences.edit();
try {
[....]
String u = username.getText().toString();
String p = password.getText().toString();
editor.putString(user, u);
editor.putString(pass, p);
if(clic.equals("checked
editor.putBoolean("isChecked", isChecked);
System.out.println("clic");
}else{
editor.putBoolean("isChecked", isunChecked);
System.out.println("no_clic");
}
editor.commit();
Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
startActivity(intent);
}
catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
return result=e.getMessage();
}
return result="ok";
}
}
@Override
protected void onResume() {
sharedpreferences=getSharedPreferences(MyPREFERENCES,
Context.MODE_PRIVATE);
if(isChecked = sharedpreferences.getBoolean("isChecked", true)){
Toast.makeText(MainActivity.this, "login", Toast.LENGTH_SHORT).show();
if (sharedpreferences.contains(user))
{
if(sharedpreferences.contains(pass)){
Toast.makeText(MainActivity.this, "login", Toast.LENGTH_SHORT).show();
System.out.println(" CheckBox is checked");
try {
SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
activityClass = Class.forName(prefs.getString("activity1", FirstActivity.class.getName()));
prefs.getString("activity2", SecondActivity.class.getName());
} catch(ClassNotFoundException ex) {
}
Intent i = new Intent(this, activityClass);
startActivity(i);
}
}
}else{
Toast.makeText(MainActivity.this, "logout", Toast.LENGTH_SHORT).show();
System.out.println("logout");
//logout();
}
super.onResume();
}
public void registration (View view){
Intent intent1;
intent1 = new Intent (this, Registration.class);
startActivity(intent1);
}
public void onPause() {
super.onPause();
this.finish();
}
public void closing() {
finish();
}
此代码不起作用,因为当未选中该复选框时,用户尚未登录。也许我把控件放在复选框上是错误的?我该如何解决这个问题?
答案 0 :(得分:0)
您的用户被重新编号,因为当您暂停应用时,所有属性都会保持活动状态。您必须销毁主活动启动器才能删除这些值要在单击复选框时保持会话我建议您使用数据库来记住用户,密码和活动。