android 软键盘的POPUP布局的问题解决

所属分类: 软件编程 / Android 阅读数: 105
收藏 0 赞 0 分享

我正在开发一个软键盘,做得很好,但是我不知道如何自定义一个长按键的弹出窗口.

我的键盘视图:

<?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óò ôö" />
 <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áà âä"/>
 <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>

我尝试将keyBackground和背景属性放在任何地方,但不成功.我试图把:

android:popupLayout="@layout/keyboard"

…在keyboardView上得到nullpointer,也许我在该参数中输入了错误的XML?

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

android:popupKeyboard="@xml/keyboard_popup"

我可以改变布局的大小,按键大小,键间距等等,但不能改变颜色或背景.

关键预览也做得很好,我把它放在keyboardView上:

android:keyPreviewLayout="@layout/preview"

…它的工作原理我认为弹出窗口应该是一样的,但不是这样.

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

那么这不是我正在寻找的,而是解决了这个问题.

我自己创建了键盘视图,并弹出一个弹出窗口来显示一个键长时间按下.

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);
 }
}

这样,您可以在xml中以任何方式自定义弹出窗口.

但是这不是正确的答案,如果你知道一个更好的方法来回答这个问题.

http://stackoverflow.com/questions/34799775/soft-keyboards-popup-layout

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android样式和主题之选择器的实例讲解

今天小编就为大家分享一篇关于Android样式和主题之选择器的实例讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Android应用动态修改主题的方法示例

今天小编就为大家分享一篇关于Android应用动态修改主题的方法示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter 网络请求框架封装详解

这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Flutter倒计时/计时器的实现代码

这篇文章主要介绍了Flutter倒计时/计时器的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android使用google breakpad捕获分析native cash

这篇文章主要介绍了Android使用google breakpad捕获分析native cash 的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Flutter WebView与JS互相调用简易指南

这篇文章主要介绍了详解Flutter WebView与JS互相调用简易指南,分为JS调用Flutter和Flutter调用JS,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android开发实现ListView和adapter配合显示图片和文字列表功能示例

这篇文章主要介绍了Android开发实现ListView和adapter配合显示图片和文字列表功能,涉及Android使用ListView结合adapter适配器实现图文显示功能相关的布局、解析、权限控制等操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android文字基线Baseline算法的使用讲解

今天小编就为大家分享一篇关于Android文字基线Baseline算法的使用讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter自定义实现神奇动效的卡片切换视图的示例代码

这篇文章主要介绍了Flutter自定义实现神奇动效的卡片切换视图的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现自定义滑动刻度尺方法示例

这篇文章主要给大家介绍了关于Android实现自定义滑动刻度尺的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多