直接上功能图片有图有真相
该功能主要是通过抄作android自带控件KeyboardView实现的
先上控制布局文件代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_keyboard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
>
<EditText
android:id="@+id/et_number"
android:layout_width="match_parent"
android:textColor="@color/color_212121"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:maxLength="11"
android:inputType="numberDecimal"/>
<android.inputmethodservice.KeyboardView
android:id="@+id/keyboardview"
android:layout_margin="2dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:background="@color/color_f8a899"
android:keyBackground="@drawable/calculator_button_bg"
android:keyTextColor="@color/color_212121"
android:keyTextSize="26.0sp"
android:shadowColor="@color/color_ffffff"
android:shadowRadius="0"
/>
</RelativeLayout>
接着是Activity的代码
package com.kxiang.quick.function.activity.keboard;
import android.os.Bundle;
import android.text.InputType;
import android.view.WindowManager;
import android.widget.EditText;
import com.kxiang.quick.R;
import com.kxiang.quick.base.BaseActivity;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class KeyboardActivity extends BaseActivity {
@Override
protected int getContentView() {
return R.layout.activity_keyboard;
}
private EditText et_number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
et_number = (EditText) findViewById(R.id.et_number);
hideSoftInputMethod(et_number);
new KeyboardUtils(thisActivity, et_number);
}
// 隐藏系统键盘并且让光标存在
public void hideSoftInputMethod(EditText ed) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
int currentVersion = android.os.Build.VERSION.SDK_INT;
String methodName = null;
if (currentVersion >= 16) {
// 4.2
methodName = "setShowSoftInputOnFocus";
}
else if (currentVersion >= 14) {
// 4.0
methodName = "setSoftInputShownOnFocus";
}
if (methodName == null) {
ed.setInputType(InputType.TYPE_NULL);
}
else {
Class<EditText> cls = EditText.class;
Method setShowSoftInputOnFocus;
try {
setShowSoftInputOnFocus = cls.getMethod(methodName, boolean.class);
setShowSoftInputOnFocus.setAccessible(true);
setShowSoftInputOnFocus.invoke(ed, false);
} catch (NoSuchMethodException e) {
ed.setInputType(InputType.TYPE_NULL);
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
下面是对KeyboardView类的一些操作,上KeyboardUtils方法
package com.kxiang.quick.function.activity.keboard;
import android.app.Activity;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.text.Editable;
import android.widget.EditText;
import com.kxiang.quick.R;
import com.kxiang.quick.utils.LogUtils;
/**
* 项目名称:RapidDevelopment
* 创建人:kexiang
* 创建时间:2017/3/1 11:15
*/
public class KeyboardUtils {
private Keyboard symbols;// 数字键盘
private EditText showNumber;
private KeyboardView keyboardView;
private boolean point = false;
private int pointNumber = 0;
public KeyboardUtils(Activity activity, EditText showNumber) {
this.showNumber = showNumber;
symbols = new Keyboard(activity, R.xml.symbols);
keyboardView = (KeyboardView) activity.findViewById(R.id.keyboardview);
keyboardView.setKeyboard(symbols);
keyboardView.setEnabled(true);
keyboardView.setPreviewEnabled(false);
keyboardView.setOnKeyboardActionListener(keyboardListener);
}
private KeyboardView.OnKeyboardActionListener keyboardListener = new KeyboardView.OnKeyboardActionListener() {
private final int NUMBER = 2;
@Override
public void onPress(int primaryCode) {
LogUtils.toE("onPress:" + primaryCode);
}
@Override
public void onRelease(int primaryCode) {
LogUtils.toE("onRelease:" + primaryCode);
}
//这个方法是对只能输入数字切小数后面只能有两位小数做判断并且显示到界面上
@Override
public void onKey(int primaryCode, int[] keyCodes) {
LogUtils.toE("onKey:" + primaryCode);
Editable editable = showNumber.getText();
int start = showNumber.getSelectionStart();
int stringLength = editable.length();
LogUtils.toE("kaishi", "start:" + start);
if (primaryCode == -5) {
if (point && ((stringLength - pointNumber - start) <= 0)) {
pointNumber--;
}
if (start > 0) {
String atString = editable.charAt(start - 1) + "";
LogUtils.toE("kaishi", "atString:" + atString);
if (atString.equals(".")) {
point = false;
pointNumber = 0;
}
}
if (editable != null && start > 0)
editable.delete(start - 1, start);
}
else {
String select = Character.toString((char) primaryCode);
LogUtils.toE("pointNumber:" + pointNumber);
if (select.equals(".")) {
if (!point) {
editable.insert(start, select);
}
point = true;
}
else {
if (point) {
int spiltNumber = stringLength - pointNumber - start;
LogUtils.toE("kaishi", "spiltNumber:" + spiltNumber);
LogUtils.toE("kaishi", "spiltNumber:" + pointNumber);
LogUtils.toE("kaishi", "spiltNumber:" + start);
if (spiltNumber > 0) {
editable.insert(start, select);
}
else {
if (pointNumber < NUMBER) {
editable.insert(start, select);
if (pointNumber == NUMBER) {
pointNumber = NUMBER;
}
else {
pointNumber++;
}
}
}
}
else {
editable.insert(start, select);
}
}
}
}
@Override
public void onText(CharSequence text) {
LogUtils.toE("onText:" + text);
}
@Override
public void swipeLeft() {
LogUtils.toE("swipeLeft:");
}
@Override
public void swipeRight() {
LogUtils.toE("swipeRight:");
}
@Override
public void swipeDown() {
LogUtils.toE("swipeDown:");
}
@Override
public void swipeUp() {
LogUtils.toE("swipeUp:");
}
};
}
最后是KeyboardView的一些配置
symbols.xml:数字控件显示
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="33.33%p"
android:horizontalGap="0px"
android:verticalGap="0px"
android:keyHeight="50dp">
<Row>
<Key
android:codes="49"
android:keyLabel="1"/>
<Key
android:codes="50"
android:keyLabel="2"/>
<Key
android:codes="51"
android:keyLabel="3"/>
</Row>
<Row>
<Key
android:codes="52"
android:keyLabel="4"/>
<Key
android:codes="53"
android:keyLabel="5"/>
<Key
android:codes="54"
android:keyLabel="6"/>
</Row>
<Row>
<Key
android:codes="55"
android:keyLabel="7"/>
<Key
android:codes="56"
android:keyLabel="8"/>
<Key
android:codes="57"
android:keyLabel="9"/>
</Row>
<Row>
<Key
android:codes="46"
android:keyLabel="."/>
<Key
android:codes="48"
android:keyLabel="0"/>
<Key
android:codes="-5"
android:isRepeatable="true"
android:keyIcon="@drawable/emoji_del"/>
</Row>
</Keyboard>
keyboard_button_bg.xml:点击各个位置的显示颜色
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<!--圆角大小-->
<corners
android:radius="4dp"
/>
<!--距离-->
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp"
/>
<!--边框颜色-->
<stroke
android:color="@color/color_no"
android:width="4dp"
/>
<!--填充颜色-->
<solid android:color="@color/color_b4b4b4"/>
</shape>
</item>
<item android:state_enabled="false">
<shape>
<corners
android:radius="4dp"
/>
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp"
/>
<stroke
android:color="@color/color_no"
android:width="4dp"
/>
<solid android:color="@color/color_ffffff"/>
</shape>
</item>
<item>
<shape>
<corners
android:radius="4dp"
/>
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp"
/>
<solid android:color="@color/color_ffffff"/>
<stroke
android:width="4dp"
android:color="@color/color_no"
/>
</shape>
</item>
</selector>