我有 2 个共享首选项的问题 .. 每次我尝试将新数据存储到共享首选项时,第一个问题都会被覆盖!
我还使用“包含(字符串键)”方法将新条目与旧条目进行比较,如果它们相等!, 但我认为这是不正确的!如果新条目与旧条目之一相等,则将新条目与共享首选项中的旧条目进行比较的最佳方法是什么?
这是我的注册代码:
package com.example.task_8;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Ac_3 extends Activity {
Button save;
EditText userName,password,userId;
String name,pass,Id;
Intent i;
SharedPreferences sp;
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
setContentView(R.layout.ac_3);
save= (Button)findViewById(R.id.button1);
userName=(EditText) findViewById(R.id.editText1);
password=(EditText) findViewById(R.id.editText2);
userId=(EditText) findViewById(R.id.editText3);
sp = getSharedPreferences("myshared", 0);
/////////////////////////////////////////////////
i = getIntent();
TextWatcher listener = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@SuppressWarnings("deprecation")
@Override
public void afterTextChanged(Editable s) {
name= userName.getText().toString();
pass= password.getText().toString();
Id=userId.getText().toString();
}
};
password.addTextChangedListener(listener);
userName.addTextChangedListener(listener);
save.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(!(Id.equals(sp.contains(Id))))
{
SharedPreferences.Editor spe = sp.edit();
spe.putString("userName",name);
spe.commit();
spe.putString("password",pass);
spe.commit();
spe.putString("userId",Id);
spe.commit();
}
else
{
AlertDialog alertDialog = null;
alertDialog = new AlertDialog.Builder(Ac_3.this).create();
alertDialog.setTitle("error msg");
alertDialog.setMessage("The two passwords does not match");
alertDialog.show();
}
i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
});
}
}
在主要活动中,如果我想登录,我想检查这个条目是否存在:
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(!name.equals(null) && !pass.equals(null))
{
if(sp.contains(Id)&&sp.contains(name)&&sp.contains(pass))
{
i = new Intent(getApplicationContext(), Ac_2.class);
i.putExtra("userName", name);
i.putExtra("password", pass);
i.putExtra("userId", Id);
startActivity(i);
}
else {
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("error msg");
//alertDialog.setPositiveButton("OK",this);
//alertDialog.setNegativeButton("Cancel", this);
alertDialog.setMessage("You should register before");
alertDialog.show();
/*alertDialog.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == Dialog.BUTTON_POSITIVE) {
//do what ever you want on OK click
}
}
}); */
}
}
}
});