如何实现特殊软键盘

16

我希望制作一个特殊的软键盘,用于我的安卓应用程序,就像下面这样:

enter image description here

我已经在软件键盘示例上进行了工作,并编辑了qwerty.xml文件。

<?xml version="1.0" encoding="utf-8"?>
<!--
/* 
**
** Copyright 2008, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License"); 
** you may not use this file except in compliance with the License. 
** You may obtain a copy of the License at 
**
**     http://www.apache.org/licenses/LICENSE-2.0 
**
** Unless required by applicable law or agreed to in writing, software 
** distributed under the License is distributed on an "AS IS" BASIS, 
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
** See the License for the specific language governing permissions and 
** limitations under the License.
*/
-->

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


     <Row   android:keyWidth="18%p">
        <Key android:codes="49" android:keyLabel="1" android:keyEdgeFlags="left"/>
        <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"/>
        <Key android:codes="48" android:keyLabel="0" android:keyEdgeFlags="right"/>
        <Key android:codes="-5" android:keyIcon="@drawable/sym_keyboard_delete" 
                android:keyWidth="15%p" android:keyEdgeFlags="right"
                android:isRepeatable="true"/>
    </Row>
    <Row   >
        <Key android:codes="113" android:keyLabel="Q" android:keyEdgeFlags="left"/>
        <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" android:keyEdgeFlags="right"/>
    </Row>

    <Row    >
        <Key android:codes="97" android:keyLabel="A" android:horizontalGap="5%p" 
                android:keyEdgeFlags="left"/>
        <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:keyLabel="L" android:keyEdgeFlags="right"/>
    </Row>

    <Row    android:keyHeight="8%p">

        <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:codes="46" android:keyLabel="."
              />
        <Key android:codes="32" android:keyLabel="SPACE"
                android:keyWidth="20%p" android:isRepeatable="true"/>
         <Key android:codes="10" android:keyIcon="@drawable/sym_keyboard_return" 
                android:keyWidth="20%p" android:keyEdgeFlags="right"/>

    </Row>


</Keyboard>

我得到了以下内容:

enter image description here

请忽略第一行,如何使键盘按钮背景像第一张图片那样?我的意思是我想让我的键盘与第一张图片的所有细节一样。如何将 Gmail、Hotmail等添加到我的键盘的第一行?我还想在用户点击任何键时添加声音,就像默认的安卓键盘一样。我该怎么做?


2
  1. 谷歌一下
  2. 阅读一些Android文档和代码示例
  3. 编写代码
  4. 失败
  5. 在StackOverflow上提出具体问题
- Paul Sasik
1
请查看此答案:https://dev59.com/HHA75IYBdhLWcg3wFE6b#3480756 - kumar_android
我更新了我的问题,如何解锁它? - Mohammed Subhi Sheikh Quroush
我想提名重新开放这个问题,但是能否澄清一下问题?你所说的“如何使键盘按钮的背景像第一张照片”是什么意思?否则,这个问题是一个很好的重新开放的候选者。 - Ben D
我的意思是第一个键盘有实心背景(灰色),而第二个键盘有渐变背景。我读了软件键盘的示例,但未发现原因,第一个键盘中的空格按钮具有较暗的背景,我想让我的键盘(第二个)像第一个(键盘照片)那样。 - Mohammed Subhi Sheikh Quroush
你应该看一下来自谷歌的LatinIME:https://android.googlesource.com/platform/packages/inputmethods/LatinIME/。他们定制了他们的键盘。 - IgorGanapolsky
1个回答

20

制作键盘的XML如下:

<?xml version="1.0" encoding="utf-8"?>
<com.YourKeyboardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keyboard"
    style="@style/keyboard_1_style"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

</com.YourKeyboardView>

样式如下:

 <style name="keyboard_1_style">
        <item name="android:keyBackground">@drawable/k1_selector</item>
        <item name="android:keyTextColor">#24B2E7</item>
        <item name="android:background">@android:color/white</item>
        <item name="android:keyPreviewLayout">@layout/k1_preview</item>
 </style>

在哪里

<item name="android:keyBackground">@drawable/k1_selector</item>

用于设置键的背景。

<item name="android:keyTextColor">#24B2E7</item> 

用于关键字文本的颜色。

<item name="android:background">@android:color/white</item> 

用于设置整个键盘的背景

<item name="android:keyPreviewLayout">@layout/k1_preview</item>

用于设置键的预览。

预览布局XML:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="40sp"
    android:textColor="@android:color/white"
    android:gravity="center"
    android:background="@drawable/neon_candidate_middle_pressed"/>

对于键之间的空格,请使用这个

对于android:background="@drawable/neon_candidate_middle_pressed",它是你想在键的预览中显示的背景图像。


1
我尝试了您的解决方案,但我需要@drawable/k1_selector和@layout/k1_preview的值。 - Mohammed Subhi Sheikh Quroush
@forsubhi 请告诉我它是否对您有效?如果是,请将其标记为答案。 - baldguy
1
@forsubhi 使用此代码:<Key android:keyWidth="13.185714%p" android:keyLabel="Hotmail" android:keyOutputText="Hotmail" android:horizontalGap="1.1%p"/> - baldguy
你如何让空格键拥有不同的背景?我正在尝试让特殊功能键拥有较暗的背景,我从Android中复制了一个样式(<item android:state_single="true" android:state_pressed="true" android:drawable="@drawable/btn_keyboard_key_dark_pressed_holo" /> <item android:state_single="true" android:drawable="@drawable/btn_keyboard_key_dark_normal_holo" />),但是标记具有state_single=true的键并没有起作用。 - SoManyGoblins
@Rahil2952 在我的自定义键盘中,黑色文本和白色键背景下的文本似乎有点模糊。请告诉我如何消除文本的这种模糊效果。 - maddy d
显示剩余9条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接