【问题标题】:Soft keyboard's POPUP layout软键盘的 POPUP 布局
【发布时间】:2016-04-20 09:07:40
【问题描述】:

我正在开发一个软键盘并且做得很好,但是我不知道如何为长按键自定义弹出布局。

我的键盘视图:

<?xml version="1.0" encoding="UTF-8"?>
<android.inputmethodservice.KeyboardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keyboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:keyPreviewLayout="@layout/preview"
android:keyBackground="@drawable/key_selector"
android:shadowRadius="0.0"
android:keyTextColor="#000000"
/>

我的键盘布局:

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="10%p"
android:keyHeight="10%p">

<Row android:verticalGap="1%p"  android:horizontalGap="0.5%p" android:keyHeight="8%p" android:keyWidth="9.6%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"
         android:popupKeyboard="@xml/keyboard_popup" android:popupCharacters="yýÿ"/>
    <Key android:codes="117"    android:keyLabel="u"
         android:popupKeyboard="@xml/keyboard_popup" android:popupCharacters="uúùũûü"/>
    <Key android:codes="105"    android:keyLabel="i" 
         android:popupKeyboard="@xml/keyboard_popup" android:popupCharacters="iíìĩîï"/>
    <Key android:codes="111"    android:keyLabel="o" 
         android:popupKeyboard="@xml/keyboard_popup" android:popupCharacters="oóò&#245;ôö" />
    <Key android:codes="112"    android:keyLabel="p" />
</Row>
<Row android:verticalGap="1%p" android:horizontalGap="0.5%p" android:keyHeight="8%p" android:keyWidth="9.6%p">
    <Key android:codes="97"    android:keyLabel="a" android:keyEdgeFlags="left" android:horizontalGap="5%p"
         android:popupKeyboard="@xml/keyboard_popup" android:popupCharacters="aáà&#227;âä"/>
    <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" />
</Row>
<Row android:verticalGap="1%p"  android:horizontalGap="0.5%p" android:keyHeight="8%p" android:keyWidth="9.6%p">
    <Key android:codes="3"      android:keyIcon="@drawable/keyboard_shift_off" 
         android:keyHeight="7.6%p" android:keyWidth="13.7%p"/>
    <Key android:codes="122"    android:keyLabel="z" android:horizontalGap="1%p"/>
    <Key android:codes="120"    android:keyLabel="x" />
    <Key android:codes="99"     android:keyLabel="c"
         android:popupKeyboard="@xml/keyboard_popup" android:popupCharacters="cç"/>
    <Key android:codes="118"    android:keyLabel="v" />
    <Key android:codes="98"     android:keyLabel="b" />
    <Key android:codes="110"    android:keyLabel="n"
         android:popupKeyboard="@xml/keyboard_popup" android:popupCharacters="nñ"/>
    <Key android:codes="109"    android:keyLabel="m" />
    <Key android:codes="-5"     android:keyIcon="@drawable/sym_keyboard_delete_dim"
        android:keyHeight="7.6%p" android:keyWidth="13.7%p" 
        android:horizontalGap="1%p"/>
</Row>
<Row android:verticalGap="1%p"  android:horizontalGap="0.5%p" android:keyHeight="8%p" android:keyWidth="9.6%p">
    <Key android:codes="-16"    android:keyIcon="@drawable/keyboard_symbol"
         android:keyHeight="7.6%p" android:keyWidth="18.7%p"/>
    <Key android:codes="44"     android:keyLabel="," android:horizontalGap="1%p"/>
    <Key android:codes="32"     android:keyIcon="@drawable/sym_keyboard_feedback_space" android:keyWidth="40%p"/>
    <Key android:codes="46"     android:keyLabel="."/>
    <Key android:codes="-3"     android:keyIcon="@drawable/keyboard_go" 
        android:keyHeight="7.6%p" android:keyWidth="18.5%p" android:horizontalGap="1%p"/>
</Row>

键盘弹出 XML:

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="10%p"
android:keyHeight="10%p">
</Keyboard>

我尝试将 keyBackgroundbackground 属性 放在任何地方,但没有成功。我试着说:

android:popupLayout="@layout/keyboard"

...在 keyboardView 上但得到空指针,也许我在那个参数中输入了错误的 XML?

在我放在这里的键盘弹出窗口 XML中:

android:popupKeyboard="@xml/keyboard_popup"

我可以更改布局的大小、键大小、键间距和其他内容,但不能更改颜色或背景。

key preview也不错,我把这个放在keyboardView上:

android:keyPreviewLayout="@layout/preview"

...它的工作原理。我认为弹出窗口应该是相同的方式,但事实并非如此。

如何自定义长按时出现的弹出窗口?

【问题讨论】:

标签: android android-layout keyboard popup android-softkeyboard


【解决方案1】:

嗯,这不是我想要的,但这解决了问题。

我制作了自己的键盘视图并制作了一个弹出窗口来显示何时长按某个键。

public class MyKeyboardView extends KeyboardView{
   @Override
   protected boolean onLongPress(final Key popupKey) {
        final View custom = LayoutInflater.from(context)
        .inflate(R.layout.popup_layout, new FrameLayout(context));
        popup = new PopupWindow(context);
        popup.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
        popup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        popup.showAtLocation(this, Gravity.NO_GRAVITY, popupKey.x, popupKey.y-50);
        return true;
   }
}

通过这种方式,您可以在 xml 中以任何您想要的方式自定义弹出窗口。

但这不是正确的答案,如果您知道更好的方法,请回答这个问题。

【讨论】:

  • 您找到其他解决方案了吗?或者你有没有在 Android Pie 中尝试过这个弹出窗口?
  • 很遗憾还没有
猜你喜欢
  • 2018-08-28
  • 1970-01-01
  • 1970-01-01
  • 2021-08-27
  • 2016-03-10
  • 2018-12-01
  • 1970-01-01
  • 2016-07-15
  • 2011-10-09
相关资源
最近更新 更多