gpt4 book ai didi

android - 按下自定义键盘的shift按钮时如何输入大写字母?

转载 作者:行者123 更新时间:2023-11-30 00:10:24 26 4
gpt4 key购买 nike

我有一种显示键盘键的布局,例如 a-z、0-9、enter、delete 和 shift(截至目前)。我想根据 shift 键状态将大写字母或小写字母写入 editText 或其他系统/应用程序组件。

问题是当我按下 shift 键时,键盘键使用 keyboardView.setShifted(true); 语句变成大写或小写。 但 editText 一直在小写字母上书写。

基本上我希望当用户按下 shift 键时,任何字母/键都会以大写形式书写,反之亦然。

我刚开始学习制作自定义键盘,所以欢迎大家提出建议。

this is what i have now

这里是键盘 View 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);

}
}

最佳答案

好的。经过 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

关于android - 按下自定义键盘的shift按钮时如何输入大写字母?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48237124/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com