如何将“ e1”字符替换为“ E”,将“ e19”字符替换为“ M”?

时间:2018-12-08 08:04:05

标签: java android

我尝试从Android Studio创建键盘应用程序(而不是应用程序内键盘)。如何将文本“ e1”替换为“ E”;并替换文本“ e19”将替换“ M”吗?以下是我文件的一些内容。

MersonKeyboardd.java文件:

public class MersonKeyboardd extends InputMethodService implements KeyboardView.OnKeyboardActionListener {

private KeyboardView kv;
private Keyboard keyboard;

private boolean isCaps = false;

@Override
public View onCreateInputView() {
    kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboardd,null);
    keyboard = new Keyboard(this,R.xml.qwerty); // Add my keypad
    kv.setKeyboard(keyboard);
    kv.setOnKeyboardActionListener(this);
    return kv;
}


@Override
public void onKey(int i, int[] ints) {
    InputConnection ic = getCurrentInputConnection();
    switch (i) {

        case Keyboard.KEYCODE_DELETE:
            ic.deleteSurroundingText(1,0);
        break;

        case Keyboard.KEYCODE_SHIFT:
            isCaps = !isCaps;
            keyboard.setShifted(isCaps);
            kv.invalidateAllKeys();
            break;

         case Keyboard.KEYCODE_DONE:
             ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_ENTER));
             break;
             default:
                 char code = (char)i;
                 if (Character.isLetter(code) && isCaps)
                     code = Character.toUpperCase(code);
                 ic.commitText(String.valueOf(code),1);

                 // this is I trying replace "e1" to "E"
                 if (String.valueOf(code) == "e1") {
                     ic.commitText("E", 1);
                 }

                 // this is I trying replace "E9" to "M"
                 if (String.valueOf(code) == "E9") {
                     ic.commitText("M", 1);
                 }

                 // this is I trying replace "e19" to "M"
                 if (String.valueOf(code) == "e19") {
                     ic.commitText("M", 1);
                 }


    }

}
}

R.xml.qwerty是XML文件,其中包含带有<Keyboard> <Row> <Key>标签的设置。

我的XML文件中没有<editText>标签。我在上面的java文件中将其声明为R.xml.qwerty的代码,这就是我的 qwerty.xml 文件的内容,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyWidth="10%p"
    android:horizontalGap="5px"
    android:verticalGap="5px"
    android:keyHeight="40dp"
    >

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

    <Row>
        <Key android:keyLabel="q" android:keyEdgeFlags="left" android:codes="113" />
        <Key android:keyLabel="w" android:codes="119" />
        <Key android:keyLabel="e" android:codes="101" />
        <Key android:keyLabel="r" android:codes="114" />
        <Key android:keyLabel="t" android:codes="116" />
        <Key android:keyLabel="y" android:codes="121" />
        <Key android:keyLabel="u" android:codes="117" />
        <Key android:keyLabel="i" android:codes="105" />
        <Key android:keyLabel="o" android:codes="111" />
        <Key android:keyLabel="p" android:keyEdgeFlags="right" android:codes="112" />

    </Row>

    <Row>
        <Key android:keyLabel="a" android:keyEdgeFlags="left" android:codes="97" />
        <Key android:keyLabel="s" android:codes="115" />
        <Key android:keyLabel="d" android:codes="100" />
        <Key android:keyLabel="f" android:codes="102" />
        <Key android:keyLabel="g" android:codes="103" />
        <Key android:keyLabel="h" android:codes="104" />
        <Key android:keyLabel="j" android:codes="106" />
        <Key android:keyLabel="k" android:codes="107" />
        <Key android:keyLabel="l" android:codes="108" />
        <Key android:keyLabel="\#\@" android:keyEdgeFlags="right" android:codes="35,64" />

    </Row>

    <Row>
        <Key android:keyLabel="CAPS" android:keyEdgeFlags="left" android:codes="-1" />
        <Key android:keyLabel="z" android:codes="122" />
        <Key android:keyLabel="x" android:codes="120" />
        <Key android:keyLabel="c" android:codes="99" />
        <Key android:keyLabel="v" android:codes="118" />
        <Key android:keyLabel="b" android:codes="98" />
        <Key android:keyLabel="n" android:codes="110" />
        <Key android:keyLabel="m" android:codes="109" />
        <Key android:keyLabel="." android:codes="46" />
        <Key android:keyLabel="\?!" android:keyEdgeFlags="right" android:codes="53,33" />
    </Row>

    <Row android:rowEdgeFlags="bottom">
        <Key android:keyLabel="," android:keyWidth="10%p" android:keyEdgeFlags="left" android:codes="44" />
        <Key android:keyLabel="/" android:keyWidth="10%p" android:codes="47" />
        <Key android:keyLabel="SPACE" android:keyWidth="40%p" android:isRepeatable="true" android:codes="32" />
        <Key android:keyLabel="DEL" android:keyWidth="20%p" android:isRepeatable="true" android:codes="-5" />

        <Key android:keyLabel="DONE" android:keyWidth="20%p" android:keyEdgeFlags="right" android:codes="-4" />

    </Row>
</Keyboard>

其路径: AndroidStudioProjects/MersonKeyboard/app/src/main/res/xml/qwerty.xml

非常感谢您!

2 个答案:

答案 0 :(得分:2)

结果

输入e1 e12 e13 e14 e15 e16 e17 e18 e19 e9时,您会得到E E2 E3 E4 E5 E6 E7 E8 M E

重要的API

如评论中所述,此问题的关键在于InputConnection及其API。将文档整理一下会有些困难,但是幸运的是,API很简单。

InputConnection基本上将IME与您的应用程序链接。它提供了您想要的功能,称为组成

重要的方法是:

  • InputConnection.commitText(CharSequence, int)-将不可组合(不可编辑)的文本发送到您的应用程序。
  • InputConnection.setComposingText(CharSequence, int)-将可组合文本发送到您的应用程序。

    注意 :多次通过OnKeyboardActionListener.onKey(int, int[]),您可以搜索模式和commitText(...)或继续到setComposingText(...)
  • InputConnection.finishComposingText()-接收所有组合文本,并将其作为非组合文本发送到您的应用程序。

代码

可以随时更改变量和方法的名称。 我没有花时间思考好名字。

我添加了两个包含字符串的 class字段

private String composing = "";
private String stillComposible = "";
  • composing-保留可能组成其他文本的文本。
  • stillComposible-保存已被组合为其他文本但仍可以被组合的文本。

onKey(...)

大多数更改是在default情况下进行的。我评论了大多数主要的代码路径。

@Override
public void onKey(int i, int[] ints) {

    InputConnection ic = getCurrentInputConnection();
    char code = (char) i;
    playClick(i);

    switch (i) {

        // Added special case for deleting composed text
        case Keyboard.KEYCODE_DELETE:
            if (composing.length() == 0) {
                ic.deleteSurroundingText(1, 0);
            } else {
                composing = "";
                ic.commitText("", 0);
            }

            break;

        case Keyboard.KEYCODE_SHIFT:
            isCaps = !isCaps;
            keyboard.setShifted(isCaps);
            kv.invalidateAllKeys();
            break;

        case Keyboard.KEYCODE_DONE:
            ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
            break;

        default:
            if (Character.isLetter(code) && isCaps)
                code = Character.toUpperCase(code);

            // If code point is "e" or "E" start a new composition
            if (String.valueOf(code).toLowerCase().equals("e")) {
                if (composing.length() > 0) {   // Pass through previous text if needed
                    ic.commitText(composing, composing.length());
                }
                composing = String.valueOf(code);
                ic.setComposingText(composing, composing.length());

                // Continue composing longer text if
            } else if (composing.length() > 0) {
                composing += code;


                // Check for replacement of composition
                if (!compositionReplaced(ic)) {

                    // Replacement followed by no replacement - special case
                    if (stillComposible.length() > 0) {
                        String text = stillComposible + code;
                        ic.commitText(text, text.length());

                        // No replacement case
                    } else {
                        ic.setComposingText(composing, composing.length());
                    }
                }

                // Otherwise pass the code point through
            } else {
                composing = "";
                ic.commitText(String.valueOf(code), 1);
            }

            // No pattern matches are larger than 3 characters.
            // If nothing matched, pass the code points through.
            if (composing.length() >= 3) {
                ic.finishComposingText();
                composing = "";
            }
    }
 }

辅助方法

在这里可以更改撰写的文本。

如果需要,可以在default情况下随意将此代码放在正确的位置。

private boolean compositionReplaced(InputConnection ic) {

    boolean isReplaced = true;

    switch (composing.toLowerCase()) {

        case "e19":
            ic.commitText("M", 1);
            composing = "";
            break;

            // Can be composed more
        case "e1":
            ic.setComposingText("E", 1);
            stillComposible = "E";
            break;

        case "e9":
            ic.commitText("E", 1);
            composing = "";
            break;

        // No replacement occur
        default:
            isReplaced = false;
            break;
    }
    return isReplaced;
}

注释

这仅解决您的主要问题。在实际应用中,要想完全发挥功能,还需要解决更多问题。

答案 1 :(得分:0)

我认为您想要的是即时编辑editText字段, 例如。

EditText et;
//.......
et.addTextChangedListener(new TextWatcher() {
   //.....
   @Override
   public void afterTextChanged(Editable editable) {

    String text = editable.toString();                
    Pattern pattern = Pattern.compile("e19");
    Matcher matcher = pattern.matcher(text);
    String replaceAll = matcher.replaceAll("M");

    pattern = Pattern.compile("e1");
    matcher = pattern.matcher(replaceAll);
    replaceAll = matcher.replaceAll("E");
    et.setText(replaceAll);

   }
 )};