漏洞修复之----自定义软键盘+随机键盘

最近一直在处理漏洞。。。 处理的多了 做一些总结

需要自定义软键盘 随机字母数字的功能

1.首先是 自定义键盘工具类 KeyboardUtil

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.Keyboard.Key;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;


/**
自定义键盘工具类
**/
public class KeyboardUtil {
    private Context ctx;
    private Activity act;
    private KeyboardView keyboardView;
    private Keyboard k1;// 字母键盘
    private Keyboard k2;// 数字键盘
    public boolean isnun = false;// 是否数据键盘
    public boolean isupper = false;// 是否大写
    public boolean isRandom = false;//是否显示随机键盘
    private EditText ed;

    public KeyboardUtil(Activity act, Context ctx, EditText edit, boolean isRandom) {
        this.act = act;
        this.ctx = ctx;
        this.ed = edit;
        this.isRandom = isRandom;
        k1 = new Keyboard(ctx, R.xml.qwerty);
        k2 = new Keyboard(ctx, R.xml.symbols);

        keyboardView = (KeyboardView) act.findViewById(R.id.keyboard_view);
        keyboardView.setKeyboard(k1);
        keyboardView.setEnabled(true);
        keyboardView.setPreviewEnabled(true);
        keyboardView.setOnKeyboardActionListener(listener);
        if (isRandom) {
            randomalpkey(k1);
            randomKeyboard();
        }
    }

    /**
     * 随机排列数字键
     */
    private void randomKeyboard() {
        //获取键盘所有按键
        List keyLists = k2.getKeys();
        //存储0~9数字
        List numberkeyLists = new ArrayList<>();

        for (int i = 0; i < keyLists.size(); i++) {
            if (null != keyLists.get(i).label && isNumber(keyLists.get(i).label.toString())) {
                numberkeyLists.add(keyLists.get(i));
            }
        }
        //临时存储
        LinkedList tempLists = new LinkedList<>();
        for (int i = 0; i < numberkeyLists.size(); i++) {
            tempLists.add(new KeyModel(48 + i, String.valueOf(i)));
        }

        //结果集
        List resultLists = new ArrayList<>();

        //取数
        Random random = new Random();
        for (int i = 0; i < numberkeyLists.size(); i++) {
            int num = random.nextInt(numberkeyLists.size() - i);
            resultLists.add(new KeyModel(tempLists.get(num).getCode(), tempLists.get(num).getLable()));
            tempLists.remove(num);
        }

        for (int i = 0; i < numberkeyLists.size(); i++) {
            numberkeyLists.get(i).label = resultLists.get(i).getLable();
            numberkeyLists.get(i).codes[0] = resultLists.get(i).getCode();
        }
    }


    // 随机字母
    private void randomalpkey(Keyboard mKeyboard) {
        List keyList = mKeyboard.getKeys();
        // 查找出a-z的数字键
        List newkeyList = new ArrayList();
        for (int i = 0; i < keyList.size(); i++) {
            if (keyList.get(i).label != null
                    && isword(keyList.get(i).label.toString())) {
                newkeyList.add(keyList.get(i));
            }
        }
        // 数组长度
        int count = newkeyList.size();
        // 结果集
        List resultList = new ArrayList();
        // 用一个LinkedList作为中介
        LinkedList temp = new LinkedList();
        // 初始化temp
        for (int i = 0; i < count - 1; i++) {
            temp.add(new KeyModel(97 + i, "" + (char) (97 + i)));
        }
        temp.add(new KeyModel(64, "" + (char) 64));// .
        // 取数
        Random rand = new Random();
        for (int i = 0; i < count; i++) {
            int num = rand.nextInt(count - i);
            resultList.add(new KeyModel(temp.get(num).getCode(), temp.get(num)
                    .getLable()));
            temp.remove(num);
        }
        for (int i = 0; i < newkeyList.size(); i++) {
            newkeyList.get(i).label = resultList.get(i).getLable();
            newkeyList.get(i).codes[0] = resultList.get(i).getCode();
        }


        keyboardView.setKeyboard(mKeyboard);
    }

    /**
     * 判断是否是数字
     *
     * @param number
     * @return boolean
     */
    private boolean isNumber(String number) {
        String numbers = "0123456789";
        return numbers.contains(number);
    }


    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_CANCEL) {// 完成
                hideKeyboard();
            } else if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退
                if (editable != null && editable.length() > 0) {
                    if (start > 0) {
                        editable.delete(start - 1, start);
                    }
                }
            } else if (primaryCode == Keyboard.KEYCODE_SHIFT) {// 大小写切换
                changeKey();
                keyboardView.setKeyboard(k1);

            } else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE) {// 数字键盘切换
                if (isnun) {
                    isnun = false;
                    keyboardView.setKeyboard(k1);
                } else {
                    isnun = true;
                    keyboardView.setKeyboard(k2);
                }
            } else if (primaryCode == 57419) { // go left
                if (start > 0) {
                    ed.setSelection(start - 1);
                }
            } else if (primaryCode == 57421) { // go right
                if (start < ed.length()) {
                    ed.setSelection(start + 1);
                }
            } else {
                editable.insert(start, Character.toString((char) primaryCode));
            }
        }
    };

    /**
     * 键盘大小写切换
     */
    private void changeKey() {
        List keylist = k1.getKeys();
        if (isupper) {//大写切换小写
            isupper = false;
            for (Key key : keylist) {
                if (key.label != null && isword(key.label.toString())) {
                    key.label = key.label.toString().toLowerCase();
                    key.codes[0] = key.codes[0] + 32;
                }
            }
        } else {//小写切换大写
            isupper = true;
            for (Key key : keylist) {
                if (key.label != null && isword(key.label.toString())) {
                    key.label = key.label.toString().toUpperCase();
                    key.codes[0] = key.codes[0] - 32;
                }
            }
        }
    }

    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;
    }

}

KeyModel类

/**
 * Created by kevin on 17/9/20.
 */

public class KeyModel {
    private Integer code;
    private String lable;

    public KeyModel(Integer code, String lable) {
        this.code = code;
        this.lable = lable;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getLable() {
        return lable;
    }

    public void setLable(String lable) {
        this.lable = lable;
    }

    @Override
    public String toString() {
        return "KeyModel{" +
                "code=" + code +
                ", lable='" + lable + '\'' +
                '}';
    }
}

3.布局文件


    

4.别忘了xml里面的xml文件

字母键盘qwerty.xml



   
      
      
      
      
      
      
      
      
      
      
   
   
      
      
      
      
      
      
      
      
      
   
   
      
      
      
      
      
      
      
      
      
   
   
      
      
      
      
      
   

5.数字键盘symbols.xml



   
      
      
      
      
   
   
      
      
      
      
   
   
      
      
      
      
   
   
      
      
      
   

 

以上是看了博友的博客 以及自己总结的一下 如有遗失部分 评论修复

 

 

你可能感兴趣的:(漏洞处理篇)