我们如何在EditTextPreference中使用android:inputType?

时间:2015-07-10 21:17:16

标签: android xml edittextpreference

我检查了EditTextPreference的文档 http://developer.android.com/reference/android/preference/EditTextPreference.html

但是我没有在那里找到android:inputType属性。然后如何在此代码段中使用它

<EditTextPreference
        android:key="edit"
        android:title="@string/location1"
        android:summary="@string/summary1"
        android:dialogTitle="@string/location1"
        android:dialogMessage="@string/message"
        android:inputType="text"
        android:singleLine="true"
        />

同样怀疑android:singleLine属性。

5 个答案:

答案 0 :(得分:3)

文档不会列出该类的属性,但InputType属性(以及其他EditTextTextView属性)仍然有效。它只在文中说明。另请参阅this related question

EditTextPreference documentation并未明确列出其支持的所有属性,但文字说明:

  

请参阅EditText Attributes

这个链接非常有用(它们可能重新组织了一些属性,但从未更新过一些链接),但这里是inputType values的直接链接。作为快速摘要,这些值是(截至发布时):

  • 文本
  • textCapCharacters
  • textCapWords
  • textCapSentences
  • textAutoCorrect
  • textAutoComplete
  • textMultiLine
  • textImeMultiLine
  • textNoSuggestions
  • textUri
  • textEmailAddress
  • textEmailSubject
  • textShortMessage
  • textLongMessage
  • textPersonName
  • textPostalAddress
  • textPassword
  • textVisiblePassword
  • textWebEditText
  • textFilter
  • textPhonetic
  • textWebEmailAddress
  • textWebPassword
  • numberSigned
  • numberDecimal
  • numberPassword
  • 电话
  • 日期时间
  • 日期
  • 时间

您显然可以使用其中一个或多个,以|分隔(虽然我从未这样做过)。

答案 1 :(得分:1)

您不能从XML,但EditTextpreference exposes the EditText执行此操作,因此您可以通过编程方式执行此操作。在活动/片段中加载首选项后,您可以执行以下操作:

EditTextPreference pref = (EditTextPreference) PreferenceManager.findPreference("edit");
EditText prefEditText = pref.getEditText();
prefEditText.setInputType(InputType.TYPE_CLASS_TEXT);
prefEditText.setSingleLine(true);
// etc

答案 2 :(得分:0)

您可以找到问题here的答案

基本上,您需要导入androidX库并遵循我的代码。

答案 3 :(得分:0)

使用AndroidX将输入类型设置为密码,例如:

root_preferences.xml:

<androidx.preference.PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <Preference
        android:title="Password"
        android:key="my_pref_password"/>
</androidx.preference.PreferenceScreen>

在您的设置片段中:

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    setPreferencesFromResource(R.xml.root_preferences, rootKey);


    EditTextPreference pref = findPreference("my_pref_password");
    pref.setOnBindEditTextListener(new EditTextPreference.OnBindEditTextListener() {
        @Override
        public void onBindEditText(@NonNull EditText editText) {
            editText.setInputType(InputType.TYPE_CLASS_TEXT |
                            InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }
    });
}

答案 4 :(得分:0)

#最简单且经过测试的答案#

仅用于 EditTextPreference 中的十进制值

onCreatePreferences

中编写此代码

确保同时使用该类的CLASS属性

示例

输入类型。TYPE_CLASS_NUMBER |输入类型。TYPE_NUMBER_FLAG_DECIMAL

ghci> :set -XPartialTypeSignatures
ghci> :set -Wno-partial-type-signatures
ghci> :instances ApiResponse _
instance FromJSON w => FromJSON (ApiResponse w)
instance Generic (ApiResponse w)