android自动软键盘,Android自定义软键盘

MyKeyboard

Android自定义键盘的使用

实现步骤

第一步:

1、新建一个xml文件夹放在res目录下面,然后新建xml文件:money_keyboard.xml

2、然后在XML文件中添加按钮布局,这个布局就是键盘的样子了

android:horizontalGap="1dp"

android:keyWidth="33.33333%p"

android:keyHeight="10%p"

android:verticalGap="1dp">

android:codes="49"

android:keyLabel="1" />

android:codes="50"

android:keyLabel="2" />

android:codes="51"

android:keyEdgeFlags="right"

android:keyLabel="3" />

android:codes="52"

android:keyLabel="4" />

android:codes="53"

android:keyLabel="5" />

android:codes="54"

android:keyEdgeFlags="right"

android:keyLabel="6" />

android:codes="55"

android:keyLabel="7" />

android:codes="56"

android:keyLabel="8" />

android:codes="57"

android:keyEdgeFlags="right"

android:keyLabel="9" />

android:codes="46"

android:keyLabel="." />

android:codes="48"

android:keyLabel="0" />

android:codes="-5"

android:keyEdgeFlags="right"

android:keyIcon="@drawable/sym_keyboard_delete"

android:keyLabel="aa" />

3 属性介绍:

Keyboard:

存储键盘以及按键相关信息。

android:horizontalGap

按键之间默认的水平间距。

android:verticalGap

按键之间默认的垂直间距。

android:keyHeight

按键的默认高度,以像素或显示高度的百分比表示。

android:keyWidth:

按键的默认宽度,以像素或显示宽度的百分比表示。

Row:

为包含按键的容器。

Key:

用于描述键盘中单个键的位置和特性。

android:codes

该键输出的unicode值。

android:codes 官网介绍是说这个是该键的unicode 值或者逗号分隔值,当然我们也可以设置成我们想要的值,在源码中提供了几个特定的值

对照表:

836002402e87

image.png

public static final int KEYCODE_SHIFT = -1;

public static final int KEYCODE_MODE_CHANGE = -2;

public static final int KEYCODE_CANCEL = -3;

public static final int KEYCODE_DONE = -4;

public static final int KEYCODE_DELETE = -5;

public static final int KEYCODE_ALT = -6;

android:isRepeatable

这个属性如果设置为true,那么当长按该键时就会重复接受到该键上的动作,在 删除键键 和 空格键 上通常设为true。

android:keyLabel

显示在按键上的文字。

android:keyIcon与keyLabel

是二选一关系,它会代替文字以图标的形式显示在键上。

android:keyWidth="33.33333%p"

每一个按钮的宽度,可以设置百分比

android:keyHeight="10%p"

每一个按钮高度,可以设置百分比

第二步:布局文件引用KeyBoradView.

android:id="@+id/keyboard_view"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_marginTop="20dp"

android:background="#d8d8d8"

android:focusable="true"

android:focusableInTouchMode="true"

android:keyBackground="@drawable/bg_keyboard_btn"

android:keyTextColor="#333333"

android:paddingTop="1dp"

android:shadowColor="#ffffff"

android:shadowRadius="0.0"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintRight_toRightOf="parent"

app:layout_constraintTop_toBottomOf="@id/et_amount" />

KeyboardView是一个渲染虚拟键盘的View。 它处理键的渲染和检测按键和触摸动作。

显然我们需要KeyboardView来对Keyboard里的数据进行渲染并呈现给我们以及相关的点击事件做处理。 1)//设置keyboard与KeyboardView相关联的方法。

public void setKeyboard(Keyboard keyboard)

2)//设置虚拟键盘事件的监听,此方法必须设置,不然会报错。

public void setOnKeyboardActionListener(OnKeyboardActionListener listener) 步骤上呢,做完第一步的关联,并设置第二步的事件,调用KeyboardView.setVisible(true);键盘就可以显示出来了, 是不是很简单。不过到这里还没有结束哦,接下来我们为了使用上的便利要进行相应的封装。 封装 这里我们通过继承EditText来对Keyboard与KeyboardView进行封装。

attr.xml文件,这里我们需要通过一个xml类型的自定义属性引入我们的键盘描述文件。

三、实例化KeyBoradView给其设置KeyBorad,以及OnKeyboardActionListener事件监听。

1、新建一个类,我取名叫KeyUtils然后在里面新建三个属性。KeyBoard用处可大了,他才是本体,可以通过设置他来切换键盘。

2、构造函数,初始下三个参数。

3、先说下预览图吧,就是效果图上的预览图,需要预览图的话的将setPreviewEnabled设置为true,不过还得在布局文件中的android.inputmethodservice.KeyboardView标签对立面设置预览布局。否则,不会有字。至于设置的布局,一个TextView就好了~

onPress: 按下触发。

onRelease:松开触发。

onKey : 松开触发,在OnRelease之前触发。

swipeLeft : 左滑动,其他同理。哈哈~就这么懒。

onText :需要在 键盘xml,也就是我此时的number.xml里面中key标签对里添加一个

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 中,自定义键盘需要实现一个继承自 InputMethodService 的服务,这个服务会在用户打开键盘时被调用。下面是一些步骤来创建自定义键盘: 1. 创建一个新的 Android 项目,并在 AndroidManifest.xml 文件中声明一个新的服务: ```xml <service android:name=".CustomKeyboard" android:label="Custom Keyboard" android:permission="android.permission.BIND_INPUT_METHOD"> <meta-data android:name="android.view.im" android:resource="@xml/method" /> </service> ``` 上面的代码声明了一个名为 CustomKeyboard 的服务,并将其与 android.view.im 绑定。在 res/xml 目录下创建一个名为 method.xml 的文件,用于指定 CustomKeyboard 的布局和行为: ```xml <?xml version="1.0" encoding="utf-8"?> <input-method xmlns:android="http://schemas.android.com/apk/res/android" android:settingsActivity=".SettingsActivity" android:imeSubtypeLocale="en_US" android:imeSubtypeMode="keyboard" > </input-method> ``` 上面的代码指定了键盘的设置活动、语言环境和子类型模式。 2. 创建 CustomKeyboard 类,并继承 InputMethodService。在这个类中,你需要重写一些回调方法,例如 onCreateInputView()、onKeyDown() 和 onStartInputView() 等。这些方法将决定键盘的外观和行为。 ```java public class CustomKeyboard extends InputMethodService implements KeyboardView.OnKeyboardActionListener { private KeyboardView keyboardView; private Keyboard keyboard; @Override public View onCreateInputView() { keyboardView = (KeyboardView) getLayoutInflater().inflate(R.layout.keyboard, null); keyboard = new Keyboard(this, R.xml.qwerty); keyboardView.setKeyboard(keyboard); keyboardView.setOnKeyboardActionListener(this); return keyboardView; } @Override public void onStartInputView(EditorInfo info, boolean restarting) { super.onStartInputView(info, restarting); keyboardView.setPreviewEnabled(false); } @Override public void onKey(int primaryCode, int[] keyCodes) { InputConnection ic = getCurrentInputConnection(); switch (primaryCode) { case Keyboard.KEYCODE_DELETE: ic.deleteSurroundingText(1, 0); break; case Keyboard.KEYCODE_SHIFT: // do something break; default: char c = (char) primaryCode; ic.commitText(String.valueOf(c), 1); } } } ``` 上面的代码创建了一个名为 CustomKeyboard 的类,并在 onCreateInputView() 方法中设置了键盘的布局和行为。在 onStartInputView() 方法中,我们禁用了键盘预览功能。在 onKey() 方法中,我们检查按下的键码并执行相应的操作。 3. 创建键盘布局。在 res/xml 目录下创建一个名为 qwerty.xml 的文件,用于指定键盘布局: ```xml <?xml version="1.0" encoding="utf-8"?> <Keyboard xmlns:android="http://schemas.android.com/apk/res/android" android:keyWidth="10%p" android:keyHeight="60dp" android:horizontalGap="0px" android:verticalGap="0px" android:keyEdgeFlags="left"> <Row> <Key android:keyLabel="q" android:keyEdgeFlags="left"/> <Key android:keyLabel="w"/> <Key android:keyLabel="e"/> <Key android:keyLabel="r"/> <Key android:keyLabel="t"/> <Key android:keyLabel="y"/> <Key android:keyLabel="u"/> <Key android:keyLabel="i"/> <Key android:keyLabel="o"/> <Key android:keyLabel="p" android:keyEdgeFlags="right"/> </Row> <Row> <Key android:keyLabel="a" android:keyEdgeFlags="left"/> <Key android:keyLabel="s"/> <Key android:keyLabel="d"/> <Key android:keyLabel="f"/> <Key android:keyLabel="g"/> <Key android:keyLabel="h"/> <Key android:keyLabel="j"/> <Key android:keyLabel="k"/> <Key android:keyLabel="l android:keyEdgeFlags="right"/> </Row> <Row> <Key android:keyLabel="shift" android:horizontalGap="10%p" android:keyWidth="20%p" android:keyEdgeFlags="left" android:isModifier="true" android:isSticky="true"/> <Key android:keyLabel="z"/> <Key android:keyLabel=""/> <Key android:keyLabel="c"/> <Key android:keyLabel="v"/> <Key android:keyLabel="b"/> <Key android:keyLabel="n"/> <Key android:keyLabel="m"/> <Key android:keyLabel="delete" android:keyWidth="20%p" android:keyEdgeFlags="right" android:icon="@drawable/ic_delete"/> </Row> <Row> <Key android:keyLabel="123" android:keyEdgeFlags="left" android:keyWidth="20%p"/> <Key android:keyLabel=" " android:keyWidth="40%p"/> <Key android:keyLabel="return" android:keyWidth="20%p" android:keyEdgeFlags="right"/> </Row> </Keyboard> ``` 上面的代码指定了一个基本的 QWERTY 键盘布局,包含字母、数字和删除键。 4. 运行应用程序并测试自定义键盘。在测试键盘时,你需要在 Android 设备的输入法设置中激活你的自定义键盘。 以上就是创建自定义键盘的基本步骤,你可以根据需要修改键盘的布局和行为。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值