自定义软键盘

自定义一个软键盘,解决软键盘遮蔽问题

有不同的考虑路径。。。。

  • 第三方的自定义控件

最终选择 StomHong 提供的方案,满足了项目的需求。

  • 我写的软键盘

正常来说,找了一个第三方的,就可以了,但是第三方的改起来没有自己定义来的方便,因此我来试试吧。

先看看设计图

数字键盘

数字键盘.png

字母键盘


字母键盘.png

字符键盘


字符键盘.png

想要改变颜色的朋友可以自定义改变哦!!!!

源码展示

<pre>
public class KeyBoardLayout extends BaseKeyBoardLayout<KeyBoardLayout> {
public KeyBoardLayout(Context context) {
super(context);
setChildViewLayoutRes(R.layout.keyboard_integer, R.layout.keyboard_letters, R.layout.keyboard_symbol);
setTextAllCaps(false);
}

@Override
public void show(@KeyBoardType int keyBoardType) {
    if (isShowing()) {
        hiddenAllChildView();
    }
    setVisibility(View.VISIBLE);
    this.keyBoardType = keyBoardType;
    randomKeySet(keyBoardType);
    isShowing = true;
    if (getParent() == null) {
        windowManager.addView(this, wmLayoutParams);
    }
    View childView = getChildViewByType(keyBoardType);
    childView.setVisibility(View.VISIBLE);
    int targetLocationY = getLocationY(targetEditText) + targetEditText.getHeight();
    height = getKeyBoardLayoutHeight();
    if (getHasScrollView()) {
        targetOffset = height;
        FrameLayout.LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        params.bottomMargin = targetOffset;
        targetContentView.setLayoutParams(params);
    } else {

        int tmpOffset = targetLocationY - (screenHeight - height);
        targetOffset = tmpOffset > 0 ? tmpOffset : targetOffset;
        //LogTrack.e("输入框最高点  = " + targetLocationY + " 软键盘最高点 = " + (screenHeight - height) + " tmpOffset = " + tmpOffset + " offset = " + targetOffset);
        if (tmpOffset > 0) {
            ViewCompat.offsetTopAndBottom(targetContentView, -targetOffset);
        }
    }
    //LogTrack.e("展示 " + childView.getTag());
}

private void randomKeySet(@KeyBoardType int keyBoardType) {
    View childView = getChildViewByType(keyBoardType);
    List<String> dataSetByType = getChildDataSetByType(keyBoardType);
    int[] keyViewArray = getChildKeyViewArrayByType(keyBoardType);
    for (int i = 0; i < keyViewArray.length; i++) {
        setText(childView.findViewById(keyViewArray[i]), dataSetByType.get(i));
    }
}

private void hiddenAllChildView() {
    for (int i = 0; i < getChildCount(); i++) {
        getChildAt(i).setVisibility(View.GONE);
    }
}

@Override
public void hidden() {
    LogTrack.e("进来了 ");
    if (!isShowing()) {
        return;
    }
    isShowing = false;
    View childView = getChildViewByType(keyBoardType);
    setVisibility(View.GONE);
    if (getParent() == null) {
        windowManager.removeView(KeyBoardLayout.this);
    }
    childView.setVisibility(View.GONE);
    if (getHasScrollView()) {
        FrameLayout.LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        params.bottomMargin = 0;
        targetContentView.setLayoutParams(params);
    } else {
        //LogTrack.e(targetOffset);
        if (targetOffset > 0) {
            //ViewCompat.setTranslationY(targetContentView, 0);
            ViewCompat.offsetTopAndBottom(targetContentView, targetOffset);
        }
    }
    targetOffset = 0;
}

private View getChildViewByType(int keyBoardType) {
    if (KeyBoardType.integer == keyBoardType) {
        return getChildAt(0);
    }
    if (KeyBoardType.letters == keyBoardType) {
        return getChildAt(1);
    }
    if (KeyBoardType.symbol == keyBoardType) {
        return getChildAt(2);
    }
    return getChildAt(0);
}

private int[] getChildKeyViewArrayByType(int keyBoardType) {
    if (KeyBoardType.integer == keyBoardType) {
        return DataSet.getIntegerKeyId();
    }
    if (KeyBoardType.letters == keyBoardType) {
        return DataSet.getLetterKeyId();
    }
    if (KeyBoardType.symbol == keyBoardType) {
        return DataSet.getSymbolKeyId();
    }
    return DataSet.getIntegerKeyId();
}

protected void setChildViewLayoutRes(int... resource) {
    String tag[] = new String[]{"数字键盘", "字母键盘", "小数键盘", "符号键盘"};
    for (int i = 0; i < resource.length; i++) {
        View view = LayoutInflater.from(getContext()).inflate(resource[i], null);
        view.setVisibility(View.GONE);
        view.setTag(tag[i]);
        view.findViewById(R.id.fun_delete).setOnLongClickListener(this);
        KeyBoardHelper.getInstance().onBindClickListener(view, this);
        addView(view);
    }
}


private void switchChildViewByType(@KeyBoardType int keyBoardType) {
    int k = 0;
    if (KeyBoardType.integer == keyBoardType) {
        k = 0;
    } else if (KeyBoardType.letters == keyBoardType) {
        k = 1;
    } else if (KeyBoardType.symbol == keyBoardType) {
        k = 2;
    }
    for (int i = 0; i < getChildCount(); i++) {
        getChildAt(i).setVisibility(k == i ? View.VISIBLE : View.GONE);
    }
    randomKeySet(keyBoardType);
}

@Override
public KeyBoardLayout setTextAllCaps(boolean isTextAllCaps) {
    super.setTextAllCaps(isTextAllCaps);
    int[] array = getLetterViewIdArray();
    View childView = getChildViewByType(KeyBoardType.letters);
    setTextAllCaps(childView.findViewById(R.id.fun_shift), isTextAllCaps);
    for (int i = 0; i < array.length; i++) {
        setTextAllCaps(childView.findViewById(array[i]), isTextAllCaps);
    }
    return this;
}

/*/*/
 */ 设置 键盘 的 label
 */
 */ @param text
 *//
public KeyBoardLayout setLabel(Object text) {
    for (int i = 0; i < getChildCount(); i++) {
        setText(getChildAt(i).findViewById(R.id.tv_label), text);
    }
    return this;
}

@Override
public void onClick(View view) {
    if (!(view instanceof TextView) || view.getId() < 0) {
        return;
    }
    if (R.id.tv_close == view.getId()) {
        hidden();
    } else if (R.id.fun_symbol == view.getId()) {
        switchChildViewByType(KeyBoardType.symbol);
    } else if (R.id.fun_letter == view.getId()) {
        switchChildViewByType(KeyBoardType.letters);
    } else if (R.id.fun_integer == view.getId()) {
        switchChildViewByType(KeyBoardType.integer);
    } else if (R.id.fun_delete == view.getId()) {
        delete(targetEditText);
    } else if (R.id.fun_shift == view.getId()) {
        setTextAllCaps(!isTextAllCaps());
    } else {
        TextView textView = (TextView) view;
        if (R.id.im_30 == view.getId()) {
            targetEditText.append(" ");
        } else {
            targetEditText.append(textView.getText());
        }
    }
}

@Override
public boolean onLongClick(View view) {
    if (R.id.fun_delete == view.getId()) {
        targetEditText.setText("");
    }
    return true;
}

private int[] getLetterViewIdArray() {
    int array[] = new int[]{R.id.imc_00, R.id.imc_01, R.id.imc_02, R.id.imc_03, R.id.imc_04, R.id.imc_05, R.id.imc_06, R.id.imc_07, R.id.imc_08, R.id.imc_09, R.id.imc_10, R.id.imc_11, R.id.imc_12, R.id.imc_13, R.id.imc_14, R.id.imc_15, R.id.imc_16, R.id.imc_17, R.id.imc_20, R.id.imc_21, R.id.imc_22, R.id.imc_23, R.id.imc_24, R.id.imc_25, R.id.imc_26, R.id.im_30};
    return array;
}

}

</pre>

  • 然后 修改 清单文件

<pre>
<activity
android:name=".mine.bank.PromoterBindBankActivity"
android:configChanges="orientation|keyboardHidden"
android:description="@string/activity_select_bank"
android:screenOrientation="portrait"/>
</pre>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 先上图: 刚开始写自定义软键盘是走了点弯路,用控件去实现,好多小细节处理起来真的太抓狂了… 后来发现 神控件 Ke...
    silence_jjj阅读 9,890评论 0 0
  • 简介 今天在掘金上看了一篇文章,实现自定义软键盘,发现其实实现方式比较简单,不需要改动系统api,只是单纯的加载自...
    fushuang阅读 9,988评论 1 14
  • 概述 在项目开发中遇到一个需求,”只要数字键盘的输入,仅仅有大写字母的输入,某些输入法总是会提示更新,弹出广告等“...
    张云飞Vir阅读 8,708评论 3 49
  • 去年因公司需要给电视盒子做一个自定义键盘,今天偶然想起来, 想趁此机会记录一下。当时好像是下载了一个别人的例子,参...
    Wing_L阅读 5,176评论 2 50
  • 老婆,今晚不要锁门,我去你房间. 你叫我什么? 当然是叫你老婆,你是我最最亲爱的老婆! 我怎么以为我只是你家的门卫...
    张译刈阅读 1,079评论 0 1