运行效果:
核心代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="me.kevingo.licensekeyboard.KeybordActivity">
<EditText
android:focusableInTouchMode="false"
android:cursorVisible="false"
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.inputmethodservice.KeyboardView
android:paddingBottom="1dp"
android:paddingLeft="1dp"
android:paddingRight="1dp"
android:paddingTop="4dp"
android:background="@color/grey"
android:layout_alignParentBottom="true"
android:visibility="gone"
android:keyPreviewOffset="-5dp"//提示框偏移量
android:keyPreviewHeight="40dp"//提示框高度
android:keyPreviewLayout="@layout/preview"//设置键盘按下后的提示布局
android:id="@+id/kb"
android:keyTextColor="@color/black"//键盘字体颜色
android:keyBackground="@drawable/se"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
public class KeybordActivity extends AppCompatActivity {
private int current;
private EditText et;
private KeyboardView keyboardView;
private List<String> content;
private String[] prinvces = {"京", "津", "冀", "鲁", "晋", "蒙", "辽", "吉", "黑"
, "沪", "苏", "浙", "皖", "闽", "赣", "豫", "鄂", "湘"
, "粤", "桂", "渝", "川", "贵", "云", "藏", "陕", "甘"
, "青", "琼", "新", "港", "澳", "台", "宁"};
private String[] abcs = {
"Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"
, "A", "S", "D", "F", "G", "H", "J", "K", "L"
, "Z", "X", "C", "V", "B", "N", "M"};
private String[] abcs123 = {"0","1", "2", "3", "4", "5", "6", "7", "8", "9"
, "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"
, "A", "S", "D", "F", "G", "H", "J", "K", "L"
, "Z", "X", "C", "V", "B", "N", "M"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_keybord);
content = new ArrayList<>();
keyboardView = (KeyboardView)findViewById(R.id.kb);
et = (EditText)findViewById(R.id.et);
keyboardView.setKeyboard(new Keyboard(this,R.xml.province_short_keyboard));
keyboardView.setEnabled(true);
et.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
keyboardView.setVisibility(View.VISIBLE);
}
});
keyboardView.setOnKeyboardActionListener(new KeyboardView.OnKeyboardActionListener() {
@Override
public void onPress(int i) {
}
@Override
public void onRelease(int i) {
if(et.getText().length()==0){
keyboardView.setKeyboard(new Keyboard(KeybordActivity.this,R.xml.province_short_keyboard));
}else if(et.getText().length()==1){
keyboardView.setKeyboard(new Keyboard(KeybordActivity.this,R.xml.abc));
}else{
keyboardView.setKeyboard(new Keyboard(KeybordActivity.this,R.xml.abc123));
}
}
@Override
public void onKey(int i, int[] ints) {
//监听键盘被点击的方法 i就是xml文件中定义的code
if(i == 112){ //xml中定义的删除键值为112
if(current==1){
content.clear();
}else if(current==0){
}else{
content.remove(current-1);
}
current = content.size();
}else if(i == -3){ //系统默认的关闭键盘的code为-3
keyboardView.setVisibility(View.GONE);
}else{
if(content.size()< 7){
if(current==0){
content.add(prinvces[i]);
}else if(current==1){
content.add(abcs[i]);
}else{
content.add(abcs123[i]);
}
}else{
Toast.makeText(KeybordActivity.this,"车牌号最多为7位",Toast.LENGTH_SHORT).show();
}
}
current = content.size();
et.setText(contentToString());
}
@Override
public void onText(CharSequence charSequence) {
}
@Override
public void swipeLeft() {
}
@Override
public void swipeRight() {
}
@Override
public void swipeDown() {
}
@Override
public void swipeUp() {
}
});
}
private String contentToString(){
String temp = "";
if(content.size()==0){
return "";
}else{
for(String s:content){
temp = temp+s;
}
}
return temp;
}
}
demo地址: 点击打开链接