11

Is there a way to create a preference in a PreferenceFragment that accepts only integer values? I could implement an EditTextPreference and register an OnPreferenceChangeListener in which I could reject the change if the user enters a a string that is not a number, but I would prefer something that is meant for holding only numbers and that does not allow users to enter anything else, maybe showing only a dial pad keyboard.. I don't such a preference exist, since every descendant of Preference is mapped onto a Boolean (CheckBoxPreference), a String (*EditTextPreference) or a String array (MultiSelectListPreference), i.e. there are no preferences mapped onto integers, but maybe some of you can give me an hint or at least tell me if there are better solutions than the one I've proposed above.

Solution proposed by Grey:

EditText editText = ((EditTextPreference)    
                             findPreference("intent_property")).getEditText();
editText.setKeyListener(new NumberKeyListener() {
    @Override
    public int getInputType() {
        // The following shows the standard keyboard but switches to the view 
        // with numbers on available on the top line of chars
        return InputType.TYPE_CLASS_NUMBER;
        // Return the following to show a dialpad as the one shown when entering phone
        // numbers.
        // return InputType.TYPE_CLASS_PHONE
    }

    @Override
    protected char[] getAcceptedChars() {
        return new String("1234567890").toCharArray();
    }
});

Shorter solution, which does not allow varying the keyboard to dialpad but requires less code:

EditText editText = ((EditTextPreference) 
                             findPreference("intent_property")).getEditText();
editText.setKeyListener(new DigitsKeyListener());

I don't like just one thing about this solution: the user can enter 0000 and it is accepted and saved in the shared preference (which is a String) as "0000".. this requires you to implement a OnSharedPreferenceChangeListener to intercept changes to shared preferences and remove leading zeros in this preference or implement a change listener directly on this preference and return false to refuse numbers with trailing zeros (tell me if there is a better solution to this last problem which does not involve implementing your own Preference). It would be beautiful if we could modify the newValue in OnPreferenceChangeListener..

4

5 に答える 5

32

これを行う簡単な方法があります。XML の EditTextPreference を android:numeric="integer" に設定するだけです (整数、符号付き、または 10 進数に設定できます)。

Eclipse (または作業中のツール) は、これを可能な属性として表示しませんが、EditTextPreference に書き込んでプロジェクトをクリーンアップしてください。エラーが発生しないことがわかります。:)

于 2012-12-07T10:45:20.550 に答える
1

getEditText() を呼び出して EditText obj を取得し、次に NumberKeyListener で setKeyListener を呼び出すことができると思います。

于 2012-05-30T21:39:18.113 に答える
1

基本的にxmlファイルのような設定を表示するために使用されるPreferenceActivityを使用する必要があります。このアクティビティでは、チェックボックスを使用したり、テキストを編集したり、値を設定に保存したりできます。

于 2013-04-18T11:31:20.640 に答える
0

数値を parseInt() しようとした後に例外が発生したときに、ダイアログを再度開くこともできます。サンプルコードは次のとおりです。

public class MyEditTextPreference extends EditTextPreference {

        private EditText _editText;

        public MyEditTextPreference(Context context, AttributeSet attrs) {
            super(context, attrs);
            _editText = getEditText();
        }

        @Override
        public void onDismiss(DialogInterface dialog) {

            super.onDismiss(dialog);

            try {
                Integer.parseInt(_editText.getText().toString());
            } catch (NumberFormatException e) {

                ((Dialog) dialog).show();
            }
        }
    }
于 2014-01-17T19:30:42.770 に答える