如何在按下自定义键盘的移位按钮时输入大写字母?

时间:2018-01-13 05:15:40

标签: android keyboard

我有一个显示键盘键的布局,如a-z,0-9,输入,删除和移位(截至目前)。我想根据shift键状态将大写字母或小写字母写入editText或其他系统/ app组件。

问题是,当我按下shift键键盘键时,使用keyboardView.setShifted(true);语句变为大写或小写。但editText会继续以小写字母书写。

基本上我想要当用户按下shift键时,任何字母/键都会写在大写字母上,反之亦然。

我刚刚开始学会制作自定义键盘,所以欢迎每一个建议。

this is what i have now

这是键盘视图的onKey的当前代码。

@Override
    public void onKey(int primarycode, int[] codes) {
        InputConnection inputConnection = getCurrentInputConnection();
        Log.i("KEYCODE", "" + primarycode);
        if (primarycode == -1) {
            if (isupper) {
                keyboardView.setShifted(true);
                Log.i("KEYCODE_SHIFT","Current status "+isupper);
            } else {
                keyboardView.setShifted(false);
                Log.i("KEYCODE_SHIFT","ELSE Current status "+isupper);
            }
        }

        if (inputConnection != null) {

            Log.i("CustomKeyboardKey", "KEY IS " + codes[1]);

            if(primarycode==10){
                Log.i("KEYCODE_DONE","Done Button Pressed ");
            }
            switch (primarycode) {
                case Keyboard.KEYCODE_DELETE:
                    CharSequence selectedText = inputConnection.getSelectedText(0);
                    if (TextUtils.isEmpty(selectedText)) {
                        inputConnection.deleteSurroundingText(1, 0);
                    } else {
                        inputConnection.commitText("", 1);
                    }

                    break;
                case Keyboard.KEYCODE_DONE:
                    Log.i("KEYCODE_DONE","Done Button Pressed ");
                case Keyboard.KEYCODE_SHIFT:
                    isupper = !isupper;

                    break;
                default:
                    char chars = (char) primarycode;
                    inputConnection.commitText(String.valueOf(chars), 1);
            }
            AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
            Vibrator vb = (Vibrator) getSystemService(VIBRATOR_SERVICE);
            vb.vibrate(25);
            am.playSoundEffect(AudioManager.FX_KEY_CLICK, 0.5f);

        }
    }

1 个答案:

答案 0 :(得分:1)

好。经过2个小时的工作,我得到了我想要的东西。所以我可以和你分享。

基本上我们需要两个布局

1)小写。

2)大写。

这是lowercase.xml文件代码的代码。

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyWidth="25%p"
    android:keyHeight="60dp">
    <Row android:keyWidth="10%p">
        <Key android:codes="48"  android:keyLabel="0" android:keyEdgeFlags="left"/>
        <Key android:codes="49" android:keyLabel="1"/>
        <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" android:keyEdgeFlags="right"/>
    </Row>

    <Row android:keyWidth="10%p">
        <Key android:codes="113"    android:keyLabel="q" />
        <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" />
    </Row>
    <Row android:keyWidth="11%p">
        <Key android:codes="97" android:keyLabel="a" android:keyEdgeFlags="left" />
        <Key android:codes="115" android:isRepeatable="true" android:popupCharacters="S"   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" />
    </Row>
    <Row android:keyWidth="12.5%p">
        <Key android:codes="-1"   android:keyLabel="Shift"   android:keyEdgeFlags="left"/>
        <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:keyEdgeFlags="right" android:codes="109"    android:keyLabel="m" />
    </Row>
    <Row android:keyWidth="20%p">
        <Key android:codes="-5"
            android:keyLabel="Delete"
            android:keyEdgeFlags="left"
            android:isRepeatable="true"
            />
        <Key android:codes="32" android:keyLabel="Space bar"
            android:keyWidth="60%p" android:isRepeatable="true"/>
        <Key android:codes="10"
            android:keyLabel="Enter"
            android:keyEdgeFlags="right"
            />
    </Row>
</Keyboard>

这里是UPPERCASE.XML(实际上你需要以小写形式写文件名)

 <?xml version="1.0" encoding="utf-8"?>
    <Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
        android:keyWidth="25%p"
        android:keyHeight="60dp">
        <Row android:keyWidth="10%p">
            <Key android:codes="48"  android:keyLabel="0" android:keyEdgeFlags="left"/>
            <Key android:codes="49" android:keyLabel="1"/>
            <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" android:keyEdgeFlags="right"/>
        </Row>

        <Row android:keyWidth="10%p">
            <Key android:codes="81"    android:keyLabel="Q" />
            <Key android:codes="87"    android:keyLabel="W" />
            <Key android:codes="69"    android:keyLabel="E" />
            <Key android:codes="82"    android:keyLabel="R" />
            <Key android:codes="84"    android:keyLabel="T" />
            <Key android:codes="89"    android:keyLabel="Y" />
            <Key android:codes="85"    android:keyLabel="U" />
            <Key android:codes="73"    android:keyLabel="I"  />
            <Key android:codes="79"    android:keyLabel="O"  />
            <Key android:codes="80"    android:keyLabel="P" />
        </Row>
        <Row android:keyWidth="11%p">
            <Key android:codes="65" android:keyLabel="A" android:keyEdgeFlags="left" />
            <Key android:codes="83" android:isRepeatable="true"    android:keyLabel="S" />
            <Key android:codes="68"    android:keyLabel="D" />
            <Key android:codes="70"    android:keyLabel="F" />
            <Key android:codes="71"    android:keyLabel="G" />
            <Key android:codes="72"    android:keyLabel="H" />
            <Key android:codes="75"    android:keyLabel="J" />
            <Key android:codes="76"    android:keyLabel="K" />
            <Key android:codes="77"    android:keyLabel="L" android:keyEdgeFlags="right" />
        </Row>
        <Row android:keyWidth="12.5%p">
            <Key android:codes="-1"   android:keyLabel="Shift"   android:keyEdgeFlags="left"/>
            <Key android:codes="90"  android:keyLabel="Z" />
            <Key android:codes="88" android:keyLabel="x" />
            <Key android:codes="67"   android:keyLabel="c" />
            <Key android:codes="86"  android:keyLabel="v" />
            <Key android:codes="66"     android:keyLabel="b" />
            <Key

 android:codes="78"   android:keyLabel="n" />
        <Key android:keyEdgeFlags="right" android:codes="77"    android:keyLabel="m" />
    </Row>
    <Row android:keyWidth="20%p">
        <Key android:codes="-5"
            android:keyLabel="Delete"
            android:keyEdgeFlags="left"
            android:isRepeatable="true"
            />
        <Key android:codes="32" android:keyLabel="Space bar"
            android:keyWidth="60%p" android:isRepeatable="true"/>
        <Key android:codes="10"
            android:keyLabel="Enter"
            android:keyEdgeFlags="right"
            />
    </Row>
</Keyboard>

这是您需要的实际代码文件。

import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.media.AudioManager;
import android.os.Vibrator;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.widget.Toast;

import java.util.List;

/**
 * Created by adMin on 1/12/2018.
 */

public class MyInput extends InputMethodService implements KeyboardView.OnKeyboardActionListener {
    KeyboardView keyboardView;
    boolean isupper;
    Keyboard keyboard;
    @Override
    public View onCreateInputView() {

        keyboardView = (KeyboardView) getLayoutInflater().inflate(R.layout.keyboard_view, null);
     keyboard = new Keyboard(this, R.xml.number_pad);
        keyboardView.setKeyboard(keyboard);
        keyboardView.setPreviewEnabled(true);
        keyboardView.setOnKeyboardActionListener(this);
        return keyboardView;

    }

    @Override
    public void onPress(int i) {
        Log.i("onPress", "on keyboard pressed " + i);
        InputConnection inputConnection=getCurrentInputConnection();

    }

    @Override
    public void onRelease(int i) {

    }

    @Override
    public void onStartInputView(EditorInfo info, boolean restarting) {
        super.onStartInputView(info, restarting);
        Log.i("OnStartInputView", "Keyboard Status " + restarting);
    }

    @Override
    public void onKey(int primarycode, int[] codes) {
        InputConnection inputConnection = getCurrentInputConnection();
        Log.i("KEYCODE", "" + primarycode);


        if (inputConnection != null) {

            Log.i("CustomKeyboardKey", "KEY IS " + codes[1]);

            if (primarycode == 10) {
                Log.i("KEYCODE_DONE", "Done Button Pressed ");
            }
            switch (primarycode) {
                case Keyboard.KEYCODE_DELETE:
                    CharSequence selectedText = inputConnection.getSelectedText(0);
                    if (TextUtils.isEmpty(selectedText)) {
                        inputConnection.deleteSurroundingText(1, 0);
                    } else {
                        inputConnection.commitText("", 1);
                    }

                    break;
                case Keyboard.KEYCODE_DONE:
                    Log.i("KEYCODE_DONE", "Done Button Pressed ");

           /**real code is here */         
                case Keyboard.KEYCODE_SHIFT:
                    //toggle the flag for determine lowercase and uppercase.
                    isupper = !isupper;
                    if (isupper) {
                        //inflate uppercase.xml file
                        keyboard=new Keyboard(this, R.xml.uppercase);
                        //set the uppercase keyboard
                        keyboardView.setKeyboard(keyboard);                        
                        keyboardView.setShifted(true);
                        Log.i("KEYCODE_SHIFT", "Current status " + isupper);
                        keyboardView.invalidateAllKeys();
                    } else {

                        keyboard=new Keyboard(this, R.xml.number_pad);
                        keyboardView.setKeyboard(keyboard);
                        keyboardView.setShifted(false);
                        Log.i("KEYCODE_SHIFT", "ELSE Current status " + isupper);
                        keyboardView.invalidateAllKeys();
                    }

                    break;
                default:
                    char chars = (char) primarycode;
                    inputConnection.commitText(String.valueOf(chars), 1);
            }
            AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
            Vibrator vb = (Vibrator) getSystemService(VIBRATOR_SERVICE);
            vb.vibrate(25);
            am.playSoundEffect(AudioManager.FX_KEY_CLICK, 0.5f);

        }
    }

    @Override
    public void onText(CharSequence charSequence) {

    }

    @Override
    public void swipeLeft() {

    }

    @Override
    public void swipeRight() {

    }

    @Override
    public void swipeDown() {

    }

    @Override
    public void swipeUp() {

    }
}

这是你得到的输出。

lowercase screenshot

uppercase screenshot