【问题标题】:How to use numbers in EditText Preference androidx?如何在 EditText Preference androidx 中使用数字?
【发布时间】:2021-03-04 06:57:23
【问题描述】:

我需要在 androidx 的 EditText Preference 中显示数字键盘。

proxy.xml:

 <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >


    <EditTextPreference
        android:dialogTitle="edittext_dialogtitle_proxy_port"
        android:key="edittext_key_proxy_port"
        android:inputType="numberPassword"
        android:digits="0123456789."
        android:summary="Summary"
        android:title="Port"/>

</PreferenceScreen>

主活动:

 public class MainActivity extends PreferenceFragmentCompat {
    EditTextPreference mPortField;
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        addPreferencesFromResource(R.xml.proxy);
        mPortField = (EditTextPreference) findPreference("edittext_key_proxy_port");
        mPortField.setOnBindEditTextListener(new EditTextPreference.OnBindEditTextListener() {
            @Override
            public void onBindEditText(@NonNull EditText editText) {

            }
        });
 }

我没有得到数字键盘

[![enter image description here][1]][1]

【问题讨论】:

  • @AntoninGAVREL 我无法在 AndroidX 的 EditTextPreference 中使用 EditText
  • 你试过用android:inputType="numberDecimal"吗?
  • 是的@artman 我试过但还是不行。仍然键盘不只显示数字
  • 太糟糕了。我无法在我的电视应用程序的 EditTextPreference 上显示数字键盘。它也使用androidx.preference.EditTextPreference。所以我认为移动设备也没有区别。猜猜我们将不得不等待支持。在我的移动应用程序中,我使用常规片段和 com.google.android.material.textfield.TextInputEditText 并自己管理保存到设置

标签: android android-xml androidx android-preferences


【解决方案1】:

根据this docandroid:inputType 仅适用于EditText,不适用于EditTextPreference(不是extending EditText, DialogPreference instead)。但是您已经知道如何将EditText 隐藏在EditTextPreference 中,所以也许只需在onBindEditText 内以编程方式设置数字限制,并使用下面的行

editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);

(更多信息HERE

【讨论】:

  • 抱歉,getEditText() 在 androidx EditTextPreference 中不起作用。 getEditText() 仅适用于 android.preference.EditTextPreference。我正在使用 androidx
  • 抱歉,我从未发布过getEditText() 线路建议...我已发布setInputType 以粘贴到onBindEditText(@NonNull EditText editText) 中。字段 inputTypedigits 可能会从 prefs XML 中删除,它们不适用于 &lt;EditTextPreference 标签
  • 哇完美!! :-) 太感谢了。! @snachmsm
猜你喜欢
  • 1970-01-01
  • 2019-03-11
  • 2021-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-24
  • 2020-02-25
相关资源
最近更新 更多