最近项目需要做一个自定义的股票代码输入软键盘,自己也学习了一下,
这里总共做了两个demo,一个是纯数字的软键盘,一个是可以切换字母和
数字的软键盘,大家共同学习!!!
这里是效果展示
1、自定义步骤
首先你需要这个文件,注意这两个文件是不能够修改名称的,只能是这个名
qwerty代表数字键盘
symbols代表是字母键盘
这里时里面的内容
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="20%p" android:horizontalGap="0px"
android:verticalGap="0px" android:keyHeight="8%p">
<!-- 这个就像是表格布局一样,每个Row都是一行,上面的一些设置全局的属性 8%p代表父布局的8% -->
<Row>
<Key android:codes="8150" android:keyLabel="150" />
<Key android:codes="49" android:keyLabel="1" />
<Key android:codes="50" android:keyLabel="2" />
<Key android:codes="51" android:keyLabel="3" />
<Key android:codes="-5" android:isRepeatable="true"
android:keyIcon="@drawable/sym_keyboard_delete" />
</Row>
<Row>
<Key android:codes="8160" android:keyLabel="160" />
<Key android:codes="52" android:keyLabel="4" />
<Key android:codes="53" android:keyLabel="5" />
<Key android:codes="54" android:keyLabel="6" />
<Key android:codes="4896" android:keyLabel="@string/key_clear" />
</Row>
<Row>
<Key android:codes="8161" android:keyLabel="161" />
<Key android:codes="55" android:keyLabel="7" />
<Key android:codes="56" android:keyLabel="8" />
<Key android:codes="57" android:keyLabel="9" />
<Key android:codes="-3" android:keyHeight="16%p"
android:keyEdgeFlags="right" android:isRepeatable="true"
android:keyLabel="@string/key_ok" />
</Row>
<Row>
<Key android:codes="8016" android:keyLabel="16" />
<Key android:codes="57419"
android:keyIcon="@drawable/sym_keyboard_left" />
<Key android:codes="48" android:keyLabel="0" />
<Key android:codes="57421"
android:keyIcon="@drawable/sym_keyboard_right" />
</Row>
</Keyboard>
然后你需要这个类
KeyBoardNumber
package com.dds.softkeynumber;
import android.app.Activity;
import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
public class KeyBoardNumber {
private KeyboardView keyboardView;
private Keyboard k2;// 股票键盘
private EditText ed;
public KeyBoardNumber(Activity act, Context ctx, EditText edit) {
this.ed = edit;
k2 = new Keyboard(ctx, R.xml.symbols);
keyboardView = (KeyboardView) act.findViewById(R.id.keyboard_view);
keyboardView.setKeyboard(k2);
keyboardView.setEnabled(true);
keyboardView.setPreviewEnabled(true);
keyboardView.setOnKeyboardActionListener(listener);
}
private OnKeyboardActionListener listener = new OnKeyboardActionListener() {
@Override
public void swipeUp() {
}
@Override
public void swipeRight() {
}
@Override
public void swipeLeft() {
}
@Override
public void swipeDown() {
}
@Override
public void onText(CharSequence text) {
}
@Override
public void onRelease(int primaryCode) {
}
@Override
public void onPress(int primaryCode) {
}
@Override
public void onKey(int primaryCode, int[] keyCodes) {
//这里是关键代码,如果是特殊键,你需要定义每个键的事件,如果说不是特殊键,照常输入
Editable editable = ed.getText();
int start = ed.getSelectionStart();
if (primaryCode == Keyboard.KEYCODE_CANCEL) {// 完成
hideKeyboard();
} else if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退
if (editable != null && editable.length() > 0) {
if (start > 0) {
editable.delete(start - 1, start);
}
}
} else if (primaryCode == 4896) {// 清空
editable.clear();
} else if (primaryCode == 57419) { // go left
if (start > 0) {
ed.setSelection(start - 1);
}
} else if (primaryCode == 57421) { // go right
if (start < ed.length()) {
ed.setSelection(start + 1);
}
} else if (primaryCode == 8150) {
editable.insert(start, "150");
} else if (primaryCode == 8160) {
editable.insert(start, "160");
} else if (primaryCode == 8161) {
editable.insert(start, "161");
} else if (primaryCode == 8016) {
editable.insert(start, "16");
}
else {
editable.insert(start, Character.toString((char) primaryCode));
}
}
};
public boolean isKeyBoardShow() {
int visibility = keyboardView.getVisibility();
if (visibility == View.GONE || visibility == View.INVISIBLE) {
return false;
} else {
return true;
}
}
public void showKeyboard() {
int visibility = keyboardView.getVisibility();
if (visibility == View.GONE || visibility == View.INVISIBLE) {
keyboardView.setVisibility(View.VISIBLE);
}
}
public void hideKeyboard() {
int visibility = keyboardView.getVisibility();
if (visibility == View.VISIBLE) {
keyboardView.setVisibility(View.INVISIBLE);
}
}
}
好了,都到了这一步了,最后剩下的就是使用了,
首先布局为
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="cn.key.NumberActivity" >
<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</EditText>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<android.inputmethodservice.KeyboardView
android:id="@+id/keyboard_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/lightblack"
android:focusable="true"
android:focusableInTouchMode="true"
android:keyBackground="@drawable/btn_keyboard_key"
android:keyTextColor="@color/white"
android:visibility="gone" />
</RelativeLayout>
</LinearLayout>
然后是代码:
MainActivity
package com.dds.softkeynumber;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;
/**
* 测试股票类代码
*
* @author dds
*
*/
public class MainActivity extends Activity {
private EditText edit;
private Context ctx;// 上下文
private Activity act;// 本实例对象
private KeyBoardNumber keyBoard;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText) findViewById(R.id.edit);
ctx = this;
act = this;
keyBoard = new KeyBoardNumber(act, ctx, edit);
// 设置其触摸事件
edit.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int inputback = edit.getInputType();
edit.setInputType(InputType.TYPE_NULL);
keyBoard.showKeyboard();
edit.setInputType(inputback);
return false;
}
});
}
@Override
public void onBackPressed() {
/**
* 为了增加用户体验,还是加个这样的处理
*/
if (!keyBoard.isKeyBoardShow()) {
super.onBackPressed();
} else {
keyBoard.hideKeyboard();
}
}
}
使用方法全在这里了,你要是项扩展的话也很简单,随便看一看就会了。
这里放上两个源码的地址
GitHUb: SoftKeyStock