android 实现自定义键盘的实例

一、概述 

android有各种输入法 和键盘。但 在某些特定场景中,需要自定义键盘布局,例如乱序键盘、输入指定字串的键盘等。在此,以实现输入身份证号的自定义键盘为例,简述android实现自定义软键盘。

二、实战

1、编写键盘xml布局文件 symbols.xml

其中,android:code=" 按键所对应值的ascii码 "

            android:keyLabel=" 按键上显示的字符 "

            android:keyIcon="按键上显示的图标"

xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    android:horizontalGap="0px"
    android:keyHeight="50dp"
    android:keyWidth="33.333333333333%p"
    android:verticalGap="0px">
    
                    android:codes="49"
            android:keyLabel="1" />
                    android:codes="50"
            android:keyLabel="2" />
                    android:codes="51"
            android:keyEdgeFlags="right"
            android:keyLabel="3" />
    
    
                    android:codes="52"
            android:keyLabel="4" />
                    android:codes="53"
            android:keyLabel="5" />
                    android:codes="54"
            android:keyEdgeFlags="right"
            android:keyLabel="6" />
    
    
                    android:codes="55"
            android:keyLabel="7" />
                    android:codes="56"
            android:keyLabel="8" />
                    android:codes="57"
            android:keyEdgeFlags="right"
            android:keyLabel="9" />
    
    
                    android:codes="88"
            android:keyLabel="X" />
                    android:codes="48"
            android:keyLabel="0" />
                    android:codes="-5"
            android:isRepeatable="true"
            android:keyIcon="@drawable/delete_img" />
    
 

2、编写调用自定义键盘的布局文件 board_show_layout.xml

在需要调用第三方键盘的layout中,添加自定义的键盘

在此,自定义键盘的id我设为 kewboard_vew

xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

            android:id="@+id/et_soft"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

            android:layout_width="fill_parent"
        android:layout_height="wrap_content">

                    android:id="@+id/keyboard_view"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@color/white"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:keyTextColor="@color/new_color_1"
            android:keyTextSize="25sp"
            android:shadowColor="@color/white"
            android:shadowRadius="0.0"
            android:visibility="gone" />
    


3、编写自定义键盘类,实现按键的监听

自定义按键类的构造方法需要 传入 调用自定义键盘的activity,上下文,对应的编辑框

public class SoftBoard {
    
   private Activity act;
   private Context ctx;
    private KeyboardView keyboardView;
    private Keyboard k;
    private EditText ed;

    //构造方法
    public SoftBoard(Activity act, Context ctx, EditText edit) {
       
        this.act = act;
        this.ctx = ctx;
       this.ed = edit;

        //将自定义的键盘布局symbols.xml填充到Keyboard布局中
        k = new Keyboard(ctx, R.xml.symbols);
        keyboardView = (KeyboardView) act.findViewById(R.id.keyboard_view);
        keyboardView.setKeyboard(k);
        
        keyboardView.setEnabled(true);

        //启用或禁用按键按下后 放大弹出的按键阅览
        keyboardView.setPreviewEnabled(false);

        //设置按键监听
        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_DELETE) {// 回退
                        if (editable != null && editable.length() > 0) {  
                                if (start > 0) {  
                                        editable.delete(start - 1, start);  
                                }  
                        }  
                 } else { //将要输入的数字现在编辑框中 
                                editable.insert(start, Character.toString((char) primaryCode));
                 }  
         }  
        };  
       
    //显示自定义键盘
    public void showKeyboard() {  
       //InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
      //imm.hideSoftInputFromWindow(act.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        int visibility = keyboardView.getVisibility();  
        if (visibility == View.GONE || visibility == View.INVISIBLE) {
            keyboardView.setVisibility(View.VISIBLE);
        }  
        keyboardView.bringToFront();
    } 
    
    //隐藏自定义键盘
    public void hideKeyboard(){
       int visibility = keyboardView.getVisibility();  
        if (visibility == View.VISIBLE) {
            keyboardView.setVisibility(View.GONE);
        }  
    }
    
    //自定义键盘是否显示
    public boolean isShowing(){
       return keyboardView.getVisibility() == View.VISIBLE;
    }
}  


4、调用自定义键盘

1、在所需要调用自定义的activity的布局文件(步骤2中的 board_show_layout.xml)中,写入自定义键盘控件

2、在activity的类文件中调用自定义键盘

public class SoftBoardActivity extends Activity{

    private EditText mEt;
    private SoftBoard id_keyboard;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.soft_show_layout);
        findView();
    }

    private void findView(){
        mEt = (EditText) findViewById(R.id.et_soft);
        id_keyboard = new SoftBoard(SoftBoardActivity.this,this,mEt);
        id_keyboard.showKeyboard();

        mEt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!id_keyboard.isShowing())
                    id_keyboard.showKeyboard();
            }
        });

        //反射解决光标跳转问题
        Class  cls = EditText.class;
        try{
            Method setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus",boolean.class);
            setShowSoftInputOnFocus.setAccessible(false);
            setShowSoftInputOnFocus.invoke(mEt,false);
        }catch (NoSuchMethodException e){

        }catch (IllegalAccessException e){

        }catch (IllegalArgumentException e){

        }catch (InvocationTargetException e){

        }
    }

    //返回键隐藏键盘
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && id_keyboard != null && id_keyboard.isShowing()){
            id_keyboard.hideKeyboard();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}

android 实现自定义键盘的实例_第1张图片








你可能感兴趣的:(android)