Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void init() {
- EditText editText = (EditText) findViewById(R.id.password);
- editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
- @Override
- public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
- boolean handled = false;
- if (actionId == EditorInfo.IME_ACTION_SEND) {
- EditText passwordText = (EditText) findViewById(R.id.password);
- String Password = passwordText.getText().toString();
- EditText usernameText = (EditText) findViewById(R.id.username);
- String Username = usernameText.getText().toString();
- SaveData(Username, Password);
- handled = true;
- }
- return handled;
- }
- });
- }
- private void SaveData(String Username, String Password) {
- SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = sharedPref.edit();
- editor.putString(getString(R.string.saved_high_score), newHighScore);
- editor.commit();
- }
- import android.content.Context;
- import android.content.SharedPreferences;
- public class SharedPrefManager {
- public static SharedPreferences getSharedPref(Context mContext) {
- SharedPreferences pref = mContext.getSharedPreferences(Constants.SETTINGS, Context.MODE_PRIVATE);
- return pref;
- }
- public static void setPrefVal(Context mContext, String key, String value) {
- if(key!=null){
- SharedPreferences.Editor edit = getSharedPref(mContext).edit();
- edit.putString(key, value);
- edit.commit();
- }
- }
- public static void setIntPrefVal(Context mContext, String key, int value) {
- if(key!=null){
- SharedPreferences.Editor edit = getSharedPref(mContext).edit();
- edit.putInt(key, value);
- edit.commit();
- }
- }
- public static void setLongPrefVal(Context mContext, String key, Long value) {
- if(key!=null){
- SharedPreferences.Editor edit = getSharedPref(mContext).edit();
- edit.putLong(key, value);
- edit.commit();
- }
- }
- public static void setBooleanPrefVal(Context mContext, String key, boolean value) {
- if(key!=null){
- SharedPreferences.Editor edit = getSharedPref(mContext).edit();
- edit.putBoolean(key, value);
- edit.commit();
- }
- }
- public static String getPrefVal(Context mContext, String key) {
- SharedPreferences pref = getSharedPref(mContext);
- String val = "";
- try {
- if (pref.contains(key))
- val = pref.getString(key, "");
- else
- val = "";
- }catch (Exception e){
- e.printStackTrace();
- }
- return val;
- }
- public static int getIntPrefVal(Context mContext, String key) {
- SharedPreferences pref = getSharedPref(mContext);
- int val = 0;
- try {
- if(pref.contains(key)) val = pref.getInt(key, 0);
- }catch (Exception e){
- e.printStackTrace();
- }
- return val;
- }
- public static Long getLongPrefVal(Context mContext, String key) {
- SharedPreferences pref = getSharedPref(mContext);
- Long val = null;
- try{
- if(pref.contains(key)) val = pref.getLong(key, 0);
- }catch (Exception e){
- e.printStackTrace();
- }
- return val;
- }
- public static boolean getBooleanPrefVal(Context mContext, String key) {
- SharedPreferences pref = getSharedPref(mContext);
- boolean val = false;
- try{
- if(pref.contains(key)) val = pref.getBoolean(key, false);
- }catch (Exception e){
- e.printStackTrace();
- }
- return val;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement