自定义键盘的布局及实现

步骤:

1.在res下新建xml文件夹,在xml文件夹中新建.xml文件,用来实现软键盘的布局

2.标签Keyboard代表是软键盘,标签Row代表的是一行,Key代表的是一个按键

3.每一个按键都有一个codes值,在类中就是通过codes的值来监听每一个按钮。keylabel的值是键盘所要显示的文字


注:

Keyborad:说明是一个软键盘定义文件

Row:说明是一行按键的定义

Key:说明是一个按键的定义

Codes:代表按键对应的输出值

keyLabel:代表按键显示的文本内容

keyIcon:代表按键显示的图标内容,如果指定了该值则在显示的时候显示为图片不显示文本

keyWidth:代表按键的宽度,可以为精确值或者相对值,对于精确值支持多种单位,例如:像素、英寸等。相对值为百分比,以%或%p结尾,其中%p表示相对于父容器。

keyHeight:代表按键的高度,取值同上

horizontalGap:代表按键前的间隙(水平方向),取值同上

isSticky:指定按键是否为sticky。如Shift大小写切换按键,具有两种状态,按下状态和正常状态,取值为true或者false

isModifier:指定按键是否为功能键,如Alt或者Shift,取值为true或false

keyOutputText:指按键输出的文本内容,取值为字符串

isRepeatable:指按键是否可是否是可重复的,如故长按该键可以触发重复按键事件则为true,否则为false

keyEdgeFlags"指定按键的对齐指令,取值为left或right



加载布局

   this.keyboardView = keyboardView1;
        keyboardView.setOnKeyboardActionListener(listener);
        this.myImeService = myImeService1;
        k1 = new Keyboard(myImeService.getApplicationContext(), R.xml.chinese);
        keyboardView.setKeyboard(k1);
        keyboardView.setEnabled(true);
        keyboardView.setPreviewEnabled(true);


设置监听,并处理事件

 private OnKeyboardActionListener listener = new OnKeyboardActionListener() {

        @Override
        public void swipeUp() {
        }

        @Override
        public void swipeRight() {
        }

        @Override
        public void swipeLeft() {
        }

        @Override
        public void swipeDown() {
        }

        @Override
        public void onText(CharSequence text) {
        }

        @Override
        public void onRelease(int primaryCode) {
        }

        @Override
        public void onPress(int primaryCode) {
        }

        @Override
        public void onKey(int primaryCode, int[] keyCodes) {
            InputConnection ic = myImeService.getCurrentInputConnection();
            switch (primaryCode) {
                case Keyboard.KEYCODE_DELETE:
//				myImeService.deleteText();
                    ic.deleteSurroundingText(1, 0);
                    break;
                case Keyboard.KEYCODE_CANCEL:
//				myImeService.hideInputMethod();
                    break;
                case Keyboard.KEYCODE_DONE:
                    ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_NUMPAD_ENTER));
                    break;
                default:
//				myImeService.commitText(Character.toString((char) primaryCode));
                    ic.commitText(String.valueOf((char) primaryCode), 1);
                    break;
            }
        }
    };




一、字母键盘

<?xml version="1.0" encoding="UTF-8"?>
<Keyboard android:keyWidth="10.000002%p"
	android:keyHeight="@dimen/key_height"
	android:horizontalGap="0.0px"
	android:verticalGap="0.0px"
	xmlns:android="http://schemas.android.com/apk/res/android">
	<Row>
		<Key android:codes="113" android:keyEdgeFlags="left"
			android:keyLabel="q" />
		<Key android:codes="119" android:keyLabel="ww" />
		<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:keyEdgeFlags="right"
			android:keyLabel="p" />
	</Row>
	<Row>
		<Key android:horizontalGap="4.999995%p" android:codes="97"
			android:keyEdgeFlags="left" 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:keyEdgeFlags="right"
			android:keyLabel="l" />
	</Row>
	<Row>
		<Key android:keyWidth="14.999998%p" android:codes="-1"
			android:keyEdgeFlags="left" android:isModifier="true"
			android:isSticky="true" android:keyIcon="@drawable/sym_keyboard_shift" />
		<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:keyWidth="14.999998%p" android:codes="-5"
			android:keyEdgeFlags="right" android:isRepeatable="true"
			android:keyIcon="@drawable/sym_keyboard_delete" />
	</Row>
	<Row android:rowEdgeFlags="bottom">
		<Key android:keyWidth="20.000004%p" android:codes="-2"
			android:keyLabel="12#" />
		<Key android:keyWidth="14.999998%p" android:codes="44"
			android:keyLabel="," />
		<Key android:keyWidth="29.999996%p" android:codes="32"
			android:isRepeatable="true" android:keyIcon="@drawable/sym_keyboard_space" />
		<Key android:keyWidth="14.999998%p" android:codes="46"
			android:keyLabel="." />
		<Key android:keyWidth="20.000004%p" android:codes="-3"
			android:keyEdgeFlags="right" android:keyLabel="完成" />
	</Row>
</Keyboard>

运行后的结果:



二、数字键盘

<?xml version="1.0" encoding="UTF-8"?>
<Keyboard android:keyWidth="20%p"
	android:keyHeight="@dimen/key_height"
	android:horizontalGap="0.0px"
	android:verticalGap="0.0px"
	xmlns:android="http://schemas.android.com/apk/res/android">
	<Row>
		<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="-5" android:keyIcon="@drawable/sym_keyboard_delete" />
	</Row>

	<Row>
		<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="-2" android:keyLabel="中文" />
	</Row>

	<Row>
		<Key android:codes="57" android:keyLabel="9" />
		<Key android:codes="48" android:keyLabel="0" />
		<Key android:codes="46" android:keyLabel="." />
		<Key android:codes="-3" android:keyLabel="完成"
			android:keyWidth="40%p" android:isRepeatable="true" />
	</Row>

</Keyboard>


运行后的结果:


三、中文键盘

<?xml version="1.0" encoding="UTF-8"?>
<Keyboard android:keyWidth="20%p"
	android:keyHeight="@dimen/key_height"
	android:horizontalGap="0.0px"
	android:verticalGap="0.0px"
	xmlns:android="http://schemas.android.com/apk/res/android">
	<Row>
		<Key android:codes="19968" android:keyLabel="一" />
		<Key android:codes="20108" android:keyLabel="二" />
		<Key android:codes="19977" android:keyLabel="三" />
		<Key android:codes="-5" android:keyIcon="@drawable/sym_keyboard_delete"
			android:keyWidth="40%p" android:isRepeatable="true"/>
	</Row>

	<Row>
		<Key android:codes="20116" android:keyLabel="五" />
		<Key android:codes="20845" android:keyLabel="六" />
		<Key android:codes="22235" android:keyLabel="四" />
		<Key android:codes="-2" android:keyLabel="數字"
			android:keyWidth="40%p" android:isRepeatable="true"/>
	</Row>

</Keyboard>


运行后的结果:











注:参考资料:

http://www.jianshu.com/p/23fc9663358f

http://www.10tiao.com/html/169/201703/2650822379/1.html

http://blog.csdn.net/acrambler/article/details/13213181

http://blog.csdn.net/hfsu0419/article/details/7924673

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在 Android 上实现自定义键盘,可以通过创建一个自定义视图并将其添加到所需的布局中来实现。要在键盘出现时将布局顶起,可以使用 Android 中的软键盘监听器。以下是一些步骤来实现这个功能: 1. 创建一个自定义视图来表示您的键盘布局。您可以使用 Android 自带的键盘布局或创建自己的布局。 2. 在您的布局文件中添加一个占位符视图,将其放置在您希望键盘出现的位置。 3. 在您的活动或碎片中,实现 ViewTreeObserver.OnGlobalLayoutListener 接口以侦听布局的全局变化。 4. 在 onGlobalLayout() 方法中,检查键盘是否正在显示,并在需要时更新布局的大小和位置。 5. 当键盘出现时,将占位符视图顶起,以便键盘不会覆盖您的布局。 以下是一个简单的示例代码,展示了如何实现这个功能: ``` public class MainActivity extends AppCompatActivity implements ViewTreeObserver.OnGlobalLayoutListener { private View mPlaceholderView; private View mKeyboardView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Find the placeholder view and keyboard view mPlaceholderView = findViewById(R.id.placeholder_view); mKeyboardView = findViewById(R.id.keyboard_view); // Add a global layout listener to detect changes in the layout mPlaceholderView.getViewTreeObserver().addOnGlobalLayoutListener(this); } @Override public void onGlobalLayout() { Rect r = new Rect(); mPlaceholderView.getWindowVisibleDisplayFrame(r); int heightDiff = mPlaceholderView.getRootView().getHeight() - (r.bottom - r.top); if (heightDiff > 100) { // arbitrary value // Keyboard is showing, adjust the layout ViewGroup.LayoutParams params = mKeyboardView.getLayoutParams(); params.height = heightDiff; mKeyboardView.setLayoutParams(params); // Move the placeholder view up mPlaceholderView.setTranslationY(-heightDiff); } else { // Keyboard is not showing, reset the layout ViewGroup.LayoutParams params = mKeyboardView.getLayoutParams(); params.height = 0; mKeyboardView.setLayoutParams(params); // Move the placeholder view back down mPlaceholderView.setTranslationY(0); } } } ``` 在这个例子中,我们假设您在布局文件中定义了一个占位符视图(id 为 placeholder_view),并将其放置在您希望键盘出现的位置。我们还假设您已经创建了一个自定义视图来表示您的键盘布局(id 为 keyboard_view)。 在 onCreate() 方法中,我们获取了占位符视图和键盘视图的引用,并添加了一个全局布局监听器。在 onGlobalLayout() 方法中,我们检查键盘是否正在显示,并相应地更新布局的大小和位置。如果键盘正在显示,我们将键盘视图的高度设置为键盘的高度,并将占位符视图向上移动,以便键盘不会覆盖布局。如果键盘没有显示,我们将键盘视图的高度重置为零,并将占位符视图向下移动,使布局恢复原来的位置。 请注意,这只是一个简单的示例代码,您可以根据自己的需求进行修改。另外,由于 Android 上的软键盘行为因设备而异,因此您可能需要进行一些额外的测试和调试才能获得最佳体验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值