Xamarin | Android |自定义KeyboardView |不同背景的不同按键

时间:2018-08-15 12:26:07

标签: android xamarin android-softkeyboard custom-keyboard android-custom-keyboard

使用Xamarin,我正在为Android创建自定义键盘视图。我已将KeyboardView中的KeyBackground设置为可绘制/选择器,以使背景为浅灰色,但是在按下时切换为深灰色。不幸的是,这只能在视图上设置,不能在单个键/行上设置。我希望将一行键(顶部的数字行)设置为可绘制/选择器,当未按下时其背景为深灰色,而在按下时仍为黑色。除了使用OnDraw方法获取键并在它们之间循环外,我似乎找不到其他方法来更改背景,然后我只能找出如何使用Draw命令更改背景,我不确定如何设置可绘制资源的keyBackground。有没有一种简单的方法可以做到这一点,我很想念。

此外,还可以将填充/边距应用于单个键,我也无法使其正常工作,但希望将句号和逗号从按钮底部移到更高的位置。

keyboard_layout.xml

<?xml version="1.0" encoding="utf-8" ?>
<CB.App.Keyboards.CustomKeyboardView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keyboard_view"
    android:layout_marginTop="20dp"
    android:layout_marginBottom="20dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:keyBackground="@drawable/key_selector"
    android:keyTextColor="@color/black"
    android:background="@color/lightgrey"
    android:shadowColor="@android:color/transparent"
    android:keyTextSize="25sp"
/>

CustomKeyboardView.cs

using System;
using System.Collections.Generic;
using System.Linq;
using Android.App;
using Android.Content;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.InputMethodServices;
using CB.App.Listeners;

namespace CB.App.Keyboards
{
    public class CustomKeyboardView : KeyboardView
    {
        public Keyboard _normalKeyBoard
        {
            get;
            private set;
        }

        public Keyboard _symbolKeyBoard
        {
            get;
            private set;
        }

        public CustomKeyboardView(Context context, Android.Util.IAttributeSet attributes) : base(context, attributes)
        {
            _normalKeyBoard = new Keyboard(this.Context, Resource.Layout.text_keyboard_layout, Resource.Integer.keyboard_normal);
            _symbolKeyBoard = new Keyboard(this.Context, Resource.Layout.text_keyboard_layout, Resource.Integer.keyboard_symbol);

            this.Keyboard = _normalKeyBoard;

            this.OnKeyboardActionListener = new MyKeyboardListener(this, (Activity)context);
            this.PreviewEnabled = false;
        }

        public override void OnDraw(Canvas canvas)
        {
            base.OnDraw(canvas);

            Paint paint = new Paint();
            paint.TextAlign = Paint.Align.Center;

            List<Keyboard.Key> keys = Keyboard.Keys.ToList();
            foreach(Keyboard.Key key in keys)
            {
                if(key.Codes.Contains((int)Android.Views.Keycode.Space))
                {
                    Drawable dr = (Drawable)Context.GetDrawable(Resource.Drawable.key_selector_reverse);
                    dr.SetBounds(key.X, key.Y + (int)(Math.Round((double)(key.Height / 4), 0)), key.X + key.Width, key.Y + (key.Height / 3) * 2);
                    dr.Draw(canvas);
                }

                //if(key.Codes.Intersect(numberKeyCodes).Any())
                //{
                //  Drawable dr = (Drawable)Context.GetDrawable(Resource.Drawable.key_selector_reverse);
                //  dr.SetBounds(key.X, key.Y + (int)(Math.Round((double)(key.Height / 4), 0)), key.X + key.Width, key.Y + (key.Height / 3) * 2);
                //  dr.Draw(canvas);
                //}
            }
        }
    }
}

text_keyboard_layout.xml

<?xml version="1.0" encoding="utf-8" ?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyWidth="10%p"
    android:horizontalGap="0dp"
    android:verticalGap="0dp"
    android:keyHeight="50dp"
    >
    <Row android:keyWidth="10%p">
        <Key android:codes="8" android:keyLabel="1" android:keyEdgeFlags="left" android:textSize="40sp" />
        <Key android:codes="9" android:keyLabel="2" />
        <Key android:codes="10" android:keyLabel="3" />
        <Key android:codes="11" android:keyLabel="4" />
        <Key android:codes="12" android:keyLabel="5" />
        <Key android:codes="13" android:keyLabel="6" />
        <Key android:codes="14" android:keyLabel="7" />
        <Key android:codes="15" android:keyLabel="8" />
        <Key android:codes="16" android:keyLabel="9" />
        <Key android:codes="7" android:keyLabel="0" android:keyEdgeFlags="right" android:keyTextSize="40sp"/>
    </Row>
    <Row android:keyWidth="10%p" android:keyboardMode="@integer/keyboard_normal">
        <Key android:codes="45" android:keyLabel="q" android:keyEdgeFlags="left"/>
        <Key android:codes="51" android:keyLabel="w" />
        <Key android:codes="33" android:keyLabel="e" />
        <Key android:codes="46" android:keyLabel="r" />
        <Key android:codes="48" android:keyLabel="t" />
        <Key android:codes="53" android:keyLabel="y" />
        <Key android:codes="49" android:keyLabel="u" />
        <Key android:codes="37" android:keyLabel="i" />
        <Key android:codes="43" android:keyLabel="o" />
        <Key android:codes="44" android:keyLabel="p" />
        <Key android:codes="67" android:keyIcon="@mipmap/keyboard_backspace" android:keyEdgeFlags="right" android:isRepeatable="true" />
    </Row>
    <Row android:keyWidth="9%p" android:keyboardMode="@integer/keyboard_symbol">
        <Key android:codes="68" android:keyLabel="`" android:keyEdgeFlags="left"/>
        <Key android:codes="-1" android:keyLabel="~" />
        <Key android:codes="71" android:keyLabel="[" />
        <Key android:codes="72" android:keyLabel="]" />
        <Key android:codes="-1" android:keyLabel="{" />
        <Key android:codes="-1" android:keyLabel="}" />
        <Key android:codes="-1" android:keyLabel="|" />
        <Key android:codes="73" android:keyLabel="\\" />
        <Key android:codes="-1" android:keyLabel="&lt;" />
        <Key android:codes="-1" android:keyLabel=">" />
        <Key android:codes="67" android:keyIcon="@mipmap/keyboard_backspace" android:keyEdgeFlags="right" android:isRepeatable="true" />
    </Row>
    <Row  android:keyWidth="9%p" android:keyboardMode="@integer/keyboard_normal">
        <Key android:codes="29" android:keyLabel="a" android:keyEdgeFlags="left" android:horizontalGap="5%p" />
        <Key android:codes="47" android:keyLabel="s" />
        <Key android:codes="32" android:keyLabel="d" />
        <Key android:codes="34" android:keyLabel="f" />
        <Key android:codes="35" android:keyLabel="g" />
        <Key android:codes="36" android:keyLabel="h" />
        <Key android:codes="38" android:keyLabel="j" />
        <Key android:codes="39" android:keyLabel="k" />
        <Key android:codes="40" android:keyLabel="l" />
        <Key android:codes="-1" android:keyIcon="@mipmap/keyboard_next" android:keyEdgeFlags="right" android:horizontalGap="4%p" />
    </Row>
    <Row android:keyWidth="8%p" android:keyboardMode="@integer/keyboard_symbol">
        <Key android:codes="9" android:keyLabel="\@" android:keyEdgeFlags="left" android:horizontalGap="5%p" />
        <Key android:codes="3" android:keyLabel="#" />
        <Key android:codes="11" android:keyLabel="$" />
        <Key android:codes="-1" android:keyLabel="_" />
        <Key android:codes="14" android:keyLabel="&amp;" />
        <Key android:codes="69" android:keyLabel="-" />
        <Key android:codes="81" android:keyLabel="+" />
        <Key android:codes="162" android:keyLabel="(" />
        <Key android:codes="163" android:keyLabel=")" />
        <Key android:codes="154" android:keyLabel="/" />
        <Key android:codes="-5" android:keyIcon="@mipmap/keyboard_next" android:keyEdgeFlags="right" android:horizontalGap="4%p" />
    </Row>
    <Row android:keyboardMode="@integer/keyboard_normal">
        <Key android:codes="59,60" android:keyIcon="@mipmap/keyboard_shift_arrow_off" android:isModifier="true" android:isSticky="true" android:keyEdgeFlags="left" />
        <Key android:codes="54" android:keyLabel="z" />
        <Key android:codes="52" android:keyLabel="x" />
        <Key android:codes="31" android:keyLabel="c" />
        <Key android:codes="50" android:keyLabel="v" />
        <Key android:codes="30" android:keyLabel="b" />
        <Key android:codes="42" android:keyLabel="n" />
        <Key android:codes="41" android:keyLabel="m" />
        <Key android:codes="33" android:keyLabel="!" />
        <Key android:codes="-1" android:keyLabel="\?" android:keyEdgeFlags="right"/>
    </Row>
    <Row android:keyboardMode="@integer/keyboard_symbol">
        <Key android:codes="161" android:keyLabel="=" android:horizontalGap="5%p" android:keyEdgeFlags="left" />
        <Key android:codes="12" android:keyLabel="%" />
        <Key android:codes="15" android:keyLabel="*" />
        <Key android:codes="-1" android:keyLabel="&quot;" />
        <Key android:codes="75" android:keyLabel="\'" />
        <Key android:codes="-1" android:keyLabel=":" />
        <Key android:codes="74" android:keyLabel=";" />
        <Key android:codes="-1" android:keyLabel="!" />
        <Key android:codes="-1" android:keyLabel="\?" android:keyEdgeFlags="right"/>
    </Row>
    <Row android:keyWidth="20%p" android:keyboardMode="@integer/keyboard_normal">
        <Key android:codes="63" android:keyLabel="\?123" android:keyWidth="11%p" android:keyEdgeFlags="left"/>
        <Key android:codes="56" android:keyLabel="." android:keyWidth="12%p"  />
        <Key android:codes="62" android:keyWidth="50%p" android:isRepeatable="true"/>
        <Key android:codes="55" android:keyLabel="," android:keyWidth="12%p"  />
        <Key android:codes="66" android:keyIcon="@mipmap/keyboard_done" android:keyWidth="15%p" android:keyEdgeFlags="right"/>
    </Row>
    <Row android:keyWidth="20%p" android:keyboardMode="@integer/keyboard_symbol">
        <Key android:codes="63" android:keyLabel="ABC" android:keyWidth="11%p" android:keyEdgeFlags="left"/>
        <Key android:codes="56" android:keyLabel="." android:keyWidth="12%p"  />
        <Key android:codes="62" android:keyWidth="50%p" android:isRepeatable="true"/>
        <Key android:codes="55" android:keyLabel="," android:keyWidth="12%p" />
        <Key android:codes="66" android:keyIcon="@mipmap/keyboard_done" android:keyWidth="15%p" android:keyEdgeFlags="right"/>
    </Row>
</Keyboard>

0 个答案:

没有答案