1.自带实例
最近一直在研究输入法,特此做一下笔记:
首先显示看到的Android自带的输入法APP,查看下图:
如果觉得这个输入法的按键的高度不合适可以根据自己的爱好修改按键的高度,具体修改的内容是:res/values/dimens.xml
<dimen name="key_height">50dip</dimen>
可以修改成30dp
<dimen name="key_height">30dip</dimen>
效果如下图:
是不是键盘矮了好多?再次我就不上传代码了,网上的代码很多,可以自行下载。同是也可以修改键盘的颜色、字体大小的等属性。
2.自定义输入法
虽然网上给出的例子可以很好的实现输入法功能,但是总是感觉不是自己写的代码,没有可控性,比如说我想重新设计键盘的样式、设计某些按键的按键效果和相应事件等。
输入法其实就是一种服务,必须继承InputMethodService这个类。要想重新设计整个键盘必须重写其中的两个方法:
//返回的是键盘View,如果想重新设计键盘布局,就必须要返回这个布局的View
//返回值就是键盘的View
@Override
public View onCreateInputView()
//返回的候选词View,若想重新设计候选词的布局,则返回这个View
@Override
public View onCreateCandidatesView()
1).新建一个新的并且是空的Module
2).配置AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.manboker.wb.imelayout">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<service
android:name="MyDefaultIME"
android:label="IMELayout"
android:permission="android.permission.BIND_INPUT_METHOD">
<meta-data
android:name="android.view.im"
android:resource="@xml/method"/>
<intent-filter>
<action android:name="android.view.InputMethod"/>
</intent-filter>
</service>
</application>
</manifest>
3).新建res/xml/method.xml
<?xml version="1.0" encoding="utf-8"?>
<input-method xmlns:android="http://schemas.android.com/apk/res/android" />
4).新建类继承自InputMethodService
package com.manboker.wb.imelayout;
import android.inputmethodservice.InputMethodService;
import android.view.KeyEvent;
import android.view.View;
import com.manboker.wb.imelayout.myinterface.IKeyBoard;
import com.manboker.wb.imelayout.view.CandidateView;
import com.manboker.wb.imelayout.view.MyKeyboardView;
/**
* Date: 2016/11/8 15:58.
*/
public class MomentCamIME extends InputMethodService {
private MyKeyboardView mMyKeyboardView;
private CandidateView mCandidateView;
private IKeyBoard mIKeyBoard = new IKeyBoard() {
@Override
public void setInputText(String text) {
getCurrentInputConnection().commitText(text, 0);
}
@Override
public void deleteInputOne() {
getCurrentInputConnection().deleteSurroundingText(1, 0);
}
@Override
public void enter() {
getCurrentInputConnection().sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_ENTER));
}
@Override
public void done() {
requestHideSelf(0);
}
@Override
public void setCondiateShow(boolean b) {
setCandidatesViewShown(b);
}
};
@Override
public View onCreateInputView() {
mMyKeyboardView = new MyKeyboardView(this);
mMyKeyboardView.setInterface(mIKeyBoard);
return mMyKeyboardView;
}
@Override
public View onCreateCandidatesView() {
mCandidateView = new CandidateView(this);
return mCandidateView;
}
}
4).设计键盘布局(只有英文和数字、符号)
主布局:res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/main_number"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<include
android:id="@+id/main_include_number"
android:layout_width="wrap_content"
android:layout_height="match_parent"
layout="@layout/number" >
</include>
</LinearLayout>
<LinearLayout
android:id="@+id/main_letter"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<include
android:id="@+id/main_include_letter"
android:layout_width="wrap_content"
android:layout_height="match_parent"
layout="@layout/qwerty" >
</include>
</LinearLayout>
</FrameLayout>
字母布局:res/layout/qwerty.xml
<?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="wrap_content"
android:background="@android:color/white"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/key_height"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:orientation="horizontal">
<Button
android:id="@+id/keyboard_q"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="q"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_w"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="w"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_e"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="e"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_r"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="r"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_t"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="t"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_y"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="y"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_u"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="u"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_i"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="i"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_o"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="o"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_p"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="p"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/key_height"
android:layout_marginTop="@dimen/key_padding"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:visibility="invisible"
/>
<Button
android:id="@+id/keyboard_a"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="a"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_s"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="s"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_d"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="d"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_f"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="f"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_g"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="g"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_h"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="h"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_j"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="j"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_k"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="k"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_l"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="l"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:visibility="invisible"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/key_height"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="@dimen/key_padding"
android:orientation="horizontal">
<ImageButton
android:id="@+id/keyboard_shift"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="3"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:src="@mipmap/sym_keyboard_shift"
/>
<Button
android:id="@+id/keyboard_z"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="z"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_x"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="x"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_c"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="c"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_v"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:text="v"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_b"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="b"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_n"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="n"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_m"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="m"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<ImageButton
android:id="@+id/keyboard_delete"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="3"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:src="@mipmap/sym_keyboard_delete"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/key_height"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="@dimen/key_padding"
android:orientation="horizontal">
<ImageButton
android:id="@+id/keyboard_done"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="3"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:src="@mipmap/sym_keyboard_done"
/>
<Button
android:id="@+id/keyboard_change_123"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="123"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_change_size"
/>
<Button
android:id="@+id/keyboard_dot"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="."
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<ImageButton
android:id="@+id/keyboard_space"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:src="@mipmap/sym_keyboard_space"
/>
<Button
android:id="@+id/keyboard_comma"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text=","
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<ImageButton
android:id="@+id/keyboard_return"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="3"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:src="@mipmap/sym_keyboard_return"
/>
</LinearLayout>
</LinearLayout>
数字键盘布局:res/layout/number.xml
<?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="wrap_content"
android:background="@android:color/white"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/key_height"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:orientation="horizontal">
<Button
android:id="@+id/keyboard_num_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="1"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_num_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="2"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_num_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="3"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_num_4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="4"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_num_5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="5"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_num_6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="6"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_num_7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="7"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_num_8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="8"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_num_9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="9"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_num_10"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="0"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/key_height"
android:layout_marginTop="@dimen/key_padding"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:orientation="horizontal">
<Button
android:id="@+id/keyboard_num_11"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="\@"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_num_12"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:layout_marginLeft="@dimen/key_padding"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="#"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_num_13"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="$"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_num_14"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="%"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_num_15"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="&"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_num_16"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="*"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_num_17"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="-"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_num_18"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="="
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_num_19"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="("
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_num_20"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="1"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text=")"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/key_height"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="@dimen/key_padding"
android:orientation="horizontal">
<ImageButton
android:id="@+id/keyboard_num_shift"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="3"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:src="@mipmap/sym_keyboard_shift"
/>
<Button
android:id="@+id/keyboard_num_21"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="!"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_num_22"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="""
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_num_23"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="\'"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_num_24"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:text=":"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_num_25"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text=";"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_num_26"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="/"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<Button
android:id="@+id/keyboard_num_27"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="\?"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<ImageButton
android:id="@+id/keyboard_num_delete"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="3"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:src="@mipmap/sym_keyboard_delete"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/key_height"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="@dimen/key_padding"
android:orientation="horizontal">
<ImageButton
android:id="@+id/keyboard_num_done"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="3"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:src="@mipmap/sym_keyboard_done"
/>
<Button
android:id="@+id/keyboard_num_change_abc"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="abc"
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_change_size"
/>
<Button
android:id="@+id/keyboard_num_dot"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text="."
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<ImageButton
android:id="@+id/keyboard_num_space"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:src="@mipmap/sym_keyboard_space"
/>
<Button
android:id="@+id/keyboard_num_comma"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="2"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:text=","
android:textAllCaps="false"
android:textColor="@color/colorKeyboardFont"
android:textSize="@dimen/key_text_size"
/>
<ImageButton
android:id="@+id/keyboard_num_return"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/key_padding"
android:layout_weight="3"
android:background="@drawable/selector_keyboard_letter"
android:gravity="center"
android:paddingBottom="@dimen/key_font_padding"
android:paddingLeft="@dimen/key_font_padding"
android:paddingRight="@dimen/key_font_padding"
android:paddingTop="@dimen/key_font_padding"
android:src="@mipmap/sym_keyboard_return"
/>
</LinearLayout>
</LinearLayout>
5).其他
values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="colorLightGray">#CCCCCC</color><!--键盘正常时背景颜色-->
<color name="colorDarkGray">#999999</color><!--键盘选中时背景颜色-->
<color name="colorKeyboardFont">#555555</color><!--键盘字体颜色-->
</resources>
res/values/dimens.xml
<resources>
<dimen name="key_height">30dip</dimen>
<dimen name="candidate_font_height">16sp</dimen>
<dimen name="candidate_vertical_padding">6sp</dimen>
<dimen name="key_text_size">20sp</dimen>
<dimen name="key_change_size">12sp</dimen>
<dimen name="key_padding">2dp</dimen>
<dimen name="key_font_padding">1dp</dimen>
</resources>
values/attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="GifView">
<attr name="gif" format="reference"/>
<attr name="paused" format="boolean"/>
</declare-styleable>
<declare-styleable name="CustomTheme">
<attr name="gifViewStyle" format="reference"/>
</declare-styleable>
</resources>
values/styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="Widget_GifView"></style>
</resources>
6).候选词View定义(一个可以显示GIF动画的Gallery)
package com.manboker.wb.imelayout.view;
import android.content.Context;
import android.os.Environment;
import android.view.LayoutInflater;
import android.widget.Gallery;
import android.widget.LinearLayout;
import com.manboker.wb.imelayout.R;
import com.manboker.wb.imelayout.gif.GifAdapter;
import java.util.ArrayList;
/**
* Date: 2016/11/11 09:17.
*/
public class CandidateView extends LinearLayout {
private Context mContext;
private Gallery mGallery;
public CandidateView(Context context) {
super(context);
mContext = context;
init();
}
private void init(){
LayoutInflater.from(mContext).inflate(R.layout.candidate_view, this);
mGallery = (Gallery) findViewById(R.id.candidate_gallery);
GifAdapter gifAdapter = new GifAdapter(mContext, getGifPath(getExternCard()));
mGallery.setAdapter(gifAdapter);
}
private String getExternCard() {
return Environment.getExternalStorageDirectory().getAbsolutePath().toString();
}
private ArrayList<String> getGifPath(String gifDir) {
ArrayList<String> paths = new ArrayList<String>();
paths.add(gifDir + "/my/20161110134130.gif");
paths.add(gifDir + "/my/20161110134136.gif");
paths.add(gifDir + "/my/20161110134142.gif");
paths.add(gifDir + "/my/20161110134151.gif");
return paths;
}
}
GIFView.java
package com.manboker.wb.imelayout.gif;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import com.manboker.wb.imelayout.R;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
* Date: 2016/11/10 13:57.
*/
public class GifView extends View {
/**
* 默认为1秒
*/
private static final int DEFAULT_MOVIE_DURATION = 1000;
private int mMovieResourceId;
private Movie mMovie;
private long mMovieStart;
private int mCurrentAnimationTime = 0;
private float mLeft;
private float mTop;
private float mScale;
private int mMeasuredMovieWidth;
private int mMeasuredMovieHeight;
private boolean mVisible = true;
private volatile boolean mPaused = false;
public GifView(Context context) {
this(context, null);
}
public GifView(Context context, AttributeSet attrs) {
this(context, attrs, R.styleable.CustomTheme_gifViewStyle);
}
public GifView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setViewAttributes(context, attrs, defStyle);
}
@SuppressLint("NewApi")
private void setViewAttributes(Context context, AttributeSet attrs, int defStyle) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
// 从描述文件中读出gif的值,创建出Movie实例
final TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.GifView, defStyle, R.style.Widget_GifView);
mMovieResourceId = array.getResourceId(R.styleable.GifView_gif, -1);
mPaused = array.getBoolean(R.styleable.GifView_paused, false);
array.recycle();
if (mMovieResourceId != -1) {
mMovie = Movie.decodeStream(getResources().openRawResource(mMovieResourceId));
}
}
/**
* 设置gif图资源
*
* @param movieResId
*/
public void setMovieResource(int movieResId) {
this.mMovieResourceId = movieResId;
mMovie = Movie.decodeStream(getResources().openRawResource(mMovieResourceId));
requestLayout();
}
/**
* 设置GIF的路径
*
* @param gifPath
*/
public void setGifPath(String gifPath) {
byte[] bs = getGiftBytes(gifPath);
mMovie = Movie.decodeByteArray(getGiftBytes(gifPath), 0, bs.length);
requestLayout();
}
public void setMovie(Movie movie) {
this.mMovie = movie;
requestLayout();
}
public Movie getMovie() {
return mMovie;
}
public void setMovieTime(int time) {
mCurrentAnimationTime = time;
invalidate();
}
/**
* 设置暂停
*
* @param paused
*/
public void setPaused(boolean paused) {
this.mPaused = paused;
if (!paused) {
mMovieStart = android.os.SystemClock.uptimeMillis() - mCurrentAnimationTime;
}
invalidate();
}
/**
* 判断gif图是否停止了
*
* @return
*/
public boolean isPaused() {
return this.mPaused;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mMovie != null) {
int movieWidth = mMovie.width();
int movieHeight = mMovie.height();
int maximumWidth = MeasureSpec.getSize(widthMeasureSpec);
float scaleW = (float) movieWidth / (float) maximumWidth;
mScale = 1f / scaleW;
mMeasuredMovieWidth = maximumWidth;
mMeasuredMovieHeight = (int) (movieHeight * mScale);
setMeasuredDimension(mMeasuredMovieWidth, mMeasuredMovieHeight);
} else {
setMeasuredDimension(getSuggestedMinimumWidth(), getSuggestedMinimumHeight());
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
mLeft = (getWidth() - mMeasuredMovieWidth) / 2f;
mTop = (getHeight() - mMeasuredMovieHeight) / 2f;
mVisible = getVisibility() == View.VISIBLE;
}
@Override
protected void onDraw(Canvas canvas) {
if (mMovie != null) {
if (!mPaused) {
updateAnimationTime();
drawMovieFrame(canvas);
invalidateView();
} else {
drawMovieFrame(canvas);
}
}
}
@SuppressLint("NewApi")
private void invalidateView() {
if (mVisible) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
postInvalidateOnAnimation();
} else {
invalidate();
}
}
}
private void updateAnimationTime() {
long now = android.os.SystemClock.uptimeMillis();
// 如果第一帧,记录起始时间
if (mMovieStart == 0) {
mMovieStart = now;
}
// 取出动画的时长
int dur = mMovie.duration();
if (dur == 0) {
dur = DEFAULT_MOVIE_DURATION;
}
// 算出需要显示第几帧
mCurrentAnimationTime = (int) ((now - mMovieStart) % dur);
}
private void drawMovieFrame(Canvas canvas) {
// 设置要显示的帧,绘制即可
mMovie.setTime(mCurrentAnimationTime);
canvas.save(Canvas.MATRIX_SAVE_FLAG);
canvas.scale(mScale, mScale);
mMovie.draw(canvas, mLeft / mScale, mTop / mScale);
canvas.restore();
}
@SuppressLint("NewApi")
@Override
public void onScreenStateChanged(int screenState) {
super.onScreenStateChanged(screenState);
mVisible = screenState == SCREEN_STATE_ON;
invalidateView();
}
@SuppressLint("NewApi")
@Override
protected void onVisibilityChanged(View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
mVisible = visibility == View.VISIBLE;
invalidateView();
}
@Override
protected void onWindowVisibilityChanged(int visibility) {
super.onWindowVisibilityChanged(visibility);
mVisible = visibility == View.VISIBLE;
invalidateView();
}
/**
* 将gif图片转换成byte[]
*
* @return byte[]
*/
private byte[] getGiftBytes(String path) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = null;
try {
is = new FileInputStream(path);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
byte[] b = new byte[1024];
int len;
try {
while ((len = is.read(b, 0, 1024)) != -1) {
baos.write(b, 0, len);
}
baos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return baos.toByteArray();
}
}
GiFAdapter.java
package com.manboker.wb.imelayout.gif;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.manboker.wb.imelayout.R;
import java.util.ArrayList;
/**
* Date: 2016/11/10 17:22.
*/
public class GifAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
private ArrayList<String> mGifList;
public GifAdapter(Context context, ArrayList<String> gifs) {
mContext = context;
mGifList = gifs;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return mGifList.size();
}
@Override
public Object getItem(int i) {
return (Object) mGifList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder = null;
if (view == null) {
holder = new ViewHolder();
view = mInflater.inflate(R.layout.gallery_item, null);
holder.itemGallery = (GifView) view.findViewById(R.id.gallery_item_gif);
holder.itemText = (TextView) view.findViewById(R.id.gallery_item_text);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.itemGallery.setGifPath(mGifList.get(i));
holder.itemText.setText("" + i);
return view;
}
private class ViewHolder {
public GifView itemGallery;
public TextView itemText;
}
}
candidate_view.xml
<?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:background="@android:color/white"
android:orientation="vertical">
<Gallery
android:id="@+id/candidate_gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:spacing="2dp"/>
</LinearLayout>
gallery_item.xml
<?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="vertical">
<com.manboker.wb.imelayout.gif.GifView
android:id="@+id/gallery_item_gif"
android:layout_width="80dp"
android:layout_height="80dp"/>
<TextView
android:id="@+id/gallery_item_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"/>
</LinearLayout>
MyKeyboardView.java
package com.manboker.wb.imelayout.view;
import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import com.manboker.wb.imelayout.R;
import com.manboker.wb.imelayout.myinterface.IKeyBoard;
import java.util.Arrays;
/**
* Date: 2016/11/8 16:14.
*/
public class MyKeyboardView extends LinearLayout {
private final int LETTER_NUM = 26;//字母数量
private Context mContext;
private IKeyBoard mIKeyBoard;
private Button mButtonA, mButtonB, mButtonC, mButtonD, mButtonE, mButtonF, mButtonG, mButtonH, mButtonI, mButtonJ, mButtonK, mButtonL,
mButtonM, mButtonN, mButtonO, mButtonP, mButtonQ, mButtonR, mButtonS, mButtonT, mButtonU, mButtonV, mButtonW, mButtonX, mButtonY, mButtonZ;
private Button[] mLetterButtons = new Button[]{mButtonA, mButtonB, mButtonC, mButtonD, mButtonE, mButtonF, mButtonG, mButtonH, mButtonI, mButtonJ, mButtonK,
mButtonL, mButtonM, mButtonN, mButtonO, mButtonP, mButtonQ, mButtonR, mButtonS, mButtonT, mButtonU, mButtonV, mButtonW, mButtonX, mButtonY, mButtonZ};
private Integer[] mLetterButtonIds = new Integer[]{
R.id.keyboard_a, R.id.keyboard_b, R.id.keyboard_c, R.id.keyboard_d, R.id.keyboard_e, R.id.keyboard_f, R.id.keyboard_g, R.id.keyboard_h,
R.id.keyboard_i, R.id.keyboard_j, R.id.keyboard_k, R.id.keyboard_l, R.id.keyboard_m, R.id.keyboard_n, R.id.keyboard_o, R.id.keyboard_p,
R.id.keyboard_q, R.id.keyboard_r, R.id.keyboard_s, R.id.keyboard_t, R.id.keyboard_u, R.id.keyboard_v, R.id.keyboard_w, R.id.keyboard_x,
R.id.keyboard_y, R.id.keyboard_z};
private String mLowerletters;//字母小写
private String mUpperletters;//字母大写
private String mLowerNumber;//数字小写
private String mUpperNumber;//数字大写
private ImageButton mIBtnShift, mIBtnDelete, mIBtnDone, mIBtnSpace, mIBtnReturn;
private Button mBtnDot, mBtnComma;
private Button mBtnChange123;
private boolean mCapsLock, mNumberCapsLock;// true - 大写锁定
private View mLetterLayout, mNumberLayout;
public MyKeyboardView(Context context) {
super(context);
mContext = context;
init();
initABC();
init123();
showLetterLayout(true);
showNumberLayout(false);
}
private void init() {
// 加载布局
LayoutInflater.from(mContext).inflate(R.layout.main, this);
mLetterLayout = findViewById(R.id.main_letter);
mNumberLayout = findViewById(R.id.main_number);
mCapsLock = false;
mNumberCapsLock = false;
mLowerletters = "abcdefghijklmnopqrstuvwxyz";
mUpperletters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
mLowerNumber = "1234567890@#$%&*-=()!\"':;/?";
mUpperNumber = "~±×÷•°`´{}©£€^®¥_+[]¡<>¢|\\¿";
}
/**
* 初始化ABC布局
*/
private void initABC() {
for (int i = 0; i < LETTER_NUM; i++) {
mLetterButtons[i] = (Button) findViewById(mLetterButtonIds[i]);
mLetterButtons[i].setOnClickListener(new MyOnClickListener());
}
mBtnDot = (Button) findViewById(R.id.keyboard_dot);
mBtnDot.setOnClickListener(new MyOnClickListener());
mBtnComma = (Button) findViewById(R.id.keyboard_comma);
mBtnComma.setOnClickListener(new MyOnClickListener());
mIBtnShift = (ImageButton) findViewById(R.id.keyboard_shift);
mIBtnDelete = (ImageButton) findViewById(R.id.keyboard_delete);
mIBtnDone = (ImageButton) findViewById(R.id.keyboard_done);
mIBtnSpace = (ImageButton) findViewById(R.id.keyboard_space);
mIBtnReturn = (ImageButton) findViewById(R.id.keyboard_return);
mIBtnShift.setOnClickListener(new MyOnClickListener());
mIBtnDelete.setOnClickListener(new MyDeleteClickListener());
mIBtnDelete.setOnTouchListener(new MyDeleteClickListener());
mIBtnDone.setOnClickListener(new MyOnClickListener());
mIBtnSpace.setOnClickListener(new MyOnClickListener());
mIBtnReturn.setOnClickListener(new MyOnClickListener());
mBtnChange123 = (Button) findViewById(R.id.keyboard_change_123);
mBtnChange123.setOnClickListener(new MyOnClickListener());
}
private class MyOnClickListener implements OnClickListener {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.keyboard_a:
case R.id.keyboard_b:
case R.id.keyboard_c:
case R.id.keyboard_d:
case R.id.keyboard_e:
case R.id.keyboard_f:
case R.id.keyboard_g:
case R.id.keyboard_h:
case R.id.keyboard_i:
case R.id.keyboard_j:
case R.id.keyboard_k:
case R.id.keyboard_l:
case R.id.keyboard_m:
case R.id.keyboard_n:
case R.id.keyboard_o:
case R.id.keyboard_p:
case R.id.keyboard_q:
case R.id.keyboard_r:
case R.id.keyboard_s:
case R.id.keyboard_t:
case R.id.keyboard_u:
case R.id.keyboard_v:
case R.id.keyboard_w:
case R.id.keyboard_x:
case R.id.keyboard_y:
case R.id.keyboard_z:
clickLetter(view.getId());
break;
case R.id.keyboard_shift:
clickShift();
break;
case R.id.keyboard_num_shift:
clickNumberShift();
break;
case R.id.keyboard_done:
case R.id.keyboard_num_done:
mIKeyBoard.done();
break;
case R.id.keyboard_space:
case R.id.keyboard_num_space:
mIKeyBoard.setInputText(" ");
break;
case R.id.keyboard_return:
case R.id.keyboard_num_return:
mIKeyBoard.enter();
break;
case R.id.keyboard_dot://点
case R.id.keyboard_num_dot:
mIKeyBoard.setInputText(".");
break;
case R.id.keyboard_comma://逗号
case R.id.keyboard_num_comma:
mIKeyBoard.setInputText(",");
break;
case R.id.keyboard_change_123://切换数字键盘
showLetterLayout(false);
showNumberLayout(true);
if (mNumberCapsLock) {//大写转换成小写
clickNumberShift();
}
break;
case R.id.keyboard_num_1:
case R.id.keyboard_num_2:
case R.id.keyboard_num_3:
case R.id.keyboard_num_4:
case R.id.keyboard_num_5:
case R.id.keyboard_num_6:
case R.id.keyboard_num_7:
case R.id.keyboard_num_8:
case R.id.keyboard_num_9:
case R.id.keyboard_num_10:
case R.id.keyboard_num_11:
case R.id.keyboard_num_12:
case R.id.keyboard_num_13:
case R.id.keyboard_num_14:
case R.id.keyboard_num_15:
case R.id.keyboard_num_16:
case R.id.keyboard_num_17:
case R.id.keyboard_num_18:
case R.id.keyboard_num_19:
case R.id.keyboard_num_20:
case R.id.keyboard_num_21:
case R.id.keyboard_num_22:
case R.id.keyboard_num_23:
case R.id.keyboard_num_24:
case R.id.keyboard_num_25:
case R.id.keyboard_num_26:
case R.id.keyboard_num_27:
clickNumber(view.getId());
break;
case R.id.keyboard_num_change_abc://切换字母键盘
showNumberLayout(false);
showLetterLayout(true);
if (mCapsLock) {//如果是大写,则转换成小写
clickShift();
}
break;
}
}
}
public void setInterface(IKeyBoard iKeyBoard) {
mIKeyBoard = iKeyBoard;
}
/**
* 点击Shift
*/
private void clickShift() {
mCapsLock = !mCapsLock;
if (mCapsLock) {//大写锁定
mIBtnShift.setImageResource(R.mipmap.sym_keyboard_shift_click);
for (int i = 0; i < LETTER_NUM; i++) {
mLetterButtons[i].setText(String.valueOf(mUpperletters.charAt(i)));
}
} else {
mIBtnShift.setImageResource(R.mipmap.sym_keyboard_shift);
for (int i = 0; i < LETTER_NUM; i++) {
mLetterButtons[i].setText(String.valueOf(mLowerletters.charAt(i)));
}
}
}
/**
* 点击字母按键
*/
private void clickLetter(int id) {
int idx = Arrays.asList(mLetterButtonIds).indexOf(id);
if (idx == -1) {
return;
}
String word = "";
if (mCapsLock) {
word = String.valueOf(mUpperletters.charAt(idx));
} else {
word = String.valueOf(mLowerletters.charAt(idx));
}
mIKeyBoard.setInputText(word);
if (word.equals("h")){
mIKeyBoard.setCondiateShow(true);
}else{
mIKeyBoard.setCondiateShow(false);
}
}
private Handler mHandler = new Handler();
private CheckForLongPress mPendingCheckForLongPress = null;
/**
* 删除按钮事件监听
*/
private class MyDeleteClickListener implements OnClickListener, OnTouchListener {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.keyboard_delete:
case R.id.keyboard_num_delete:
mIKeyBoard.deleteInputOne();
break;
}
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (view.getId() == R.id.keyboard_delete || view.getId() == R.id.keyboard_num_delete) {
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
removeLongPressCallback();
} else if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
checkForLongClick();
}
}
return false;
}
}
private void checkForLongClick() {
if (mPendingCheckForLongPress == null) {
mPendingCheckForLongPress = new CheckForLongPress();
}
mHandler.postDelayed(mPendingCheckForLongPress, 1000);
}
class CheckForLongPress implements Runnable {
int currentKeycode = 0;
public void run() {
longPressDelete(currentKeycode);
mHandler.postDelayed(this, 50);
}
}
private void longPressDelete(int keycode) {
mIKeyBoard.deleteInputOne();
}
private void removeLongPressCallback() {
if (mPendingCheckForLongPress != null) {
mHandler.removeCallbacks(mPendingCheckForLongPress);
mPendingCheckForLongPress = null;
}
}
private final int NUMBER_NUM = 27;//数字数量
private Button mButton1, mButton2, mButton3, mButton4, mButton5, mButton6, mButton7, mButton8, mButton9, mButton10, mButton11, mButton12,
mButton13, mButton14, mButton15, mButton16, mButton17, mButton18, mButton19, mButton20, mButton21, mButton22, mButton23, mButton24,
mButton25, mButton26, mButton27;
private Button[] mNumberButtons = new Button[]{mButton1, mButton2, mButton3, mButton4, mButton5, mButton6, mButton7, mButton8, mButton9, mButton10, mButton11,
mButton12, mButton13, mButton14, mButton15, mButton16, mButton17, mButton18, mButton19, mButton20, mButton21, mButton22, mButton23, mButton24,
mButton25, mButton26, mButton27};
private Integer[] mNumberButtonIds = new Integer[]{
R.id.keyboard_num_1, R.id.keyboard_num_2, R.id.keyboard_num_3, R.id.keyboard_num_4, R.id.keyboard_num_5, R.id.keyboard_num_6, R.id.keyboard_num_7,
R.id.keyboard_num_8, R.id.keyboard_num_9, R.id.keyboard_num_10, R.id.keyboard_num_11, R.id.keyboard_num_12, R.id.keyboard_num_13, R.id.keyboard_num_14,
R.id.keyboard_num_15, R.id.keyboard_num_16, R.id.keyboard_num_17, R.id.keyboard_num_18, R.id.keyboard_num_19, R.id.keyboard_num_20, R.id.keyboard_num_21,
R.id.keyboard_num_22, R.id.keyboard_num_23, R.id.keyboard_num_24, R.id.keyboard_num_25, R.id.keyboard_num_26, R.id.keyboard_num_27};
private Button mNumberBtnDot, mNumberBtnComma;
private ImageButton mNumberIBtnShift, mNumberIBtnDelete, mNumberIBtnDone, mNumberIBtnSpace, mNumberIBtnReturn;
private Button mNumberBtnChangeabc;
/**
* 初始化数字键盘
*/
private void init123() {
for (int i = 0; i < NUMBER_NUM; i++) {
mNumberButtons[i] = (Button) findViewById(mNumberButtonIds[i]);
mNumberButtons[i].setOnClickListener(new MyOnClickListener());
}
mNumberBtnDot = (Button) findViewById(R.id.keyboard_num_dot);
mNumberBtnDot.setOnClickListener(new MyOnClickListener());
mNumberBtnComma = (Button) findViewById(R.id.keyboard_num_comma);
mNumberBtnComma.setOnClickListener(new MyOnClickListener());
mNumberIBtnShift = (ImageButton) findViewById(R.id.keyboard_num_shift);
mNumberIBtnDelete = (ImageButton) findViewById(R.id.keyboard_num_delete);
mNumberIBtnDone = (ImageButton) findViewById(R.id.keyboard_num_done);
mNumberIBtnSpace = (ImageButton) findViewById(R.id.keyboard_num_space);
mNumberIBtnReturn = (ImageButton) findViewById(R.id.keyboard_num_return);
mNumberIBtnShift.setOnClickListener(new MyOnClickListener());
mNumberIBtnDelete.setOnClickListener(new MyDeleteClickListener());
mNumberIBtnDelete.setOnTouchListener(new MyDeleteClickListener());
mNumberIBtnDone.setOnClickListener(new MyOnClickListener());
mNumberIBtnSpace.setOnClickListener(new MyOnClickListener());
mNumberIBtnReturn.setOnClickListener(new MyOnClickListener());
mNumberBtnChangeabc = (Button) findViewById(R.id.keyboard_num_change_abc);
mNumberBtnChangeabc.setOnClickListener(new MyOnClickListener());
}
/**
* 点击字母按键
*/
private void clickNumber(int id) {
int idx = Arrays.asList(mNumberButtonIds).indexOf(id);
if (idx == -1) {
return;
}
if (mNumberCapsLock) {
mIKeyBoard.setInputText(String.valueOf(mUpperNumber.charAt(idx)));
} else {
mIKeyBoard.setInputText(String.valueOf(mLowerNumber.charAt(idx)));
}
}
/**
* 点击Shift
*/
private void clickNumberShift() {
mNumberCapsLock = !mNumberCapsLock;
if (mNumberCapsLock) {//大写锁定
mNumberIBtnShift.setImageResource(R.mipmap.sym_keyboard_shift_click);
for (int i = 0; i < NUMBER_NUM; i++) {
mNumberButtons[i].setText(String.valueOf(mUpperNumber.charAt(i)));
}
} else {
mNumberIBtnShift.setImageResource(R.mipmap.sym_keyboard_shift);
for (int i = 0; i < NUMBER_NUM; i++) {
mNumberButtons[i].setText(String.valueOf(mLowerNumber.charAt(i)));
}
}
}
/**
* 隐藏字母键盘布局
*
* @param b
*/
private void showLetterLayout(boolean b) {
int show = View.VISIBLE;
if (!b) {
show = View.GONE;
}
mLetterLayout.setVisibility(show);
}
/**
* 隐藏数字键盘布局
*
* @param b
*/
private void showNumberLayout(boolean b) {
int show = View.VISIBLE;
if (!b) {
show = View.GONE;
}
mNumberLayout.setVisibility(show);
}
}
使用到的接口:
package com.manboker.wb.imelayout.myinterface;
/**
* Date: 2016/11/8 16:42.
*/
public interface IKeyBoard {
public void setInputText(String text);
public void deleteInputOne();
public void enter();
public void done();
public void setCondiateShow(boolean b);
}
3.以上就是自定义输入法的全部代码,GIF图大家可以自己找几张放到手机里面。
注意:
- GifView读取本地GIF图片的时候,一定要调用:
private byte[] getGiftBytes(String path)
这个方法来解码。 - 这个Demo在输入小写字母h的时候可以弹出候选词列表,可以定义其他的单词或方式。
以下是效果图