Android 自定义键盘布局

先上效果图

效果一
在这里插入图片描述

效果二

在这里插入图片描述
效果三
在这里插入图片描述

一:代码如下:

<Keyboard android:keyWidth="24.9%p"
    android:keyHeight="49dp"
    android:horizontalGap="1%p"
    android:verticalGap="5px"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <Row android:keyWidth="23%p">
        <Key android:codes="49" android:keyEdgeFlags="left" android:keyLabel="1" />
        <Key android:codes="50" android:keyLabel="2" />
        <Key android:codes="51" android:keyLabel="3" />
        <Key android:codes="-5" android:keyIcon="@mipmap/ic_delete" android:isRepeatable="true"/>
    </Row>
    <Row android:keyWidth="23%p">
        <Key  android:codes="52" android:keyLabel="4"/>
        <Key android:codes="53" android:keyLabel="5" />
        <Key android:codes="54" android:keyLabel="6" />
        <Key android:codes="\u5b8c\u6210" android:keyHeight="19%p" android:keyLabel="确定" />
    </Row>
    <Row android:keyWidth="23%p" >
        <Key android:codes="55" android:keyEdgeFlags="left" android:keyLabel="7" />
        <Key android:codes="56" android:keyLabel="8" />
        <Key android:codes="57" android:keyLabel="9" />
    </Row>
    <Row>
        <Key android:codes="48" android:keyLabel="0" android:keyEdgeFlags="left" android:keyWidth="47%p" />
        <Key android:codes="46" android:keyLabel="." android:keyWidth="23%p" />
    </Row>
</Keyboard>

二:代码如下:

<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
   android:horizontalGap="0px"
   android:verticalGap="0px"
   android:keyHeight="6.5%p"
   android:keyWidth="29.2%p"
   >
   <Row android:verticalGap="1%p">
       <Key android:codes="49" android:keyLabel="1" android:horizontalGap="2%p"/>
       <Key android:codes="50" android:keyLabel="2" android:horizontalGap="2%p"/>
       <Key android:codes="51" android:keyLabel="3" android:horizontalGap="2%p"/>

   </Row>
   <Row android:verticalGap="1%p">
       <Key  android:codes="52" android:keyLabel="4" android:horizontalGap="2%p"/>
       <Key android:codes="53" android:keyLabel="5"  android:horizontalGap="2%p"/>
       <Key android:codes="54" android:keyLabel="6"  android:horizontalGap="2%p"/>
   </Row>
   <Row android:verticalGap="1%p" >
       <Key android:codes="55" android:keyLabel="7"  android:horizontalGap="2%p"/>
       <Key android:codes="56" android:keyLabel="8"  android:horizontalGap="2%p"/>
       <Key android:codes="57" android:keyLabel="9"  android:horizontalGap="2%p"/>
   </Row>
   <Row android:verticalGap="1%p" >
       <Key android:codes="48" android:keyLabel="0"  android:keyWidth="60%p" android:horizontalGap="2%p" />
       <Key android:codes="-5" android:isRepeatable="true" android:keyIcon="@mipmap/ic_delete" android:horizontalGap="2%p"/>
   </Row>
   </Keyboard>

三:代码如下:

<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:horizontalGap="0px"
    android:verticalGap="0px"
    android:keyHeight="6.5%p"
    android:keyWidth="29.2%p"
    >

   <Row android:verticalGap="1%p">
        <Key android:codes="49" android:keyLabel="1" android:horizontalGap="2%p"/>
        <Key android:codes="50" android:keyLabel="2" android:horizontalGap="2%p"/>
        <Key android:codes="51" android:keyLabel="3" android:horizontalGap="2%p"/>
        <Key android:codes="-5" android:isRepeatable="true" android:keyIcon="@mipmap/ic_delete" android:horizontalGap="2%p"/>
    </Row>
    <Row android:verticalGap="1%p">
        <Key  android:codes="52" android:keyLabel="4" android:horizontalGap="2%p"/>
        <Key android:codes="53" android:keyLabel="5"  android:horizontalGap="2%p"/>
        <Key android:codes="54" android:keyLabel="6"  android:horizontalGap="2%p"/>
        <Key android:codes="-3" android:isRepeatable="true"  android:keyLabel="完成"  android:horizontalGap="2%p" />
        &lt;!&ndash;android:keyHeight="20%p"&ndash;&gt;
    </Row>
    <Row android:verticalGap="1%p">
        <Key android:codes="55" android:keyLabel="7"  android:horizontalGap="2%p"/>
        <Key android:codes="56" android:keyLabel="8"  android:horizontalGap="2%p"/>
        <Key android:codes="57" android:keyLabel="9"  android:horizontalGap="2%p"/>
    </Row>
    <Row android:verticalGap="1%p" >
        <Key android:codes="48" android:keyLabel="0"  android:keyWidth="60%p" android:horizontalGap="2%p" />
        <Key android:codes="46" android:keyLabel="."  android:horizontalGap="2%p"/>
    </Row>
    </Keyboard>

以上是自定义键盘的三种效果

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在 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 上的软键盘行为因设备而异,因此您可能需要进行一些额外的测试和调试才能获得最佳体验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AaVictory.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值