欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义软键盘的步骤记录

程序员文章站 2022-07-27 20:17:59
目录3、处理点击事件onkey效果图还是咱们的老规矩,先放最终效果图 ????????????实现自定义软键盘需要实现一个软键盘很简单,只需要很简单的3步1、通过xml文...

效果图

还是咱们的老规矩,先放最终效果图 ????????????

Android自定义软键盘的步骤记录

实现自定义软键盘

需要实现一个软键盘很简单,只需要很简单的3步

1、通过xml文件,定义出来键盘结构

2、将定义好的键盘结构与keyboardview绑定起来

3、实现onkey方法,处理输入和操作事件

1、通过xml定义键盘

在res下面定义一个xml文件夹,并创建你的软键盘布局xml文件

这边需要根据自己的每一个key对应的比例计算出来大小,%p就是占整个的百分比,要注意间隔距离。

<?xml version="1.0" encoding="utf-8"?>
<keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:horizontalgap="1%p"
    android:keywidth="10%p"
    android:keyheight="50dp"
    android:verticalgap="1%p">

    <row>
        <key
            android:codes="81"<!--最终展示内容的unicode-->
            android:horizontalgap="1%p"<!--横向间隔比率-->
            android:keywidth="8.9%p"<!--键位宽度比率-->
            android:keyedgeflags="left"<!--键盘间隔对其方式-->
            android:keylabel="q" <!--键盘上展示的文案--> />
        <key
            android:codes="87"
            android:keywidth="8.9%p"
            android:keylabel="w" />
        <key
            android:codes="69"
            android:keywidth="8.9%p"
            android:keylabel="e" />
        <key
            android:codes="82"
            android:keywidth="8.9%p"
            android:keylabel="r" />
        <key
            android:codes="84"
            android:keywidth="8.9%p"
            android:keylabel="t" />
        <key
            android:codes="89"
            android:keywidth="8.9%p"
            android:keylabel="y" />
        <key
            android:codes="85"
            android:keywidth="8.9%p"
            android:keylabel="u" />
        <key
            android:codes="73"
            android:keywidth="8.9%p"
            android:keylabel="i" />
        <key
            android:codes="79"
            android:keywidth="8.9%p"
            android:keylabel="o" />
        <key
            android:codes="80"
            android:keywidth="8.9%p"
            android:keyedgeflags="right"
            android:keylabel="p" />
    </row>

    <row>
        <key
            android:codes="65"
            android:horizontalgap="5.5%p"
            android:keywidth="9%p"
            android:keyedgeflags="left"
            android:keylabel="a" />
        <key
            android:codes="83"
            android:keywidth="9%p"
            android:keylabel="s" />
        <key
            android:codes="68"
            android:keywidth="9%p"
            android:keylabel="d" />
        <key
            android:codes="70"
            android:keywidth="9%p"
            android:keylabel="f" />
        <key
            android:codes="71"
            android:keywidth="9%p"
            android:keylabel="g" />
        <key
            android:codes="72"
            android:keywidth="9%p"
            android:keylabel="h" />
        <key
            android:codes="74"
            android:keywidth="9%p"
            android:keylabel="j" />
        <key
            android:codes="75"
            android:keywidth="9%p"
            android:keylabel="k" />
        <key
            android:codes="76"
            android:keywidth="9%p"
            android:keyedgeflags="left"
            android:keylabel="l" />
    </row>

    <row>
        <key
            android:codes="-1005"
            android:keywidth="13.5%p"
            android:keyedgeflags="left"
            android:keylabel="中" />
        <key
            android:codes="90"
            android:keywidth="9%p"
            android:keylabel="z" />
        <key
            android:codes="88"
            android:keywidth="9%p"
            android:keylabel="x" />
        <key
            android:codes="67"
            android:keywidth="9%p"
            android:keylabel="c" />
        <key
            android:codes="86"
            android:keywidth="9%p"
            android:keylabel="v" />
        <key
            android:codes="66"
            android:keywidth="9%p"
            android:keylabel="b" />
        <key
            android:codes="78"
            android:keywidth="9%p"
            android:keylabel="n" />
        <key
            android:codes="77"
            android:keywidth="9%p"
            android:keylabel="m" />
        <key
            android:codes="-5"
            android:isrepeatable="true"
            android:keywidth="13.5%p" />
    </row>
    <row>
        <key
            android:codes="-1004"
            android:keywidth="24%p"
            android:keyedgeflags="left"
            android:keylabel="123" />
        <key
            android:codes="32"
            android:keywidth="48%p"
            android:keylabel="space" />
        <key
            android:codes="-1003"
            android:keywidth="24%p"
            android:keyedgeflags="right"
            android:keylabel="确定" />
    </row>
</keyboard>

2、将xml文件与keyboardview绑定起来

创建出来的keyboard文件是要与keyboard类结合起来使用的。

wordkeyboard = new keyboard(context, r.xml.stock_word_keyboard);

实现自己的keyboardview,继承自keyboardview。

public class mykeyboardview extends keyboardview {
...
init{
  wordkeyboard = new keyboard(context, r.xml.stock_word_keyboard);
  //将你的keyboard与keyboardview绑定起来
  this.setkeyboard(wordkeyboard);
}

我们真实需要添加到布局中的view实际上就是自定义的mykeyboardview ,它的使用和其他自定义view没有任何区别。

3、处理点击事件onkey

如果你完成了上面两步,并将view添加到布局中,你会发现已经可以展示出来了。但是点击并没有任何效果。
如果想要出效果,就需要实现onkey进行处理。

keyboardview.this.setonkeyboardactionlistener(new onkeyboardactionlistener() {

    @override
    public void onkey(int primarycode, int[] keycodes) {
        try {
            editable editable = edittext.gettext();
            int start = edittext.getselectionstart();
            int end = edittext.getselectionend();
            string code = string.valueof(primarycode);
            switch (code) {
            		//切换到数字键盘
                case keyboardkeymap.tool_switch_to_num:
                    onkeyboardcallback.switchtonumberkeyboard();
                    break;
                    //切换到系统键盘
                case keyboardkeymap.tool_switch_to_word:
                    onkeyboardcallback.switchtosystemkeyboard();
                    break;
                    //隐藏键盘
                case keyboardkeymap.tool_hide:
                    onkeyboardcallback.onhidestockkeyboard();
                    break;
                    //删除
                case keyboardkeymap.tool_del:
                    if (editable != null && editable.length() > 0) {
                        if (start == end) {
                            editable.delete(start - 1, start);
                        } else {
                            editable.delete(start, end);
                        }
                    }
                    break;
                    //清空输入
                case keyboardkeymap.tool_clear:
                    if (editable != null) {
                        editable.clear();
                    }
                    break;
                    //确认按钮
                case keyboardkeymap.tool_confirm:
                    onkeyboardcallback.onconfirmstockkeyboard();
                    break;
                default:
                   //正常输入
                    if (editable != null) {
                        if (keyboardkeymap.isstockprefix(code)) {
	                        //这里处理更加特殊的输入定义,
	                        //比如你需要输入城市简称等(车牌自定义键盘需要)
                            string resultcode = keyboardkeymap.findinputbykey(code);
                            editable.replace(start, end, resultcode);
                        } else {
                            //这里如果是正常的键位(排除确认、清空、切换等功能键位),
                            //则将键位上的unicode转换为正常的数字,比如定义键盘p对应的
                            //unicode是80,因为xml定义键位的时候为了方便匹配,所以值
                            //是使用的unicode,这边则会将80转换为真正要输入的p字母。
                            string resultcode = character.tostring((char) primarycode);
                            editable.replace(start, end, resultcode);
                        }
                    }
                    break;
            }
        } catch (exception e) {
            e.printstacktrace();
        }
    }
}

到这里,基本的自定义键盘定义操作就完成了。当然如果你是工作使用,并没有结束,因为一般情况下自定义键盘需要和系统键盘并存,因此你还需要处理键盘切换的闪动问题。对于键盘切换控制,我这里就不过多介绍了,可以自行查阅软键盘+表情切换,处理方案目前已经很成熟了。原理是一样的。

附赠一些实用的效果处理

1、点击空白处,关闭软键盘,如果有内容,出发内容点击,并关系软键盘,如果是滑动,则只关闭软键盘
效果实现太简单了,这里不做过多说明,理解事件分发自然懂。

class autohidekeyboardcstlayout @jvmoverloads constructor(
    context: context, attrs: attributeset? = null
) : constraintlayout(context, attrs) {

    var keyboardhidelistener: (() -> unit)? = null

    override fun onintercepttouchevent(ev: motionevent?): boolean {
        if (ev?.action == motionevent.action_down) {
            keyboardhidelistener?.invoke()
        }
        return super.onintercepttouchevent(ev)
    }
}

关闭操作只需要在回调方法执行即可。

contenthidekeyboardcstlayout.keyboardhidelistener = {
    hidepanelandkeyboard()
}

2、切换软键盘panel,很简单的实现

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="@android:color/white"
    android:elevation="0.5dp">

    <androidx.appcompat.widget.appcompattextview
        android:id="@+id/tvstocknumkeyboard"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centervertical="true"
        android:layout_marginstart="10dp"
        android:button="@null"
        android:padding="6dp"
        android:text="123"
        android:textcolor="@drawable/stock_switch_label_color"
        android:textsize="16dp"
        android:textstyle="bold" />

    <androidx.appcompat.widget.appcompattextview
        android:id="@+id/tvstockwordkeyboard"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centervertical="true"
        android:layout_marginstart="18dp"
        android:layout_toendof="@+id/tvstocknumkeyboard"
        android:button="@null"
        android:padding="6dp"
        android:text="abc"
        android:textcolor="@drawable/stock_switch_label_color"
        android:textsize="16dp"
        android:textstyle="bold" />

    <androidx.appcompat.widget.appcompattextview
        android:id="@+id/tvsystemkeyboard"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centervertical="true"
        android:layout_marginstart="18dp"
        android:layout_toendof="@+id/tvstockwordkeyboard"
        android:button="@null"
        android:padding="6dp"
        android:text="中文"
        android:textcolor="@drawable/stock_switch_label_color"
        android:textsize="16dp"
        android:textstyle="bold" />

    <framelayout
        android:id="@+id/keyboarddone"
        android:layout_width="60sp"
        android:layout_height="match_parent"
        android:layout_alignparentend="true"
        android:layout_centervertical="true">

        <imageview
            android:layout_width="16dp"
            android:layout_height="16dp"
            android:layout_gravity="center"
            android:contentdescription="@null"
            android:scaletype="centerinside"
            android:src="@drawable/keyboard_done_"
            android:textcolor="@color/white"
            android:textsize="16sp" />
    </framelayout>

    <view
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="#eeeeee" />
</relativelayout>

颜色切换selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#f14400" android:state_selected="true" />
    <item android:color="#334455" android:state_selected="false" />
</selector>
class keyboardswitcher @jvmoverloads constructor(
    context: context, attrs: attributeset? = null
) : relativelayout(context, attrs) {

    private var mviewbinding: rtckeyboardswitcherbinding? = null
    private var mstockkeyboardview: stockkeyboardview? = null

    init {
        mviewbinding = rtckeyboardswitcherbinding.inflate(layoutinflater.from(context), this, true)
    }

    fun pressnumberkeyboard() {
        mviewbinding?.tvstocknumkeyboard?.performclick()
    }

    fun presswordkeyboard() {
        mviewbinding?.tvstockwordkeyboard?.performclick()
    }

    fun presssystemkeyboard() {
        mviewbinding?.tvsystemkeyboard?.performclick()
    }

    fun switchkeyboard(
        _switchkeyboard: (issystemkeyboard: boolean) -> unit,
        _keyboarddone: () -> unit
    ) {
        mviewbinding?.apply {
            tvstocknumkeyboard.setonclicklistener {
                resetselectedstate()
                _switchkeyboard.invoke(false)
                mstockkeyboardview?.shownumberkeyboard()
                it.isselected = true
            }
            tvstockwordkeyboard.setonclicklistener {
                resetselectedstate()
                _switchkeyboard.invoke(false)
                mstockkeyboardview?.showwordkeyboard()
                it.isselected = true
            }
            tvsystemkeyboard.setonclicklistener {
                resetselectedstate()
                _switchkeyboard.invoke(true)
                it.isselected = true
            }
            keyboarddone.setonclicklistener {
                _keyboarddone.invoke()
            }
        }
    }

    fun setdefaultkeyboard(index: int) {
        resetselectedstate()
        mviewbinding?.apply {
            when (index) {
                0 -> {
                    tvstocknumkeyboard.isselected = true
                }
                1 -> {
                    tvstockwordkeyboard.isselected = true
                }
                2 -> {
                    tvsystemkeyboard.isselected = true
                }
            }
        }
    }

    private fun resetselectedstate() {
        mviewbinding?.apply {
            tvstocknumkeyboard.isselected = false
            tvstockwordkeyboard.isselected = false
            tvsystemkeyboard.isselected = false
        }
    }

    override fun ontouchevent(event: motionevent?): boolean {
        if (event?.action == motionevent.action_down) {
            performclick()
        }
        return true
    }

    override fun performclick(): boolean {
        return super.performclick()
    }

    fun attach(stockkeyboardview: stockkeyboardview) {
        this.mstockkeyboardview = stockkeyboardview
    }

    fun shownumberkeyboard() {
        this.mstockkeyboardview?.shownumberkeyboard()
    }
}

总结

到此这篇关于android自定义软键盘的文章就介绍到这了,更多相关android自定义软键盘内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!