Android输入类型未被尊重

时间:2015-05-12 20:16:55

标签: android

我有一个listview,每行都有一个EditText。 EditText应该收集数值,所以我试图保持alpha键盘关闭,并允许用户只看到数字键盘。

但是,当列表视图被填充并且我点击列表中的任何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);

任何人都知道删除/添加textwatcher是否会破坏InputType?这是我应该探索的路径,以找出为什么键盘总是回到qwerty?

1 个答案:

答案 0 :(得分:1)

可能是ListView窃取焦点。尝试将此列表添加到ListView

<强> XML

android:descendantFocusability="afterDescendants"

或使用Java

listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);