Android InputType 不受尊重

标签 android

我有一个 ListView ,每行都有一个 EditText。 EditText 应该收集数值,所以我尝试关闭字母键盘并允许用户只看到数字小键盘。

但是,当 ListView 被填充并且我单击列表中的任何 EditText 时,数字键盘出现 - 但随后很快被完整的 qwerty 键盘取代 - 并且用户必须切换回数字键盘以输入数字。

我已经以编程方式尝试了各种组合,如下所示:

mPercent.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL);

以及 XML 的多种组合,例如

android:inputType="phone"

android:digits="0123456789"

但是,无论组合如何,它们的键盘总是快速切换回 qwerty 并关闭数字键。

我认为这可能与 TextWatcher 有关。我使用 textwatcher 来指示输入的内容。我删除它,添加该行的 edittext 中可能包含的任何文本,然后添加 textwatcher。

这是我的自定义适配器的核心:

public View getView(final int position, View inView, ViewGroup parent) {
        ViewHolder holder = null;

        if (inView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            inView = inflater.inflate(mLayoutResource, null);
            holder = new ViewHolder();
            holder.mPercent = (EditText) inView.findViewById(R.id.percent);
            holder.mMaterial = (TextView) inView.findViewById(R.id.name);
            inView.setTag(holder);
        } else {
            holder = (ViewHolder) inView.getTag();
        }

        mMaterial.moveToPosition(position);

        holder.mPercent.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL);

// remove textwatcher on percentage
        holder.mPercent.removeTextChangedListener(percentWatcher);

        // insert the percentage for this row
        final String material = mMaterial.getString(mMaterial.getColumnIndex(OptionsProvider.OPTIONS_DATA));
        final EditText percent = holder.mPercent;

        percent.setTag(material);   // use material and position as keys into which of the many edittexts the textwatcher is watching
        percent.setId(position);

        holder.mMaterial.setText(material);

        // persist any text the user may have typed in the comment box
        if (percentages.containsKey(material)) {
            percent.setText(percentages.get(material));
        }
        else {
            percent.setText("");
        }

        // turn on textwatcher
        percentWatcher = new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // save this text and the position in which is resides.
                // if the user scrolls before pressing send, only this position should
                // contain the text
                String text = percent.getText().toString().trim();
                if (text.length() > 0) {
                    if ((position == percent.getId() &&
                            (material.equals( percent.getTag().toString())))) {
                        percentages.put(material, text);
                        Log.d(TAG, "Percentage inserted "+material+"="+text);
                    }
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {  }

            // force the EditText to stay with 3 digits (max of 100%)
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    int pct = Integer.parseInt(s.toString());
                    Log.d(TAG, " Material "+(String) percent.getTag()+ " = "+pct);
                }
            }
        };

        // add the textwatcher back to the edittext
        holder.mPercent.addTextChangedListener(percentWatcher);

有人知道删除/添加文本观察器是否会损坏 InputType 吗?这是我应该探索的路径来弄清楚为什么键盘总是回到 qwerty 吗?

最佳答案

可能是 ListView 抢走了焦点。尝试将此添加到您的 ListView

XML

android:descendantFocusability="afterDescendants"

或者在 Java 中

listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);

关于Android InputType 不受尊重,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30200631/

相关文章:

android - 在我的 android 手机中不使用互联网查找纬度和经度

android - 如何强制特定依赖项在Gradle中使用特定版本的库

java.lang.RuntimeException : writeValueXml: unable to write value 错误

Android Open Accessory 未检测到外部硬件

android - fragment 中的 setOnClickListener

java - Android/Java : How to reference from a class to the MainActivity. java

Android - 在 Kotlin 中声明和使用 View

android - 使用 Android 平板电脑作为 "input device"

android - 如何以编程方式检查带有波纹动画的 Android 单选按钮?

安卓:xml 还是 json?