android 软键盘的POPUP布局的问题解决
程序员文章站
2022-06-10 19:06:55
我正在开发一个软键盘,做得很好,但是我不知道如何自定义一个长按键的弹出窗口.
我的键盘视图:
我正在开发一个软键盘,做得很好,但是我不知道如何自定义一个长按键的弹出窗口.
我的键盘视图:
<?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中以任何方式自定义弹出窗口.
但是这不是正确的答案,如果你知道一个更好的方法来回答这个问题.
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
Android仿淘宝切换商品列表布局效果的示例代码
-
Android实现输入法弹出时把布局顶上去和登录按钮顶上去的解决方法
-
Android 仿京东商城底部布局的选择效果(Selector 选择器的实现)
-
Android编程实现点击EditText之外的控件隐藏软键盘功能
-
Android中ImageView无法居中的问题解决方法
-
Android 自定义View实现任意布局的RadioGroup效果
-
Android 软键盘出现不适应的解决办法总结
-
Android ImageView 不显示JPEG图片的问题解决
-
Android开发之ListView功能扩展,实现高性能的瀑布流布局讲解
-
android POST数据遇到的UTF-8编码(乱码)问题解决办法