在开发股票App的时候可能会被要求使用股票键盘,前一阵从不炒股得人第一次听说了股票键盘,后来查阅了一下,其实就是自定义的键盘,奈何一只搜索股票键盘,难住了我,所以我写了一个股票键盘。
话不多说直接上马了:
首先要先写一个布局(就是在要显示键盘的类的布局里):
其中 KeyboardView键盘的控件。
接下来写一个键盘类:
import android.app.Activity;
import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import java.util.List;
import cn.uniwa.uniwa.R;
public class KeyBoardNumber {
private KeyboardView keyboardView;
private Keyboard k1;// 英文键盘
private Keyboard k2;// 数字键盘
private EditText ed;
boolean b = true;
Activity a;//接收传过来Activity
LinearLayout ll;
ImageView hotIvDel;
LinearLayout hotLlResult;
RelativeLayout hotRlNoFind;
public KeyBoardNumber(Activity act, Context ctx, EditText edit) {//Activity可以将使用到此键盘的类传过来,也可以删除不传
this.ed = edit;
a = act;
k1 = new Keyboard(ctx, R.xml.letter);
k2 = new Keyboard(ctx, R.xml.symbols);
keyboardView = (KeyboardView) act.findViewById(R.id.hot_keyboard_view);
keyboardView.setKeyboard(k2);
//软键盘是否启用
keyboardView.setEnabled(true);
//按键提示是否启用
keyboardView.setPreviewEnabled(false);
keyboardView.setOnKeyboardActionListener(listener);
List keylist = k1.getKeys();
for (Keyboard.Key key : keylist) {
if (key.label != null && isword(key.label.toString())) {
key.label = key.label.toString().toUpperCase();
key.codes[0] = key.codes[0] - 32;
}
}
}
private KeyboardView.OnKeyboardActionListener listener = new KeyboardView.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) {//注意!设置的键盘按钮可以再此方法内设置
ll = (LinearLayout) a.findViewById(R.id.hot_ll_text);
Editable editable = ed.getText();
int start = ed.getSelectionStart();
if (primaryCode == Keyboard.KEYCODE_CANCEL) {// 完成
// Toast.makeText(KeyBoardNumber.this,"ok+"+editable,Toast.LENGTH_LONG).show();
// hideKeyboard();
} else if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退
if (editable != null && editable.length() > 0) {
if (start > 0) {
editable.delete(start - 1, start);
// hotIvDel.setVisibility(View.GONE);
// hotLlResult.setVisibility(View.GONE);
// ll.setVisibility(View.VISIBLE);
// hotRlNoFind.setVisibility(View.GONE);
}
}
} else if (primaryCode == -15) {
EditText ed = (EditText) a.findViewById(R.id.hot_et_search);//这些方法用不到可以删除,这是从使用键盘的activity传来的id
hotLlResult = (LinearLayout) a.findViewById(R.id.hot_ll_result);
hotRlNoFind = (RelativeLayout) a.findViewById(R.id.hot_rl_noFind);
String text = ed.getText().toString();
if (text.equals("")||text == null){
hotIvDel.setVisibility(View.GONE);
hotLlResult.setVisibility(View.GONE);
ll.setVisibility(View.VISIBLE);
hotRlNoFind.setVisibility(View.GONE);
}
hideKeyboard();
// ll.setVisibility(View.VISIBLE);
// ed.setText("");
} else if (primaryCode == 4896) {// 清空
editable.clear();
} else if (primaryCode == 002) {
editable.insert(start, "002");
} else if (primaryCode == 300) {
editable.insert(start, "300");
} else if (primaryCode == 600) {
editable.insert(start, "600");
} else if (primaryCode == 601) {
editable.insert(start, "601");
} else if (primaryCode == 000) {
editable.insert(start, "000");
} else if (primaryCode == 115) {
editable.insert(start, "S");
} else if (primaryCode == 8016) {
// editable.insert(start, "16");
if (b) {
keyboardView.setKeyboard(k1);
b = false;
} else {
keyboardView.setKeyboard(k2);
b = true;
}
} 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);
}
}
private boolean isword(String str) {
String wordstr = "abcdefghijklmnopqrstuvwxyz";
if (wordstr.indexOf(str.toLowerCase()) > -1) {
return true;
}
return false;
}
注意看我写的注释应该就能明白了
接下来是英文键盘:
数字键盘:
以上键盘设置完毕,只要在Activity中使用即可:
private KeyBoardNumber keyBoard;
keyBoard = new KeyBoardNumber(act, mContext, hotEtSearch);
hotEtSearch.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { int inputback = hotEtSearch.getInputType(); // hotEtSearch.setInputType(InputType.TYPE_NULL); keyBoard.showKeyboard(); hotEtSearch.setInputType(inputback); return false; } });这样就会显示键盘了;
但还是要注意,键盘布局的显示和隐藏;
如果有问题可以私聊我,会尽力帮忙。。