如何在Android自定义键盘中添加shift键?

如何在Android自定义键盘中添加shift键?,android,keyboard,android-edittext,android-keypad,Android,Keyboard,Android Edittext,Android Keypad,我正在使用自定义键盘。我想添加shift键,并在用户按下时更改xml文件 lowercase.xml <Keyboard xmlns:android="http://schemas.android.com/apk/res/android" android:keyWidth="10%p" android:horizontalGap="0px" android:verticalGap="0px" android:keyHeight="6%p" >

我正在使用自定义键盘。我想添加shift键,并在用户按下时更改xml文件

lowercase.xml

<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyWidth="10%p"
    android:horizontalGap="0px"
    android:verticalGap="0px"
    android:keyHeight="6%p"
    >


    <Row   android:keyWidth="18%p">
        <Key android:codes="49" android:keyLabel="1" android:keyEdgeFlags="left"/>
        <Key android:codes="50" android:keyLabel="2"/>
        <Key android:codes="51" android:keyLabel="3"/>
        <Key android:codes="52" android:keyLabel="4"/>
        <Key android:codes="53" android:keyLabel="5"/>
        <Key android:codes="54" android:keyLabel="6"/>
        <Key android:codes="55" android:keyLabel="7"/>
        <Key android:codes="56" android:keyLabel="8"/>
        <Key android:codes="57" android:keyLabel="9"/>
        <Key android:codes="48" android:keyLabel="0" android:keyEdgeFlags="right"/>

    </Row>
    <Row   >
        <Key android:codes="113" android:keyLabel="Q" android:keyEdgeFlags="left"/>
        <Key android:codes="119" android:keyLabel="W"/>
        <Key android:codes="101" android:keyLabel="E"/>
        <Key android:codes="114" android:keyLabel="R"/>
        <Key android:codes="116" android:keyLabel="T"/>
        <Key android:codes="121" android:keyLabel="Y"/>
        <Key android:codes="117" android:keyLabel="U"/>
        <Key android:codes="105" android:keyLabel="I"/>
        <Key android:codes="111" android:keyLabel="O"/>
        <Key android:codes="112" android:keyLabel="P" android:keyEdgeFlags="right"/>
    </Row>

    <Row    >
        <Key android:codes="46" android:keyLabel="." android:keyEdgeFlags="left"/>
        <Key android:codes="97" android:keyLabel="A"/>
        <Key android:codes="115" android:keyLabel="S"/>
        <Key android:codes="100" android:keyLabel="D"/>
        <Key android:codes="102" android:keyLabel="F"/>
        <Key android:codes="103" android:keyLabel="G"/>
        <Key android:codes="104" android:keyLabel="H"/>
        <Key android:codes="106" android:keyLabel="J"/>
        <Key android:codes="107" android:keyLabel="K"/>
        <Key android:codes="108" android:keyLabel="L" android:keyEdgeFlags="right"/>
        <Key android:codes="-5" android:keyIcon="@drawable/sym_keyboard_delete"
            android:keyWidth="15%p" android:keyEdgeFlags="right"
            android:isRepeatable="true"/>
    </Row>

    <Row    android:keyHeight="8%p">
        <Key android:codes="00"
     android:keyIcon="@drawable/key_qwerty_shift"       android:iconPreview="@drawable/key_qwerty_shift_b" android:keyWidth="15%p" android:isModifier="true" android:isSticky="true" android:keyEdgeFlags="left"/>
        <Key android:codes="32" android:keyLabel="SPACE"
            android:keyWidth="20%p" android:isRepeatable="true"/>
        <Key android:codes="122" android:keyLabel="Z"/>
        <Key android:codes="120" android:keyLabel="X"/>
        <Key android:codes="99" android:keyLabel="C"/>
        <Key android:codes="118" android:keyLabel="V"/>
        <Key android:codes="98" android:keyLabel="B"/>
        <Key android:codes="110" android:keyLabel="N"/>
        <Key android:codes="109" android:keyLabel="M"/>
        <Key android:codes="10" android:keyIcon="@drawable/sym_keyboard_return"
            android:keyWidth="20%p" android:keyEdgeFlags="right"/>

    </Row>


</Keyboard>
和upercase.xml

<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyWidth="10%p"
    android:horizontalGap="0px"
    android:verticalGap="0px"
    android:keyHeight="6%p"
    >


    <Row   android:keyWidth="18%p">
        <Key android:codes="49" android:keyLabel="1" android:keyEdgeFlags="left"/>
        <Key android:codes="50" android:keyLabel="2"/>
        <Key android:codes="51" android:keyLabel="3"/>
        <Key android:codes="52" android:keyLabel="4"/>
        <Key android:codes="53" android:keyLabel="5"/>
        <Key android:codes="54" android:keyLabel="6"/>
        <Key android:codes="55" android:keyLabel="7"/>
        <Key android:codes="56" android:keyLabel="8"/>
        <Key android:codes="57" android:keyLabel="9"/>
        <Key android:codes="48" android:keyLabel="0" android:keyEdgeFlags="right"/>

    </Row>
    <Row   >
        <Key android:codes="113" android:keyLabel="Q" android:keyEdgeFlags="left"/>
        <Key android:codes="119" android:keyLabel="W"/>
        <Key android:codes="101" android:keyLabel="E"/>
        <Key android:codes="114" android:keyLabel="R"/>
        <Key android:codes="116" android:keyLabel="T"/>
        <Key android:codes="121" android:keyLabel="Y"/>
        <Key android:codes="117" android:keyLabel="U"/>
        <Key android:codes="105" android:keyLabel="I"/>
        <Key android:codes="111" android:keyLabel="O"/>
        <Key android:codes="112" android:keyLabel="P" android:keyEdgeFlags="right"/>
    </Row>

    <Row    >
        <Key android:codes="46" android:keyLabel="." android:keyEdgeFlags="left"/>
        <Key android:codes="97" android:keyLabel="A"/>
        <Key android:codes="115" android:keyLabel="S"/>
        <Key android:codes="100" android:keyLabel="D"/>
        <Key android:codes="102" android:keyLabel="F"/>
        <Key android:codes="103" android:keyLabel="G"/>
        <Key android:codes="104" android:keyLabel="H"/>
        <Key android:codes="106" android:keyLabel="J"/>
        <Key android:codes="107" android:keyLabel="K"/>
        <Key android:codes="108" android:keyLabel="L" android:keyEdgeFlags="right"/>
        <Key android:codes="-5" android:keyIcon="@drawable/sym_keyboard_delete"
            android:keyWidth="15%p" android:keyEdgeFlags="right"
            android:isRepeatable="true"/>
    </Row>

    <Row    android:keyHeight="8%p">
        <Key android:codes="00"
     android:keyIcon="@drawable/key_qwerty_shift"       android:iconPreview="@drawable/key_qwerty_shift_b" android:keyWidth="15%p" android:isModifier="true" android:isSticky="true" android:keyEdgeFlags="left"/>
        <Key android:codes="32" android:keyLabel="SPACE"
            android:keyWidth="20%p" android:isRepeatable="true"/>
        <Key android:codes="122" android:keyLabel="Z"/>
        <Key android:codes="120" android:keyLabel="X"/>
        <Key android:codes="99" android:keyLabel="C"/>
        <Key android:codes="118" android:keyLabel="V"/>
        <Key android:codes="98" android:keyLabel="B"/>
        <Key android:codes="110" android:keyLabel="N"/>
        <Key android:codes="109" android:keyLabel="M"/>
        <Key android:codes="10" android:keyIcon="@drawable/sym_keyboard_return"
            android:keyWidth="20%p" android:keyEdgeFlags="right"/>

    </Row>


</Keyboard>
CustomKeyboard.java

class CustomKeyboard {

    /** A link to the KeyboardView that is used to render this CustomKeyboard. */
    private KeyboardView mKeyboardView;
    /** A link to the activity that hosts the {@link #mKeyboardView}. */
    private Activity mHostActivity;

    /** The key (code) handler. */
    private OnKeyboardActionListener mOnKeyboardActionListener = new OnKeyboardActionListener() {

        public final static int CodeDelete = -5; // Keyboard.KEYCODE_DELETE
        public final static int CodePrev = 55000;
        public final static int CodeNext = 55001;
        public final static int CodeDone = 55002;

        @Override
        public void onKey(int primaryCode, int[] keyCodes) {
            // NOTE We can say '<Key android:codes="49,50" ... >' in the xml
            // file; all codes come in keyCodes, the first in this list in
            // primaryCode
            // Get the EditText and its Editable
            View focusCurrent = mHostActivity.getWindow().getCurrentFocus();
            if (focusCurrent == null
                    || focusCurrent.getClass() != EditText.class)
                return;
            EditText edittext = (EditText) focusCurrent;
            Editable editable = edittext.getText();
            int start = edittext.getSelectionStart();
            // Apply the key to the edittext
            if (primaryCode == CodeDone) {
                hideCustomKeyboard();
            } 
            else if (primaryCode == CodeDelete) 
            {
                if (editable != null && start > 0)
                    editable.delete(start - 1, start);
            } 
            else if (primaryCode == CodePrev) {
                View focusNew = edittext.focusSearch(View.FOCUS_BACKWARD);
                if (focusNew != null)
                    focusNew.requestFocus();
            } 
            else if (primaryCode == CodeNext) {
                View focusNew = edittext.focusSearch(View.FOCUS_FORWARD);
                if (focusNew != null)
                    focusNew.requestFocus();
            } 
            else { // insert character
                editable.insert(start, Character.toString((char) primaryCode));
            }
        }

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

    /**
     * Create a custom keyboard, that uses the KeyboardView (with resource id
     * <var>viewid</var>) of the <var>host</var> activity, and load the keyboard
     * layout from xml file <var>layoutid</var> (see {@link Keyboard} for
     * description). Note that the <var>host</var> activity must have a
     * <var>KeyboardView</var> in its layout (typically aligned with the bottom
     * of the activity). Note that the keyboard layout xml file may include key
     * codes for navigation; see the constants in this class for their values.
     * Note that to enable EditText's to use this custom keyboard, call the
     * {@link #registerEditText(int)}.
     * 
     * @param host
     *            The hosting activity.
     * @param viewid
     *            The id of the KeyboardView.
     * @param layoutid
     *            The id of the xml file containing the keyboard layout.
     */
    public CustomKeyboard(Activity host, int viewid, int layoutid) {
        mHostActivity = host;
        mKeyboardView = (KeyboardView) mHostActivity.findViewById(viewid);
        mKeyboardView.setKeyboard(new Keyboard(mHostActivity, layoutid));
        mKeyboardView.setPreviewEnabled(false); // NOTE Do not show the preview
                                                // balloons
        mKeyboardView.setOnKeyboardActionListener(mOnKeyboardActionListener);
        // Hide the standard keyboard initially
        mHostActivity.getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }

    /** Returns whether the CustomKeyboard is visible. */
    public boolean isCustomKeyboardVisible() {
        return mKeyboardView.getVisibility() == View.VISIBLE;
    }

    /**
     * Make the CustomKeyboard visible, and hide the system keyboard for view v.
     */
    public void showCustomKeyboard(View v) {
        mKeyboardView.setVisibility(View.VISIBLE);
        mKeyboardView.setEnabled(true);
        if (v != null)
            ((InputMethodManager) mHostActivity
                    .getSystemService(Activity.INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(v.getWindowToken(), 0);
    }

    /** Make the CustomKeyboard invisible. */
    public void hideCustomKeyboard() {
        mKeyboardView.setVisibility(View.GONE);
        mKeyboardView.setEnabled(false);
    }

    /**
     * Register <var>EditText<var> with resource id <var>resid</var> (on the
     * hosting activity) for using this custom keyboard.
     * 
     * @param resid
     *            The resource id of the EditText that registers to the custom
     *            keyboard.
     */
    public void registerEditText(int resid) {
        // Find the EditText 'resid'
        EditText edittext = (EditText) mHostActivity.findViewById(resid);
        // Make the custom keyboard appear
        edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
            // NOTE By setting the on focus listener, we can show the custom
            // keyboard when the edit box gets focus, but also hide it when the
            // edit box loses focus
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus)
                    showCustomKeyboard(v);
                else
                    hideCustomKeyboard();
            }
        });
        edittext.setOnClickListener(new OnClickListener() {
            // NOTE By setting the on click listener, we can show the custom
            // keyboard again, by tapping on an edit box that already had focus
            // (but that had the keyboard hidden).
            @Override
            public void onClick(View v) {
                showCustomKeyboard(v);
            }
        });
        // Disable standard keyboard hard way
        // NOTE There is also an easy way:
        // 'edittext.setInputType(InputType.TYPE_NULL)' (but you will not have a
        // cursor, and no 'edittext.setCursorVisible(true)' doesn't work )
        edittext.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
                return true; // Consume touch event
            }
        });
        // Disable spell check (hex strings look like words to Android)
        edittext.setInputType(edittext.getInputType()
                | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
    }

}

请帮助我将大写键盘转换为小写键盘。

这是一个老问题。只是偶然发现的。以下是我对答案的看法,如果有人感兴趣,我会提供给大家参考。 加:

其中,键盘是使用一个XML充气的键盘实例。例如:

keyboard = new Keyboard(this, R.xml.lowercase);
caps是一个标志,用于确定是移到大写还是移回小写。这基本上应该会在按键时移动键盘

p、
您应该使用switch case语句,而不是所有if-else语句。

这是一个老问题。只是偶然发现的。以下是我对答案的看法,如果有人感兴趣,我会提供给大家参考。 加:

其中,键盘是使用一个XML充气的键盘实例。例如:

keyboard = new Keyboard(this, R.xml.lowercase);
caps是一个标志,用于确定是移到大写还是移回小写。这基本上应该会在按键时移动键盘

p、
您应该使用switch case语句,而不是所有if-else语句。

从Android的软键盘示例中获得了解决方案。您可以共享一个小片段或参考代码。。我也在寻找samegot解决方案,例如Android的软键盘。你能分享一个小片段或参考代码吗。。我也在寻找同样的目标
keyboard = new Keyboard(this, R.xml.lowercase);