自定义键盘无法在片段

时间:2015-10-13 08:49:23

标签: android android-fragments custom-keyboard

我在片段中实现了自己的自定义键盘。单击editText时键盘会打开。但是所有的数字键都不起作用,像7,0和8这样的键可以点击,但大部分时间它都没有。像enter,right和left这样的键正常工作。相同的代码在Activity中完全正常,但在Fragment中实现时它不起作用。此片段中也没有调用onFocus Listerner。什么是理由?我实现的代码如下:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_barcode_detail,
            container, false);
editText_barcode = (EditText) rootView
            .findViewById(R.id.editText_barcode);
// Lookup the KeyboardView
mKeyboardViewNum = (KeyboardView)    
rootView.findViewById(R.id.numerickeyboardview);

    mKeyboardView = (KeyboardView) rootView.findViewById(R.id.keyboardview);
// Numeric Keyboard
            key = new NumericKeyboard();
            mKeyboardNum = new Keyboard(mActivity,
                    R.xml.numeric_keyboard);
            // Lookup the KeyboardView
            // Attach the keyboard to the view
            mKeyboardViewNum.setKeyboard(mKeyboardNum);
            listKeysNum = mKeyboardNum.getKeys();
            // Do not show the preview balloons
             mKeyboardView.setPreviewEnabled(true);
            // Install the key handler
            mKeyboardViewNum

.setOnKeyboardActionListener(key.mOnKeyboardActionListenerNum);
            // Numeric Keyboard
editText_barcode.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            editos = (EditText) v;
            System.out.println("====onc== "+editos);
            hideCustomKeyboard();
            key.showCustomNumKeyboard(v);
        }
    });
    editText_barcode.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            EditText edittext = (EditText) v;
            int inType = edittext.getInputType(); // Backup the input type
            edittext.setInputType(InputType.TYPE_NULL); // Disable standard keyboard
            edittext.onTouchEvent(event); // Call native handler
            edittext.setInputType(inType); // Restore input type
            edittext.setTextIsSelectable(true);
            return false;
        }
    });
    editText_barcode.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                editos = (EditText) v;
                System.out.println("====onf== "+editos);
                key.showCustomNumKeyboard(v);
            } else {
                key.hideCustomNumKeyboard();
            }

        }
    });
return rootView;
}

        private OnKeyboardActionListener mOnKeyboardActionListenerNum = new   
OnKeyboardActionListener() {
        @Override
        public void onKey(int primaryCode, int[] keyCodes) {
            // Here check the primaryCode to see which key is pressed
            // based on the android:codes property

            int start = editos.getSelectionStart();
            Editable editable = editos.getText();

            switch (primaryCode) {
            case 0:
                editable.insert(start, "0");
                System.out.println("=====0== "+editable);
                break;
            case 1:
                editable.insert(start, "1");
                break;
            case 2:
                editable.insert(start, "2");
                break;
            case 3:
                editable.insert(start, "3");
                break;
            case 4:
                editable.insert(start, "4");
                break;
            case 5:
                editable.insert(start, "5");
                break;
            case 6:
                editable.insert(start, "6");
                break;
            case 7:
                editable.insert(start, "7");
                break;
            case 8:
                editable.insert(start, "8");
                break;
            case 9:
                editable.insert(start, "9");
                break;
            case 10:
                if (!editos.getText().toString().contains(".")) {
                    editable.insert(start, ".");
                }
                break;
            case -1:
                if (editable != null && start > 0) {
                    editable.delete(start - 1, start);
                }
                break;
            case 100:
                if (editos == editText_barcode) {
                        hideCustomNumKeyboard();
                }
                break;
            case 101:
                if (start > 0)
                    editos.setSelection(start - 1);
                break;
            case 201:
                if (start < editos.length())
                    editos.setSelection(start + 1);
                break;
            }
        }

        @Override
        public void onPress(int arg0) {
        }

        @Override
        public void onRelease(int primaryCode) {
        }

        @Override
        public void onText(CharSequence text) {
        }

        @Override
        public void swipeDown() {
        }

        @Override
        public void swipeLeft() {
        }

        @Override
        public void swipeRight() {
        }

        @Override
        public void swipeUp() {
        }
    };

和XML是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/light_grey" >

<RelativeLayout
    android:id="@+id/top"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/listrow_layerlist_background_dark_purple" >


<android.inputmethodservice.KeyboardView
    android:id="@+id/keyboardview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:keyPreviewLayout="@layout/kbpreview"
    android:keyPreviewOffset="12dp"
    android:visibility="gone" />

<android.inputmethodservice.KeyboardView
    android:id="@+id/numerickeyboardview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:keyPreviewLayout="@layout/kbpreview"
    android:keyPreviewOffset="12dp"
    android:visibility="gone" />

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/bottom"
    android:layout_below="@id/linear2"
    android:divider="@null" >
</ListView>

1 个答案:

答案 0 :(得分:0)

发布作为答案,因为我没有评论的声誉......

查看以下文档链接。 https://developer.android.com/reference/android/R.attr.html#keycode

您用作切换输入的primaryCode参数应该使用这些代码。 (KeyEvent.KEYCODE_ [0-9]对应于7-16)

你能发布你的XML文件吗? 我的假设是你的每个Key的android:代码对应于那些的KEYCODE_ [0-9]。

如果这是一个正确的假设,那么你的开关盒7将通过按键0,情况8按键1等触发。