《犀利仁师:比武招亲》------挑战奕奕输入法开发
来源:互联网 发布:显卡在线测试软件 编辑:程序博客网 时间:2024/04/29 15:27
前言
投票
话说,今天天气晴朗,阳光明媚,白云朵朵,长安城大龄剩女路云菲摆擂台比武招亲。长安第一高等学府弘文学院的武术先生柳傲天前来打擂。众学生前来助阵。一场热闹的挑战就此开始啦。
今天挑战的题目就是高仿Android版《奕奕输入法》。
具体要求如下:
第一关:创建输入法的服务类。并且进行配置。
第二关:显示一个简单的键盘。
第三关:创建一个输入法的设置界面,并且显示出来。
第四关:完善键盘。
第五关:实现键盘的点击事件。
通过以上无关,即可进入下一轮选拔。哈哈。
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
第一关:创建输入法的服务类。并且进行配置。
1、创建服务类。
//创建一个服务类,该类继承自输入法服务类。public class Main extends InputMethodService {}
代码解释:首先创建一个服务类,并且让该类继承自输入法服务类(InputMethodService)。因为输入法本身就输入一个服务,就好像壁纸等后台服务功能。都是在后台服务,不过该服务可以显示视图(键盘和候选字词的视图)。
2、在AndroidMainfest.xml中进行配置。
<!-- 这里是输入法的服务 --> <service android:name="com.smileim.cn.Main" android:permission="android.permission.BIND_INPUT_METHOD" > <!--意图过滤的设置--> <intent-filter> <action android:name="android.view.InputMethod" > </action> </intent-filter> <!--元数据的设置--> <meta-data android:name="android.view.im" android:resource="@xml/method" > </meta-data> </service>
代码解释:
这里是对上面的服务类Main进行相应的配置。
包括Main类的路径,权限。以及在intent-filter中进行action的设置。元数据的设置。
注意这里:android:resource="@xml/method"。也就是说需要创建一个xml文件夹,在该文件夹下面创建一个method文件。
3、创建xml文件夹。
图:关于xml文件夹的创建
到此,基本的工作就完成了,接下来就可以来显示键盘、候选字词,以及设置界面啦。
哈哈,是不是很简单,不要被别人的话吓住哦。要相信自己。加油。
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
第二关:显示一个简单的键盘
基本工作完成以后,接下来就来显示一个简单的键盘。
首先要做的就是设计一个简单的界面。这里是设计了一个横向视图,共计7个按钮,分别代表:A、B、C、D、E、F、G。
这个很简单,但是为了保持代码的完整性,贴出来吧。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <Button android:id="@+id/Button_01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="A" ></Button> <!--其它几个省略了--> ......</LinearLayout>
设计好了视图,就要将它显示出来。
显示视图也是很简单的。就是在服务类的回调方法里面就行调用就可以了。虽然很简单,但是,没有人告诉我们,谁又能知道它是回调方法呢!
//创建一个服务类,该类继承自输入法服务类。public class Main extends InputMethodService {//回调方法,创建输入视图public View onCreateInputView() {LayoutInflater layoutInflater = LayoutInflater.from(this);View view = layoutInflater.inflate(R.layout.testkeyboard, null);return view;}}
代码解释:
在回调方法:onCreateInputView()里面进行调用。显示视图。写到这里,说一下我第一次的经历吧。当时,我不小心把方法写错了,写成了onCreateView(),结果是查找了很长时间才发现错误。可见,做事情一定要仔细和认真。
效果图如下:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
第三关:创建设置界面
用过Android智能手机的亲亲都知道,输入法是可以有一个界面进行各种设置的。
接下来就来开发一个简单的设置界面。
步骤如下:
创建一个视图,用来显示设置界面。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" ><TextView android:id="@+id/TextView_01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/set" ></TextView> </LinearLayout>
代码解释:创建一个视图,用来显示一个视图,这里是显示一行简单的文本。
然后,在继承自Activity的类里面进行调用。
public class Set extends Activity{public void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.set);}}
接下来在AndroidMainfest.xml里面进行配置。
<activity android:name="com.smileim.cn.Set" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
然后,就是最重要的一步了,就是把类Set写在method.xml中。
<?xml version="1.0" encoding="utf-8"?><input-method xmlns:android="http://schemas.android.com/apk/res/android" android:isDefault="true" android:settingsActivity="com.smileim.cn.Set" ></input-method>
android:settingsActivity="com.smileim.cn.Set"是非常重要的。可千万别忘记了。
写到这里,就可以来显示设置界面啦。
效果图:
图:设置界面
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
第四关:完善键盘
真正的键盘并没有这么简单。接下来就来说一下键盘的通用开发方法。
创建一个键盘视图,用<KeyboardView></KeyboardView>标记对来表示一个整体布局。然后,在服务类里面,得到它的对象,然后调用setKeyboard()来添加键盘。当我们需要字母键盘的时候,就添加字母键盘;需要数字键盘的时候,就添加数字键盘。
首先创建一个整体布局。
<?xml version="1.0" encoding="utf-8"?><android.inputmethodservice.KeyboardView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/KeyboardView_01" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FF000000" ></android.inputmethodservice.KeyboardView>
然后,就是创建键盘视图:
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android" android:keyWidth="10%p" android:keyHeight="50dip" android:horizontalGap="0dip" android:verticalGap="0dip" > <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> <!--其它行省略--></Keyboard>
代码解释:android:keyWidth="10%p"表示百分之十。
android:horizontalGap="0dip"表示水平间隔。
android:verticalGap="0dip"表示垂直间隔 。
android:keyEdgeFlags="left"表示按键的位置标记。这里是左侧。
接下来就是显示键盘的方法:
// 回调方法,只调用一次。建议把初始化的内容放到这里进行。public void onCreate() {super.onCreate();qwertyView = new Keyboard(this, R.xml.qwerty);}// 回调方法,创建输入视图public View onCreateInputView() {LayoutInflater layoutInflater = LayoutInflater.from(this);View view = layoutInflater.inflate(R.layout.keyboardlayout, null);keyboardView=(KeyboardView)view.findViewById(R.id.KeyboardView_01);keyboardView.setEnabled(true);keyboardView.setPreviewEnabled(true);keyboardView.setKeyboard(qwertyView);return keyboardView;}
代码解释:
在onCreateInputView()回调方法中引入了布局视图,得到对象keyboardView。
在onCreate()回调方法中引入了键盘的视图。
然后,keyboardView对象调用setKeyboard()方法,显示键盘。
效果图:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
第五关:响应键盘的点击事件
响应键盘的点击事件是在onStartInputView()回调方法中进行的。
// 回调方法,响应按键的点击事件public void onStartInputView(EditorInfo attribute, boolean restarting) {super.onStartInputView(attribute, restarting);keyboardView.setOnKeyboardActionListener(new KeyboardListener(Main.this));}
代码解释:
这里是通过一个监听器方法,实现点击事件的监听。
对于每个按键的监听,我们把它封装到了一个类里面,这也是为了保持代码的整洁和便于维护。
public class KeyboardListener implements OnKeyboardActionListener {private Main service;public KeyboardListener(Main service1) {this.service = service1;}public void onKey(int primaryCode, int[] keyCodes) {InputConnection inputConnection = service.getCurrentInputConnection();if (primaryCode == 113) {inputConnection.commitText("q", 1);} //其它按键的提交省略} }
在这个封装的类里面,通过构造方法引入了服务类。
然后,通过服务类,得到InputConnection类的对象,然后通过commit()实现按键的点击响应。
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
到此,五关已过,但是,词库和汉字的查找算法尚未开发。希望大家能够多多的支持,帮我投上一票。谢谢。
- 《犀利仁师:比武招亲》------挑战奕奕输入法开发
- 《犀利仁师》将播 张哲瀚挑战高丽“求学少年”09:54
- 犀利仁师<吴奇隆,李诗诗>
- 《犀利仁师》曝预告 吴奇隆刘诗诗过招
- 《犀利仁师》长片花曝光 笑中飚泪先睹为快
- 《犀利仁师》明日收官 "隆诗恋"惊天逆转
- 刘诗诗《犀利仁师》鬼马卖萌 丰富表情笑果十足
- 技术比武
- 比武大会
- 输入法开发
- PHP框架大比武
- 全国各地骗子大比武
- 华为内部比武题
- JVM动态语言比武
- 大比武考试。。。。悲剧
- 全国各地骗子大比武
- 犀利广州话
- T9输入法开发历程
- 优秀程序员的十个习惯
- Android读取手机中的短信
- Candy
- LeetCode: Remove Duplicates from Sorted Array II
- 关于c++的误区
- 《犀利仁师:比武招亲》------挑战奕奕输入法开发
- Leetcode: Minimum Window Substring
- Struts2技术详解
- python ODBC库 pyodbc
- 读卡器的状态机, python实现
- error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“0”不匹配值“2”
- SecureCRT突然卡死的问题
- 垂直制表符(‘\v’)和水平制表符(‘\t’)
- import android.provider.Telephony cannot be resolved