EditTextPreference的应用

2018-02-07  本文已影响49人  涛涛123759

android.preference.ListPreference的一些特性

android:key 选项的名称或键
android:title 选项的标题
android:summary 选项的简短摘要
android:entries 可将选项设置成的列表项文本
android:entryValues 定义每个列表项的键或值
android:dialogTitle 对话框的标题
android:defaultValue 项列表中选项的默认值

1、布局:

<PreferenceScreen
   xmlns:android="http://schemas.android.com/apk/res/android" 
    android:key="flight_option_preference"
    android:title="@string/prefTile"
    android:summary="@string/prefSummary">

    <ListPreference
        android:key="@string/speed_preference"          //选项的名称或键
        android:title="@string/listTitle"                    //选项的标题
        android:summary="@string/listSummery"                //选项的简短摘要
        android:entries="@array/flight_sort_options"              //可将选项设置成的列表项文本
        android:entryValues="@array/flight_sort_options_values"       // 定义每个列表项的键或值
        android:dialogTitle="@string/dialogTile"                // 对话框的标题
        android:defaultValue="@string/flight_sort_option_default_value"/>  //项列表中选项的默认值

</PreferenceScreen>

2、加载:

public class TtsSettings extends PreferenceActivity implements OnPreferenceChangeListener {

    public static final String PREFER_NAME = "com.iflytek.setting";
    private EditTextPreference mSpeedPreference;
    private EditTextPreference mPitchPreference;
    private EditTextPreference mVolumePreference;

    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        // 指定保存文件名字
        getPreferenceManager().setSharedPreferencesName(PREFER_NAME);
        addPreferencesFromResource(R.xml.tts_setting);
        mSpeedPreference = (EditTextPreference) findPreference("speed_preference");
        mSpeedPreference.getEditText().addTextChangedListener(new SettingTextWatcher(TtsSettings.this, mSpeedPreference, 0, 200));

    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        return true;
    }


}

public class SettingTextWatcher implements TextWatcher {
    private int editStart;
    private int editCount;
    private EditTextPreference mEditTextPreference;
    int minValue;//最小值
    int maxValue;//最大值
    private Context mContext;

    public SettingTextWatcher(Context context, EditTextPreference e, int min, int max) {
        mContext = context;
        mEditTextPreference = e;
        minValue = min;
        maxValue = max;
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
//      Log.e("demo", "onTextChanged start:"+start+" count:"+count+" before:"+before);
        editStart = start;
        editCount = count;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//      Log.e("demo", "beforeTextChanged start:"+start+" count:"+count+" after:"+after);
    }

    @Override
    public void afterTextChanged(Editable s) {
        if (TextUtils.isEmpty(s)) {
            return;
        }
        String content = s.toString();
//      Log.e("demo", "content:"+content);
        if (isNumeric(content)) {
            int num = Integer.parseInt(content);
            if (num > maxValue || num < minValue) {
                s.delete(editStart, editStart + editCount);
                mEditTextPreference.getEditText().setText(s);
                Toast.makeText(mContext, "超出有效值范围", Toast.LENGTH_SHORT).show();
            }

        } else {
            s.delete(editStart, editStart + editCount);
            mEditTextPreference.getEditText().setText(s);
            Toast.makeText(mContext, "只能输入数字哦", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 正则表达式-判断是否为数字
     */
    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }

};

3、取值:

SharedPreferences mSharedPreferences = getSharedPreferences(TtsSettings.PREFER_NAME, MODE_PRIVATE);
String speedStr = mSharedPreferences.getString("speed_preference", "50")
上一篇下一篇

猜你喜欢

热点阅读