sharedPreferences
效果图,
sharePreferences.java
package tjuci.edu.dl; import android.app.Activity; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.view.View; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class sharePreferences extends Activity { EditText userName, userCode; CheckBox chk; SharedPreferences prefer; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); userName = (EditText) findViewById(R.id.userName); userCode = (EditText) findViewById(R.id.userCode); chk = (CheckBox) findViewById(R.id.checkBox); prefer = getSharedPreferences("userInfo", MODE_PRIVATE); // System.out.println("========="+"pref是否为null"+pref==null); System.out.println("1111111111111"); boolean b = prefer == null; // System.out.println("prefer是否为null"+b); String xm = prefer.getString("xingming", ""); String mm = prefer.getString("mima", ""); userName.setText(xm); userCode.setText(mm); if(xm.equals("")){ chk.setChecked(false); System.out.println("88888,xm:"+xm); }else{ System.out.println("999999,xm:"+xm); chk.setChecked(true); } } public void doClick(View v) { switch (v.getId()) { case R.id.btnLogin: String user = userName.getText().toString().trim(); String code = userCode.getText().toString().trim(); if (user.equals("wyl") && code.equals("123")) { if (chk.isChecked()) { Editor edit = prefer.edit(); edit.putString("xingming", user); edit.putString("mima", "123"); edit.commit(); System.out.println("==========="); }else{ chk.setChecked(false); } System.out.println("dddddd"); Toast.makeText(this, "登陆成功!", 100).show(); }else{ Editor e = prefer.edit(); e.remove("xingming"); e.remove("mima"); e.commit(); Toast.makeText(this, "登陆失败,请确认!", 100).show(); String Xm = prefer.getString("xingming", "----"); Toast.makeText(this, "姓名:"+Xm, 100).show(); } break; default: break; } } }