自定义软键盘

2017-03-27  本文已影响0人  AnnaLeeYY

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

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

最终选择 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>

上一篇下一篇

猜你喜欢

热点阅读