1. 程式人生 > >android自定義鍵盤 限制車牌號碼輸入

android自定義鍵盤 限制車牌號碼輸入

執行效果:

     

核心程式碼:

<?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地址:點選開啟連結