无法从View转换为EditTextPreference

Tob*_*sen 3 android

我已经定义了我的设置活动的布局,如下所示:

<?xml version="1.0" encoding="utf-8"?>
Run Code Online (Sandbox Code Playgroud)

<PreferenceCategory 
    android:title="Second Category"
    android:key="second_category">

    <EditTextPreference
        android:id="@+id/pcnSetting"
        android:key="pcn"
        android:title="Set Canton Digits" 
        android:summary="Define your canton digits"
        android:dialogTitle="Set Canton Digits"
        android:dialogMessage="Please add your canton digits"
        android:inputType="number"
        android:imeOptions="actionDone"
        android:maxLength="4"/>

</PreferenceCategory>
Run Code Online (Sandbox Code Playgroud)

我想导入EditTextPreference到我的java代码,只是为了确保用户键入至少和最多4位数.

这就是我尝试EditTextPreference进入java的方式

    canton = (EditTextPreference)findViewById(R.id.pcnSetting); 
Run Code Online (Sandbox Code Playgroud)

但是Eclipse给了我这个错误:

Cannot cast from View to EditTextPreference
Run Code Online (Sandbox Code Playgroud)

为什么会这样?如何检查用户输入的内容是否至少最大4 int's

nan*_*esh 7

EditTextPreference不是View,它是首选项.因此,您必须在PreferenceActivity中使用findPreference().然后,您可以执行EditTextPreference.getEdittext以从此首选项获取视图