package com.example.utl;
import java.util.Map;
import android.content.Context;
import android.content.SharedPreferences;
/\*\*
\* 保存信息配置类
\*
\* @author admin
\*/
public class SharedPreferencesHelper {
private SharedPreferences sharedPreferences;
/\*
\* 保存手机里面的名字
\*/private SharedPreferences.Editor editor;
public SharedPreferencesHelper(Context context,String FILE\_NAME) {
sharedPreferences = context.getSharedPreferences(FILE\_NAME,
Context.MODE\_PRIVATE);
editor = sharedPreferences.edit();
}
/\*\*
\* 存储
\*/
public void put(String key, Object object) {
if (object instanceof String) {
editor.putString(key, (String) object);
} else if (object instanceof Integer) {
editor.putInt(key, (Integer) object);
} else if (object instanceof Boolean) {
editor.putBoolean(key, (Boolean) object);
} else if (object instanceof Float) {
editor.putFloat(key, (Float) object);
} else if (object instanceof Long) {
editor.putLong(key, (Long) object);
} else {
editor.putString(key, object.toString());
}
editor.commit();
}
/\*\*
\* 获取保存的数据
\*/
public Object getSharedPreference(String key, Object defaultObject) {
if (defaultObject instanceof String) {
return sharedPreferences.getString(key, (String) defaultObject);
} else if (defaultObject instanceof Integer) {
return sharedPreferences.getInt(key, (Integer) defaultObject);
} else if (defaultObject instanceof Boolean) {
return sharedPreferences.getBoolean(key, (Boolean) defaultObject);
} else if (defaultObject instanceof Float) {
return sharedPreferences.getFloat(key, (Float) defaultObject);
} else if (defaultObject instanceof Long) {
return sharedPreferences.getLong(key, (Long) defaultObject);
} else {
return sharedPreferences.getString(key, null);
}
}
/\*\*
\* 移除某个key值已经对应的值
\*/
public void remove(String key) {
editor.remove(key);
editor.commit();
}
/\*\*
\* 清除所有数据
\*/
public void clear() {
editor.clear();
editor.commit();
}
/\*\*
\* 查询某个key是否存在
\*/
public Boolean contain(String key) {
return sharedPreferences.contains(key);
}
/\*\*
\* 返回所有的键值对
\*/
public Map<String, ?> getAll() {
return sharedPreferences.getAll();
}
}
登录界面:
Activity中应用:
/\*\*
\* 登录界面
\*
\* @author admin
\*
\*/
public class LoginActivity extends Activity implements OnClickListener {
private EditText username;
private EditText password;
private Button login;// 登录按钮
private CheckBox holdPwd;// 保存用户和密码
private MyDialog mydialog;// 自定义对话框
private SharedPreferencesHelper sharedPreferencesHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE\_NO\_TITLE);
setContentView(R.layout.activity\_login);
init();
}
### 最后
我这里整理了一份完整的学习思维以及Android开发知识大全PDF。
![](https://img-blog.csdnimg.cn/img_convert/3442e64c51105a65d0cd7bf68ff25d79.webp?x-oss-process=image/format,png)
当然实践出真知,即使有了学习线路也要注重实践,学习过的内容只有结合实操才算是真正的掌握。
wFeature(Window.FEATURE\_NO\_TITLE);
setContentView(R.layout.activity\_login);
init();
}
### 最后
我这里整理了一份完整的学习思维以及Android开发知识大全PDF。
[外链图片转存中...(img-AP9lLyWj-1719661726944)]
当然实践出真知,即使有了学习线路也要注重实践,学习过的内容只有结合实操才算是真正的掌握。