自定义键盘无法分段工作 [英] Custom keyboard not working in fragment

查看:62
本文介绍了自定义键盘无法分段工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在片段中实现了自己的自定义键盘.当我单击editText时,键盘将打开.但是所有的数字键都不起作用,某些键(例如7、0和8)在单击时起作用,但大多数情况下不起作用.回车键,右键和左键等键均正常工作.相同的代码在Activity中绝对可以正常工作,但在Fragment中实现时则不能正常工作.而且在此片段中未调用onFocus Listerner.会是什么原因呢?我实现的代码如下:

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

而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>

推荐答案

将其发布为答案,因为我没有评论的口碑...

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

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

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

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

您能发布您的XML文件吗?我的假设是,其中每个键的android:code都与这些键的KEYCODE_ [0-9]相对应.

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.

如果这是一个正确的假设,那么您的开关工况7将通过按下键0触发,工况8通过按键1触发,等等.

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

这篇关于自定义键盘无法分段工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆