1.首先在res目录下新建文件夹xml,并在xml文件夹下创建3个文件,分别配置省、市、车牌号信息
(1)province_abbreviation.xml
(2)letters.xml
(3)number_or_letters.xml
2.写键盘工具类KeybordUtil
public class Keyboard_Util {
private Context mContext;
private Activity mActivity;
private KeyboardView mKeyboardView;
private EditText mEdit;
private int keybordType;
/**
* 省份简称键盘
*/
private Keyboard province_keyboard;
/**
* 数字与大写字母键盘
*/
private Keyboard number_keyboar;
/**
* 字母键盘
*/
private Keyboard letter_keybord;
public Keyboard_Util(Activity activity, EditText edit) {
mActivity = activity;
mContext = activity;
mEdit = edit;
province_keyboard = new Keyboard(mContext, R.xml.province_abbreviation);
number_keyboar = new Keyboard(mContext, R.xml.number_or_letters);
letter_keybord = new Keyboard(mContext, R.xml.letters);
mKeyboardView = (KeyboardView) activity.findViewById(R.id.keyboard_view);
mKeyboardView.setEnabled(true);
mKeyboardView.setPreviewEnabled(false);
mKeyboardView.setOnKeyboardActionListener(listener);
}
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) {
Editable editable = mEdit.getText();
int start = mEdit.getSelectionStart();
if (primaryCode == -3) {
if (editable != null && editable.length() > 0) {
if (start > 0) {
editable.delete(start - 1, start);
}
}
} else {
if (keybordType==0||keybordType==1){
editable.replace(0,editable.length(),Character.toString((char) primaryCode));
}else {
editable.insert(start, Character.toString((char) primaryCode));
}
}
}
};
/**
* 切换键盘
*
* @param keybordType
*/
public void changeKeybord(int keybordType) {
this.keybordType=keybordType;
switch (keybordType) {
case 0:
mKeyboardView.setKeyboard(province_keyboard);
break;
case 1:
mKeyboardView.setKeyboard(letter_keybord);
break;
case 2:
mKeyboardView.setKeyboard(number_keyboar);
break;
}
}
/**
* 软键盘展示状态
*/
public boolean isShow() {
return mKeyboardView.getVisibility() == View.VISIBLE;
}
/**
* 软键盘展示
*/
public void showKeyboard() {
int visibility = mKeyboardView.getVisibility();
if (visibility == View.GONE || visibility == View.INVISIBLE) {
mKeyboardView.setVisibility(View.VISIBLE);
}
}
/**
* 软键盘隐藏
*/
public void hideKeyboard() {
int visibility = mKeyboardView.getVisibility();
if (visibility == View.VISIBLE) {
mKeyboardView.setVisibility(View.INVISIBLE);
}
}
/**
* 禁掉系统软键盘
*/
public void hideSoftInputMethod() {
mActivity.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) {
mEdit.setInputType(InputType.TYPE_NULL);
} else {
Class cls = EditText.class;
Method setShowSoftInputOnFocus;
try {
setShowSoftInputOnFocus = cls.getMethod(methodName,
boolean.class);
setShowSoftInputOnFocus.setAccessible(true);
setShowSoftInputOnFocus.invoke(mEdit, false);
} catch (NoSuchMethodException e) {
mEdit.setInputType(InputType.TYPE_NULL);
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
3.布局
4.使用
public class MainActivity extends AppCompatActivity {
private Keyboard_Util keyboardUtil;
private EditText et_searchtxt;
private EditText et_province;
private EditText et_city;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_province=findViewById(R.id.et_province);
et_city=findViewById(R.id.et_city);
et_searchtxt = (EditText) findViewById(R.id.et_searchtxt);
et_province.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
keyboardUtil = new Keyboard_Util(MainActivity.this, et_province);
keyboardUtil.hideSoftInputMethod();
keyboardUtil.changeKeybord(0);
keyboardUtil.showKeyboard();
return false;
}
});
et_province.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
et_city.requestFocus();
et_city.setFocusable(true);
keyboardUtil = new Keyboard_Util(MainActivity.this, et_city);
keyboardUtil.hideSoftInputMethod();
keyboardUtil.changeKeybord(1);
keyboardUtil.showKeyboard();
}
});
et_city.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
keyboardUtil = new Keyboard_Util(MainActivity.this, et_city);
keyboardUtil.hideSoftInputMethod();
keyboardUtil.changeKeybord(1);
keyboardUtil.showKeyboard();
return false;
}
});
et_city.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.length()>0){
et_searchtxt.requestFocus();
et_searchtxt.setFocusable(true);
keyboardUtil = new Keyboard_Util(MainActivity.this, et_searchtxt);
keyboardUtil.hideSoftInputMethod();
keyboardUtil.changeKeybord(2);
keyboardUtil.showKeyboard();
}
}
});
et_searchtxt.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
keyboardUtil = new Keyboard_Util(MainActivity.this, et_searchtxt);
keyboardUtil.hideSoftInputMethod();
keyboardUtil.changeKeybord(2);
keyboardUtil.showKeyboard();
return false;
}
});
}
}