如何使用共享首选项在文本视图中保存弹出值?

时间:2017-02-24 10:23:14

标签: java android sharedpreferences popupmenu

我能够将整数和字符串保存为共享首选项但是已经搜索过,似乎无法将弹出选定值保存为共享首选项?如何在共享首选项中保存文本视图值,请参阅下面的代码,让我知道如何存储到共享首选项并在onCreate中检索(..)

MainActivity

cardViewlist.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PopupMenu popup = new PopupMenu(MainActivity.this, cardViewlist);
            popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu());
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    int demoCount = PreferenceHelper.setIntValue(MainActivity.this, PreferenceHelper.NAME_ANAOUNCE_TOTAL_TIME,Integer.parseInt(item.toString()));
                    Toast.makeText(MainActivity.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
                    Log.d("asd", String.valueOf(demoCount));
                    textViewcount.setText(String.valueOf(demoCount));
                    return true;
                }

            });

            popup.show();
        }
    });

服务:

  final int count = PreferenceHelper.getIntValue(TTSService.this, PreferenceHelper.NAME_ANAOUNCE_TOTAL_TIME,1);
        if (ConName != null) {
            switch (count) {
                case 1:

                    tts.speak(ConName1, TextToSpeech.QUEUE_FLUSH, null);
                    break;

                case 2:

                    tts.speak(ConName2, TextToSpeech.QUEUE_FLUSH, null);
                    break;

                case 3:
                    tts.speak(ConName3, TextToSpeech.QUEUE_FLUSH, null);
                    break;

                case 4:
                    tts.speak(ConName4, TextToSpeech.QUEUE_FLUSH, null);
                    break;

                case 5:
                    tts.speak(ConName5, TextToSpeech.QUEUE_FLUSH, null);
                    break;
            }
        }

PreferenceHelper:

 public class PreferenceHelper {

public static String PREF_NAME="CallAnnouncer";
public static SharedPreferences AppPreference;
public static final String NAME_ANAOUNCE_TOTAL_TIME = "TotalTimeName";
public static String PREF_KEY_APP_LAUNCH_FIRST_TIME="IS_APP_FIRST_TIME";
public static String PREF_KEY_CALL_ENABLED="IS_INCOMING_ENABLED";
public static String PREF_KEY_SMS_ENABLED="IS_SMS_ENABLED";
public static String PREF_KEY_LANGUAGE="language";



public static String getValue(Context context, String key, String defaultValue){
    AppPreference=context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    String str=AppPreference.getString(key,defaultValue);
    return str;
}

public static void setValue(Context context, String key, String value){
    AppPreference=context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    Editor editor = AppPreference.edit();
    editor.putString(key,value);
    editor.commit();
}

public static String setValueLan(Context context, String key, Locale locale){
    AppPreference=context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    Editor editor = AppPreference.edit();
    editor.putString(key,locale.toString());
    editor.commit();
    return key;
}

public static String getValueLan(Context context, String key, Locale locale){
    AppPreference=context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    String str=AppPreference.getString(key,locale.getDisplayLanguage());
    return str;
}

public static boolean contains(Context context, String key){
    AppPreference=context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    if(AppPreference.contains(key)){
        return true;
    }else{
        return false;
    }
}
public static void clearPreference(Context context){
    AppPreference=context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    AppPreference.edit().clear().commit();
}
public static int getIntValue(Context context, String key, int Value) {
    AppPreference = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    int str = AppPreference.getInt(key, Value);
    return str;
}

public static int setIntValue(Context context, String key, int value) {
    AppPreference = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    Editor editor = AppPreference.edit();
    editor.putInt(key, value);
    editor.commit();
    return value;
}
 }

0 个答案:

没有答案