EditTextPreference的应用

android.preference.ListPreference的一些特性

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

1、布局:



      //项列表中选项的默认值


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")

你可能感兴趣的:(EditTextPreference的应用)