public class GooodsPadView extends LinearLayout {

public Context mContext;
public StringBuilder sbInputContent = new StringBuilder();
/**
 * 根控件区
 */
private View layoutLettersPad;
private View layoutDigitPad;

//第1排
private RelativeLayout rl_letterspad_lock;
private ImageView iv_letterspad_lock;
private Button bt_letterspad_q;
private Button bt_letterspad_w;
private Button bt_letterspad_e;
private Button bt_letterspad_r;
private Button bt_letterspad_t;
private Button bt_letterspad_y;
private Button bt_letterspad_u;
private Button bt_letterspad_i;
private Button bt_letterspad_o;
private Button bt_letterspad_p;
private View rl_letterspad_delete;

//第2排
private Button bt_letterspad_empty;
private Button bt_letterspad_a;
private Button bt_letterspad_s;
private Button bt_letterspad_d;
private Button bt_letterspad_f;
private Button bt_letterspad_g;
private Button bt_letterspad_h;
private Button bt_letterspad_j;
private Button bt_letterspad_k;
private Button bt_letterspad_l;
private Button bt_letterspad_clear;

//第3排
private Button bt_letterspad_123;
private Button bt_letterspad_z;
private Button bt_letterspad_x;
private Button bt_letterspad_c;
private Button bt_letterspad_v;
private Button bt_letterspad_b;
private Button bt_letterspad_n;
private Button bt_letterspad_m;
private Button bt_letterspad_confirm;

/**
 * 数字键盘控件区
 */
//第1排
private RelativeLayout rl_digitpad_lock;
private ImageView iv_digitpad_lock;
private Button bt_digitpad_1;
private Button bt_digitpad_2;
private Button bt_digitpad_3;
private RelativeLayout rl_digitpad_delete;

//第2排
private Button bt_digitpad_abc;
private Button bt_digitpad_4;
private Button bt_digitpad_5;
private Button bt_digitpad_6;
private Button bt_digitpad_0;
private Button bt_digitpad_clear;

//第3排
private Button bt_digitpad_7;
private Button bt_digitpad_8;
private Button bt_digitpad_9;
private Button bt_digitpad_confirm;

/**
 * 其他参数
 */
private Boolean isGoodsPadLock = false;//默认情况下,不锁住键盘

public GooodsPadView(Context context) {
    super(context);
}

public GooodsPadView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    this.mContext = context;
    View rootView = LayoutInflater.from(context).inflate(R.layout.layout_goodspad, this, false);
    initRes(rootView);
    setListener();

    /**
     * 加载控件
     */
    addView(rootView);
}

public GooodsPadView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

}

public void initRes(View rootView) {
    /**
     * 键盘根控件区
     */
    layoutLettersPad = rootView.findViewById(R.id.layoutLettersPad);
    layoutDigitPad = rootView.findViewById(R.id.layoutDigitPad);

    /**
     * 字母键盘控件区
     */
    //第1排
    rl_letterspad_lock = rootView.findViewById(R.id.rl_letterspad_lock);
    iv_letterspad_lock = rootView.findViewById(R.id.iv_letterspad_lock);
    bt_letterspad_q = rootView.findViewById(R.id.bt_letterspad_q);
    bt_letterspad_w = rootView.findViewById(R.id.bt_letterspad_w);
    bt_letterspad_e = rootView.findViewById(R.id.bt_letterspad_e);
    bt_letterspad_r = rootView.findViewById(R.id.bt_letterspad_r);
    bt_letterspad_t = rootView.findViewById(R.id.bt_letterspad_t);
    bt_letterspad_y = rootView.findViewById(R.id.bt_letterspad_y);
    bt_letterspad_u = rootView.findViewById(R.id.bt_letterspad_u);
    bt_letterspad_i = rootView.findViewById(R.id.bt_letterspad_i);
    bt_letterspad_o = rootView.findViewById(R.id.bt_letterspad_o);
    bt_letterspad_p = rootView.findViewById(R.id.bt_letterspad_p);
    rl_letterspad_delete = rootView.findViewById(R.id.rl_letterspad_delete);

    //第2排
    bt_letterspad_empty = rootView.findViewById(R.id.bt_letterspad_empty);
    bt_letterspad_a = rootView.findViewById(R.id.bt_letterspad_a);
    bt_letterspad_s = rootView.findViewById(R.id.bt_letterspad_s);
    bt_letterspad_d = rootView.findViewById(R.id.bt_letterspad_d);
    bt_letterspad_f = rootView.findViewById(R.id.bt_letterspad_f);
    bt_letterspad_g = rootView.findViewById(R.id.bt_letterspad_g);
    bt_letterspad_h = rootView.findViewById(R.id.bt_letterspad_h);
    bt_letterspad_j = rootView.findViewById(R.id.bt_letterspad_j);
    bt_letterspad_k = rootView.findViewById(R.id.bt_letterspad_k);
    bt_letterspad_l = rootView.findViewById(R.id.bt_letterspad_l);
    bt_letterspad_clear = rootView.findViewById(R.id.bt_letterspad_clear);

    //第3排
    bt_letterspad_123 = rootView.findViewById(R.id.bt_letterspad_123);
    bt_letterspad_z = rootView.findViewById(R.id.bt_letterspad_z);
    bt_letterspad_x = rootView.findViewById(R.id.bt_letterspad_x);
    bt_letterspad_c = rootView.findViewById(R.id.bt_letterspad_c);
    bt_letterspad_v = rootView.findViewById(R.id.bt_letterspad_v);
    bt_letterspad_b = rootView.findViewById(R.id.bt_letterspad_b);
    bt_letterspad_n = rootView.findViewById(R.id.bt_letterspad_n);
    bt_letterspad_m = rootView.findViewById(R.id.bt_letterspad_m);
    bt_letterspad_confirm = rootView.findViewById(R.id.bt_letterspad_confirm);

    /**
     * 数字键盘控件区
     */
    //第1排
    rl_digitpad_lock = rootView.findViewById(R.id.rl_digitpad_lock);
    iv_digitpad_lock = rootView.findViewById(R.id.iv_digitpad_lock);
    bt_digitpad_1 = rootView.findViewById(R.id.bt_digitpad_1);
    bt_digitpad_2 = rootView.findViewById(R.id.bt_digitpad_2);
    bt_digitpad_3 = rootView.findViewById(R.id.bt_digitpad_3);
    rl_digitpad_delete = rootView.findViewById(R.id.rl_digitpad_delete);

    //第2排
    bt_digitpad_abc = rootView.findViewById(R.id.bt_digitpad_abc);
    bt_digitpad_4 = rootView.findViewById(R.id.bt_digitpad_4);
    bt_digitpad_5 = rootView.findViewById(R.id.bt_digitpad_5);
    bt_digitpad_6 = rootView.findViewById(R.id.bt_digitpad_6);
    bt_digitpad_confirm = rootView.findViewById(R.id.bt_digitpad_confirm);

    //第3排
    bt_digitpad_clear = rootView.findViewById(R.id.bt_digitpad_clear);
    bt_digitpad_7 = rootView.findViewById(R.id.bt_digitpad_7);
    bt_digitpad_8 = rootView.findViewById(R.id.bt_digitpad_8);
    bt_digitpad_9 = rootView.findViewById(R.id.bt_digitpad_9);
    bt_digitpad_0 = rootView.findViewById(R.id.bt_digitpad_0);
    bt_digitpad_confirm = rootView.findViewById(R.id.bt_digitpad_confirm);
    ;
    /**
     * 默认情况下显示的键盘
     */
    onKeyboarWSwitchHandler(true);

    isGoodsPadLock = false;
    iv_letterspad_lock.setImageResource(R.mipmap.img_pad_lockclose);
    iv_digitpad_lock.setImageResource(R.mipmap.img_pad_lockclose);
}

private void setListener() {
    /**
     * 字母事件监听
     */
    bt_letterspad_a.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_letterspad_a);
        }
    });

    bt_letterspad_b.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_letterspad_b);
        }
    });
    bt_letterspad_c.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_letterspad_c);
        }
    });
    bt_letterspad_d.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_letterspad_d);
        }
    });
    bt_letterspad_e.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_letterspad_e);
        }
    });
    bt_letterspad_f.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_letterspad_f);
        }
    });
    bt_letterspad_g.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_letterspad_g);
        }
    });
    bt_letterspad_h.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_letterspad_h);
        }
    });
    bt_letterspad_i.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_letterspad_i);
        }
    });
    bt_letterspad_j.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_letterspad_j);
        }
    });
    bt_letterspad_k.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_letterspad_k);
        }
    });
    bt_letterspad_l.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_letterspad_l);
        }
    });
    bt_letterspad_m.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_letterspad_m);
        }
    });
    bt_letterspad_n.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_letterspad_n);
        }
    });
    bt_letterspad_o.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_letterspad_o);
        }
    });
    bt_letterspad_p.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_letterspad_p);
        }
    });
    bt_letterspad_q.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_letterspad_q);
        }
    });
    bt_letterspad_r.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_letterspad_r);
        }
    });
    bt_letterspad_s.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_letterspad_s);
        }
    });
    bt_letterspad_t.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_letterspad_t);
        }
    });
    bt_letterspad_u.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_letterspad_u);
        }
    });
    bt_letterspad_v.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_letterspad_v);
        }
    });
    bt_letterspad_w.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_letterspad_w);
        }
    });
    bt_letterspad_x.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_letterspad_x);
        }
    });
    bt_letterspad_y.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_letterspad_y);
        }
    });
    bt_letterspad_z.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_letterspad_z);
        }
    });

    /**
     * 数字监听
     */
    bt_digitpad_0.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_digitpad_0);
        }
    });
    bt_digitpad_1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_digitpad_1);
        }
    });
    bt_digitpad_2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_digitpad_2);
        }
    });
    bt_digitpad_3.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_digitpad_3);
        }
    });
    bt_digitpad_4.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_digitpad_4);
        }
    });
    bt_digitpad_5.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_digitpad_5);
        }
    });
    bt_digitpad_6.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_digitpad_6);
        }
    });
    bt_digitpad_7.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_digitpad_7);
        }
    });
    bt_digitpad_8.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_digitpad_8);
        }
    });
    bt_digitpad_9.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onLettersHandler(bt_digitpad_9);
        }
    });

    /**
     * 锁键盘事件
     */
    rl_letterspad_lock.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onKeyboardLockHandler();
        }
    });


    rl_digitpad_lock.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onKeyboardLockHandler();
        }
    });
    /**
     * 内容删除事件
     */
    rl_letterspad_delete.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onContentDeleteHandler();
        }
    });


    rl_digitpad_delete.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onContentDeleteHandler();
        }
    });

    /**
     * 内容清空事件
     */
    bt_letterspad_clear.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onContentClearHandler();
        }
    });


    bt_digitpad_clear.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onContentClearHandler();
        }
    });

    /**
     * 键盘切换事件
     */

    bt_letterspad_123.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onKeyboarWSwitchHandler(false);
        }
    });


    bt_digitpad_abc.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onKeyboarWSwitchHandler(true);
        }
    });


    /**
     * 确认事件
     */
    bt_letterspad_confirm.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onContentConfirmHandler();
        }
    });


    bt_digitpad_confirm.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onContentConfirmHandler();
        }
    });
}

/**
 * A-Z 字母处理
 */
private void onLettersHandler(Button mLettersButton) {
    String currentLetters = mLettersButton.getText().toString();
    sbInputContent.append(currentLetters.toUpperCase());
    onContentHandler();
}

/**
 * 锁键盘事件
 */
private void onKeyboardLockHandler() {
    if (!isGoodsPadLock) {
        isGoodsPadLock = true;
        iv_letterspad_lock.setImageResource(R.mipmap.img_pad_lockopen);
        iv_digitpad_lock.setImageResource(R.mipmap.img_pad_lockopen);
    } else {
        isGoodsPadLock = false;
        iv_letterspad_lock.setImageResource(R.mipmap.img_pad_lockclose);
        iv_digitpad_lock.setImageResource(R.mipmap.img_pad_lockclose);
    }
}

/**
 * 内容清空事件
 */
private void onContentClearHandler() {
    sbInputContent = new StringBuilder();
    onContentHandler();
}

/**
 * 内容删除事件
 */
private void onContentDeleteHandler() {
    int length = sbInputContent.length();
    if (length >= 1) {
        sbInputContent.delete(length - 1, length);
    }
    onContentHandler();
}

/**
 * 键盘切换事件
 */
private void onKeyboarWSwitchHandler(Boolean isLettersPad) {
    if (isLettersPad) {
        //切换到字母键盘
        layoutLettersPad.setVisibility(VISIBLE);
        layoutDigitPad.setVisibility(GONE);
    } else {
        //切换到数字键盘
        layoutLettersPad.setVisibility(GONE);
        layoutDigitPad.setVisibility(VISIBLE);
    }
}

/**
 * 内容处理
 */
private void onContentHandler() {
    //输入实时回调
    if (onInputContentListener != null) {
        onInputContentListener.invoke(sbInputContent.toString());
    }
    //设置对应EditText的值
    if (mEditText != null) {
        /**
         * 隐藏系统输入法
         */
        SysInputHelper.showOrHide(mContext, mEditText);
        mEditText.setCursorVisible(true);
        mEditText.requestFocus();

        /**
         * 显示输入的内容以及光标处理
         */
        String mInputContent = sbInputContent.toString();
        mEditText.setText(mInputContent);
        if (mInputContent.length() > 0) {
            mEditText.setSelection(mInputContent.length());
        }
    }
}


/**
 * 确认事件
 */
private void onContentConfirmHandler() {
    if (onContentConfirmListener != null){
    onContentConfirmListener.invoke(sbInputContent.toString());}
}


/**
 * 以下为对外开放接口或方法
 */


/**
 * 键盘状态
 */
public boolean isGoodsPadLock() {
    return isGoodsPadLock;
}

public OnContentConfirmListener onContentConfirmListener;
public OnInputContentListener onInputContentListener;

public void setOnContentConfirmListener(OnContentConfirmListener onContentConfirmListener) {
    this.onContentConfirmListener = onContentConfirmListener;
}

public final void setOnInputContentListener(OnInputContentListener onInputContentListener) {
    this.onInputContentListener = onInputContentListener;
}

private interface OnContentConfirmListener {
    void invoke(String str);
}

private interface OnInputContentListener {
    void invoke(String str);
}


/**
 * 设置对应的EditText
 */
private EditText mEditText;

public void setEditText(EditText mEditText) {
    if (mEditText != null) {
        sbInputContent.delete(0, sbInputContent.length());
        sbInputContent.append(mEditText.getText().toString());
    }
    //重置成字母键盘  后应韦要求,不作恢复处理
    this.mEditText = mEditText;
}}

    etSearch.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            /**
             * 隐藏系统输入法
             */
            etSearch.setCursorVisible(true);
            etSearch.requestFocus();
            etSearch.setInputType(InputType.TYPE_NULL);
            /**
             * 设置EditText
             */
            kv_goodspad.setVisibility(View.VISIBLE);
            kv_goodspad.setEditText(etSearch);
            return false;
        }
    });
Copy the code