Custom keyboard not working in fragment

2019-05-10 00:19发布

I have implemented my own custom keyboard in a fragment. The keyboard opens when i click the editText. But all the number keys are not working, some keys like 7, 0 and 8 works onclicking but most of the time it doesn't. keys like enter, right and left are working correctly. The same code works absolutely fine in Activity but its not working when implemented in Fragment. And also onFocus Listerner is not being called in this fragment. What would be reason? The code I implemented is as follows:

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() {
        }
    };

and XML is:

<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条回答
爷、活的狠高调
2楼-- · 2019-05-10 01:09

Posting as an answer since I don't have the reputation to comment...

Check out the following documentation link. https://developer.android.com/reference/android/R.attr.html#keycode

The primaryCode parameter you are using as your switch input should be using these codes. (KeyEvent.KEYCODE_[0-9] correspond to 7-16)

Can you post your XML file please? My assumption is that your android:codes for each Key in there are corresponding to the KEYCODE_[0-9] for those.

If this is a correct assumption then your switch case 7 will be triggered by pressing key 0, case 8 by key 1, etc.

查看更多
登录 后发表回答