android自定义键盘(解决弹出提示的字体颜色问题)
来源:互联网 发布:海贼王883知乎 编辑:程序博客网 时间:2024/06/14 15:58
最近准备要做一个项目,需要用到自定义小键盘来确保安全,而且还需要精确获得用户点击键盘时的落点位置、力度、指尖接触屏幕的面积等参数。
在写自定义键盘的时候,用到了国内网上的一些代码,出处是
http://blog.csdn.net/hfsu0419/article/details/7924673
向先人致敬!
然后发现down下来的代码用到我的项目时,出现了各种问题:
1.首先,是一打开应用,就会出现弹出的是系统的输入法键盘,而是不自定义键盘,这个问题是由于EditText会在应用打开的使用获得焦点,导致直接弹出系统输入法,而不是自定义输入法。解决的办法是使EditText在应用打开时不获取焦点,于是我在activity对应的布局文件的全局Layout标签中加入
android:focusable="true" android:focusableInTouchMode="true"
那么在activity运行时,EditText就失去了焦点,系统输入法也不会弹出。然后对EditText加入触摸监听器,当点击EditText控件时,就响应弹出我们的键盘
et.setOnTouchListener(new View.OnTouchListener(){@Overridepublic boolean onTouch(View v, MotionEvent event) {int inputback = et.getInputType();et.setInputType(InputType.TYPE_NULL);AdvantageKeyboard kb = new AdvantageKeyboard(act,ctx,et);kb.showKeyboard();et.setInputType(inputback);v.performClick();return false;}});
2.点击时,键盘弹出的提示框里面没有字体。例如这样
这是由于显示的字体时白色,而背景也是白色导致的。后来各种百度,有人说是因为theme的问题,因为我用的是android5.0.1的API,自动生成项目时,生成的activity是直接继承ActionBarActivity的,直接改theme会导致应该出现秒退现象。如果要改theme就要先改继承ActionBarActivity为Activity,然后再去改theme,但是我很不喜欢这样,因为这样没有通用性,后来在google了一下外国人的贴子,才了解到这个弹出的东西叫preview,我们可以修改它的布局的。
在我们自定义的KeyboardView中加入android:keyPreviewLayout标签,加入后如下:
<pre name="code" class="html"> <android.inputmethodservice.KeyboardView android:id="@+id/keyboard_view" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:focusable="true" android:focusableInTouchMode="true" android:background="@color/black" android:keyBackground="@drawable/btn_keyboard_key" android:keyPreviewLayout="@layout/key_preview_layout" android:keyTextColor="@color/white" android:visibility="gone" />
然后在layout文件夹中,新建一个key_preview_layout.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="#ff8888ff"/>
其中android:background就是我们的提示框的背景颜色,android:textColor是字体颜色,改成你喜欢的就OK了。
3.加入键盘的触摸响应,并记录点击的相关信息(力度,接触面积,落点坐标等)。
我们可以在自定义键盘中的构造函数中对keyboardView加入触摸监听器,例如这样写构造方法。
public AdvantageKeyboard(Activity act, Context ctx, EditText edit) {this.act = act;this.ctx = ctx;this.ed = edit;kb_letter = new Keyboard(this.ctx, R.xml.qwerty);kb_number = new Keyboard(this.ctx, R.xml.symbols);keyboardView = (KeyboardView) this.act.findViewById(R.id.keyboard_view);keyboardView.setKeyboard(kb_number);is_nun = true;keyboardView.setEnabled(true);keyboardView.setPreviewEnabled(true);keyboardView.setOnKeyboardActionListener(action_listener);keyboardView.setOnTouchListener(touch_listener);//加入触摸监听器}
然后new一个我们处理这些数据的监听器,需要我们“重载”(可能叫“覆盖”比较合适)OnTouch函数,把采集的数据显示到Locat上。需要注意的是,这个OnTouch函数假如返回值为true,就代表这个动作已被处理,为false就代表没被处理,由于后面还有其他函数需要获得此动作,所以这里返回false,否则,接下来的对按键动作的响应(字体输出等)将不会发生。
private OnTouchListener touch_listener = new OnTouchListener(){@Overridepublic boolean onTouch(View v, android.view.MotionEvent event) {float pressure, size, rx, ry, x, y, interval;long time, down_time;int action = event.getAction(); switch (action) { case (MotionEvent.ACTION_DOWN): pressure = event.getPressure(); size = event.getSize(); time = event.getEventTime(); down_time = event.getDownTime(); x = event.getX(); y = event.getY(); rx = event.getRawX(); ry = event.getRawY(); interval = (float) 0.0; if(is_first_press){ interval = down_time - last_down_time; } is_first_press = true; last_down_time = down_time; Log.i("!!!!!!!!!","pressure:"+String.valueOf(pressure)+"\n" +"size:"+String.valueOf(size)+"\n" +"time:"+String.valueOf(time)+"\n" +"downtime:"+String.valueOf(down_time)+"\n" +"x:"+String.valueOf(x)+" y:"+String.valueOf(y)+"\n" +"rx:"+String.valueOf(rx)+" ry:"+String.valueOf(ry)+"\n" +"interval:"+String.valueOf(interval)+"\n"); break; case (MotionEvent.ACTION_UP): v.performClick(); break; case (MotionEvent.ACTION_MOVE): break; }return false;}};
然后搞定了...
源码下载:http://download.csdn.net/detail/arrow2013/8414521
- android自定义键盘(解决弹出提示的字体颜色问题)
- Android-解决AutoCompleteText下拉提示的字体颜色是白色的问题
- Android自定义dialog中的EditText无法弹出键盘的解决
- Android自定义dialog中的EditText无法弹出键盘的解决
- Android自定义dialog中的EditText无法弹出键盘的解决
- 解决Android键盘弹出后,背景图挤压变形的问题
- 解决activity自动弹出键盘的问题
- Android 解决TextVIew加载自定义字体慢的问题
- android自定义dialog,软件键盘弹出挡住输入区域解决
- android 虚拟键盘弹出的问题
- Android学习问题:关于AlertDialog中自定义布局带有的EditText无法弹出键盘
- Android自定义view解决TabWidget 的下方的横线(Strip)颜色问题
- Android自定义Button字体颜色
- Android自定义Button字体颜色
- Android 选择日期时间对话框(可选择开始结束时间,已解决弹出键盘问题)
- Android 选择日期时间对话框(可选择开始结束时间,已解决弹出键盘问题)
- [iOS]解决键盘弹出时挡住输入框的问题
- anroid解决键盘弹出后遮住登录按钮的问题
- JavaBean 和 Map 之间互相转换
- XcodeDebug---远程bug报告和分析
- netfilter(1)
- 一个私有云选型方案
- php高手进阶:实现强大的翻页跳转功能
- android自定义键盘(解决弹出提示的字体颜色问题)
- HTTP协议第二篇
- 先介绍一下在翻页中用到的数据库语法
- AOP 的利器:ASM 3.0 介绍
- 【GoLang笔记】A Tour of Go - Exercise: Web Crawler
- 这里将介绍两种翻页显示功能的实现:
- bug of vector
- PHP新版本10月出炉!跳过6直上PHP7?
- SVN 相关