使用Remote定制键盘焦点问题

时间:2017-02-22 12:03:10

标签: android keyboard android-tv

我正在为AndroidTV创建应用程序,我的要求是创建自定义屏幕键盘。我使用 KyboardView 创建了一个键盘,我将键盘包裹在 RelativeLayout 中。我无法使用DPAD Remote专注于键盘。以下是我的代码 -

活动

KeyboardView keyboardView = (KeyboardView) view.findViewById(R.id.keyboardView);
keyboardView.setKeyboard(new Keyboard(getActivity(),R.xml.keyboard_qwerty));
keyboardView.setPreviewEnabled(false);
keyboardView.setOnKeyboardActionListener(new KeyboardView.OnKeyboardActionListener() {
        @Override
        public void onPress(int primaryCode) {

        }

        @Override
        public void onRelease(int primaryCode) {

        }

        @Override
        public void onKey(int primaryCode, int[] keyCodes) {
            Log.e(TAG, "#onkey - "+primaryCode);
        }

        @Override
        public void onText(CharSequence text) {
            YuppLog.e(TAG, "#text - "+text);

        }

        @Override
        public void swipeLeft() {

        }

        @Override
        public void swipeRight() {

        }

        @Override
        public void swipeDown() {

        }

        @Override
        public void swipeUp() {

        }
    });

keyboard_qwertty

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyHeight="7.5%p"
android:horizontalGap="0.3%p"
android:verticalGap="0.5%p"
android:keyWidth="10%p">
<Row>
    <Key
        android:codes="49"
        android:keyEdgeFlags="left"
        android:keyLabel="1" />
    <Key
        android:codes="50"
        android:keyLabel="2" />
    <Key
        android:codes="51"
        android:keyLabel="3" />
    <Key
        android:codes="52"
        android:keyLabel="4" />
    <Key
        android:codes="53"
        android:keyLabel="5" />
    <Key
        android:codes="54"
        android:keyLabel="6" />
    <Key
        android:codes="55"
        android:keyLabel="7" />
    <Key
        android:codes="56"
        android:keyLabel="8" />
    <Key
        android:codes="57"
        android:keyLabel="9" />
    <Key
        android:codes="48"
        android:keyEdgeFlags="right"
        android:keyLabel="0" />
</Row>
<Row>
    <Key
        android:codes="113"
        android:keyEdgeFlags="left"
        android:keyLabel="q" />
    <Key
        android:codes="119"
        android:keyLabel="w" />
    <Key
        android:codes="101"
        android:keyLabel="e" />
    <Key
        android:codes="114"
        android:keyLabel="r" />
    <Key
        android:codes="116"
        android:keyLabel="t" />
    <Key
        android:codes="121"
        android:keyLabel="y" />
    <Key
        android:codes="117"
        android:keyLabel="u" />
    <Key
        android:codes="105"
        android:keyLabel="i" />
    <Key
        android:codes="111"
        android:keyLabel="o" />
    <Key
        android:codes="112"
        android:keyEdgeFlags="right"
        android:keyLabel="p" />
</Row>
<Row>
    <Key
        android:codes="97"
        android:keyEdgeFlags="left"
        android:keyLabel="a" />
    <Key
        android:codes="115"
        android:keyLabel="s" />
    <Key
        android:codes="100"
        android:keyLabel="d" />
    <Key
        android:codes="102"
        android:keyLabel="f" />
    <Key
        android:codes="103"
        android:keyLabel="g" />
    <Key
        android:codes="104"
        android:keyLabel="h" />
    <Key
        android:codes="106"
        android:keyLabel="j" />
    <Key
        android:codes="107"
        android:keyLabel="k" />
    <Key
        android:codes="108"
        android:keyLabel="l" />

    <Key
        android:codes="45"
        android:keyEdgeFlags="right"
        android:keyLabel="-" />
</Row>
<Row>
    <Key
        android:codes="-1"
        android:isModifier="true"
        android:isSticky="true"
        android:keyEdgeFlags="left"
        android:keyIcon="@drawable/ic_keyboard_capital"
        android:keyWidth="10%p" />
    <Key
        android:codes="122"
        android:keyLabel="z" />
    <Key
        android:codes="120"
        android:keyLabel="x" />
    <Key
        android:codes="99"
        android:keyLabel="c" />
    <Key
        android:codes="118"
        android:keyLabel="v" />
    <Key
        android:codes="98"
        android:keyLabel="b" />
    <Key
        android:codes="110"
        android:keyLabel="n" />
    <Key
        android:codes="109"
        android:keyLabel="m" />
    <Key
        android:codes="96"
        android:keyLabel="'"
        android:keyEdgeFlags="right" />
</Row>
<Row android:rowEdgeFlags="bottom">
    <Key
        android:codes="123123"
        android:keyLabel="!#$"
        android:keyWidth="20.3%p" />
    <Key
        android:codes="32"
        android:keyIcon="@drawable/ic_keyboard_space_bar"
        android:keyWidth="48%p" />

    <Key
        android:codes="-5"
        android:isRepeatable="true"
        android:keyEdgeFlags="right"
        android:keyIcon="@drawable/ic_keyboard_delete"
        android:keyWidth="30.6%p" />
</Row>

1 个答案:

答案 0 :(得分:1)

经过大量的研究并且无法得到我的答案后,我以某种方式设法使用我自己的逻辑并在 android.inputmethodservice.Keyboard 类的帮助下创建了一个自定义键盘。下面是我获取自定义键盘的代码。

package com.gaurav.keyboard;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.inputmethodservice.Keyboard;
import android.support.v7.widget.RecyclerView;


/**
 * Created by Gaurav on 29/12/16.
 * <p>
 * Utils class returns keyboard based on the requested Keyboard type 
   {KB_NUMERIC, KB_QWERTY, KB_EMAIL, KB_MONTH & KB_YEAR}.
 * <p>
 * Client has to implement {@link OnKeyClickListener} to get key click 
   event.
 * <p>
 * For multiple keyboards instantiate the class.
 */

public class KeyboardHelper {
public final String TAG = KeyboardHelper.class.getSimpleName();
private Context mContext;
private CustomKeyboardView keyboardView;

/**
 * CONSTANTS FOR KEYBOARD TYPES
 */
public static final int KB_NUMERIC = 1;
public static final int KB_QWERTY = 2;
public static final int KB_EMAIL = 3;
public static final int KB_MONTH = 4;
public static final int KB_YEAR = 5;
public static final int KB_SEARCH = 6;

/**
 * KEYBOARD KEY CODES
 */
public static final int KEY_CODE_SPACE = 32;
public static final int KEY_CODE_BACK_SPACE = -5;
public static final int KEY_CODE_SPECIAL_CHARACTER = 123123;
public static final int KEY_CODE_CLEAR = 123124;
public static final int KEY_CODE_CAPITAL_CASE = -1;
public static final int KEY_CODE_HOTMAIL_DOT_COM = 54321;
public static final int KEY_CODE_GMAIL_DOT_COM = 54320;
public static final int KEY_CODE_YAHOO_DOT_COM = 54319;
public static final int KEY_CODE_AT_THE_RATE = 85245;
public static final int KEY_CODE_DOT = 789789;
public static final int KEY_CODE_DOT_COM = 456654;
static final int KEY_CODE_EMPTY = 540;

/**
 * interface to get key event of keyboard
 * <p>
 * to get key code - clickedKey.codes[0]
 * NOTE :- All keys have single code.
 */
public interface OnKeyClickListener {
    void onKeyClick(Keyboard.Key clickedKey);
}

public KeyboardHelper(Context mContext, OnKeyClickListener onKeyClickListener) {
    this.mContext = mContext;
    keyboardView = new CustomKeyboardView(mContext, onKeyClickListener);
    keyboardView.setHasFixedSize(true);
}

/**
 * sets the selector for key
 *
 * @param selector - {@link Drawable}
 */
public void setKeySelector(Drawable selector) {
    if (selector != null)
        keyboardView.setKeyDrawable(selector);
}

/**
 * Returns the keyboard view based on the keyboard type
 *
 * @param mKeyboardType - int
 * @return - {@link RecyclerView}
 */
public RecyclerView requestKeyboard(int mKeyboardType) {
    switch (mKeyboardType) {
        case KB_NUMERIC:
            return keyboardView.loadKeyboard(KB_NUMERIC, new Keyboard(mContext, R.xml.keyboard_numeric));
        case KB_QWERTY:
            return keyboardView.loadKeyboard(KB_QWERTY, new Keyboard(mContext, R.xml.keyboard_qwerty));
        case KB_EMAIL:
            return keyboardView.loadKeyboard(KB_EMAIL, new Keyboard(mContext, R.xml.keyboard_email));
        case KB_MONTH:
            return keyboardView.loadKeyboard(KB_MONTH, new Keyboard(mContext, R.xml.keyboard_month));
        case KB_YEAR:
            return keyboardView.loadKeyboard(KB_YEAR, new Keyboard(mContext, R.xml.keyboard_month));
        case KB_SEARCH:
            return keyboardView.loadKeyboard(KB_SEARCH, new Keyboard(mContext, R.xml.keyboard_search));
        default:
            return keyboardView.loadKeyboard(KB_QWERTY, new Keyboard(mContext, R.xml.keyboard_qwerty));
    }
}

}

CustomKeyboardView 会返回一个包含在RecyclerView中的完全成熟的键盘。虽然我在 initializeRows()中硬编码了一些值,但键数是固定的。

package com.gaurav.keyboard;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.inputmethodservice.Keyboard;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import java.util.Calendar;
import java.util.List;

/**
 * Created by Gaurav on 21/2/17.
 * This class extends {@link RecyclerView} and returns Keyboard layout
 */
class CustomKeyboardView extends RecyclerView {
    private final String TAG = CustomKeyboardView.class.getSimpleName();

private Context mContext;
private Resources resources;

private int ROWS;

private LinearLayoutManager linearLayoutManager;

private List<Keyboard.Key> mKeys;
private List<Keyboard.Key> mModifierKeys;
private int keyboardType;
private int startIndex;
private int endIndex;
int[] years = new int[3];
private int currentYear;

private KeyboardAdapter keyboardAdapter;

private KeyboardHelper.OnKeyClickListener onKeyClickListener;
private boolean areKeysCapital = false;
private boolean areKeysSpecial = false;

private Drawable keyDrawable;

private boolean isKeyPressed = false;

private List<Keyboard.Key> mSpecialKeys;
private List<Keyboard.Key> mCharKeys;

CustomKeyboardView(Context context, KeyboardHelper.OnKeyClickListener mCallBack) {
    super(context);
    mContext = context;
    resources = getResources();

    linearLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);

    try {
        onKeyClickListener = mCallBack;
    } catch (Exception e) {
        e.getMessage();
        e.printStackTrace();
    }

    setLayoutManager(linearLayoutManager);
}

/**
 * sets drawable for key layout
 *
 * @param keyDrawable - {@link Drawable}
 */
void setKeyDrawable(Drawable keyDrawable) {
    this.keyDrawable = keyDrawable;
}

/**
 * Returns {@link CustomKeyboardView} based on the keyboard type
 * @param keyboardType - requested keyboard
 * @param keyboard     - {@link Keyboard} needs to be loaded
 * @return - {@link CustomKeyboardView}
 */
CustomKeyboardView loadKeyboard(int keyboardType, Keyboard keyboard) {
    this.keyboardType = keyboardType;
    mKeys = keyboard.getKeys();
    if (keyboardType == KeyboardHelper.KB_YEAR) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        currentYear = calendar.get(Calendar.YEAR);
    }
    initializeRows();
    keyboardAdapter = new KeyboardAdapter(mContext);
    setAdapter(keyboardAdapter);
    return this;
}

/**
 * initializes rows of the keyboard's rows and columns
 * numeric, month and year keyboard will have 4 rows, qwerty keyboard has 6 rows and email keyboard has 6 rows
 */
private void initializeRows() {
    switch (keyboardType) {
        case KeyboardHelper.KB_NUMERIC:
            startIndex = 0;
            endIndex = 3;
            ROWS = 4;
            break;
        case KeyboardHelper.KB_QWERTY:
            startIndex = 0;
            endIndex = 10;
            ROWS = 5;
            mSpecialKeys = new Keyboard(mContext, R.xml.special_character_keys).getKeys();
            mCharKeys = new Keyboard(mContext, R.xml.character_keys).getKeys();
            break;
        case KeyboardHelper.KB_EMAIL:
            startIndex = 0;
            endIndex = 10;
            ROWS = 6;
            mSpecialKeys = new Keyboard(mContext, R.xml.special_character_keys).getKeys();
            mCharKeys = new Keyboard(mContext, R.xml.character_keys).getKeys();
            break;
        case KeyboardHelper.KB_MONTH:
            startIndex = 0;
            endIndex = 3;
            ROWS = 4;
            break;
        case KeyboardHelper.KB_YEAR:
            startIndex = 0;
            endIndex = 3;
            ROWS = 4;
            break;
        case KeyboardHelper.KB_SEARCH:
            startIndex = 0;
            endIndex = 19;
            ROWS = 3;
            break;
    }
}

/**
 * gets keys for the given positioned row
 * NOTE - calculations have been done based on keys in each rows.
 * @param position - row position
 * @return - List of {@link android.inputmethodservice.Keyboard.Key}
 */
private List<Keyboard.Key> getRowKeys(int position) {
    List<Keyboard.Key> rowKeys = null;
    switch (keyboardType) {
        case KeyboardHelper.KB_NUMERIC:
            if (position < 3)
                rowKeys = mKeys.subList(startIndex, endIndex);
            else
                rowKeys = mKeys.subList(startIndex, mKeys.size());
            startIndex = endIndex;
            endIndex = endIndex + 3;
            break;
        case KeyboardHelper.KB_QWERTY:
            if (position < 3)
                rowKeys = mKeys.subList(startIndex, endIndex);
            else if (position == 3)
                rowKeys = mKeys.subList(30, 39);
            else if (position == 4)
                rowKeys = mKeys.subList(39, mKeys.size());
            startIndex = endIndex;
            endIndex = endIndex + 10;
            break;
        case KeyboardHelper.KB_EMAIL:
            if (position < 3)
                rowKeys = mKeys.subList(startIndex, endIndex);
            else if (position == 3)
                rowKeys = mKeys.subList(30, 39);
            else if (position == 4)
                rowKeys = mKeys.subList(39, 42);
            else if (position == 5)
                rowKeys = mKeys.subList(42, mKeys.size());
            startIndex = endIndex;
            endIndex = endIndex + 10;
            break;
        case KeyboardHelper.KB_MONTH:
            rowKeys = mKeys.subList(startIndex, endIndex);
            startIndex = endIndex;
            endIndex = endIndex + 3;
            break;
        case KeyboardHelper.KB_YEAR:
            rowKeys = mKeys.subList(startIndex, endIndex);
            startIndex = endIndex;
            endIndex = endIndex + 3;
            for (int i = 0; i < years.length; i++) {
                years[i] = currentYear++;
            }
            break;
        case KeyboardHelper.KB_SEARCH:
            rowKeys = mKeys.subList(startIndex, endIndex);
            startIndex = endIndex;
            endIndex = endIndex + 19;
            if (position == 1)
                endIndex = mKeys.size();
            break;
    }
    return rowKeys;
}

/**
 * Adapter for the keyboard
 * It helps to set each row
 */
private class KeyboardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final Context mContext;

    KeyboardAdapter(Context mContext) {
        this.mContext = mContext;
    }

    class KeyboardViewHolder extends RecyclerView.ViewHolder {
        private RecyclerView keyboardRow;

        KeyboardViewHolder(View itemView) {
            super(itemView);
            keyboardRow = (RecyclerView) itemView.findViewById(R.id.keyboardRow);
            keyboardRow.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        }
    }

    @Override
    public int getItemCount() {
        return ROWS;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        KeyboardViewHolder _holder = (KeyboardViewHolder) holder;

        _holder.keyboardRow.setAdapter(new KeyAdapter(getRowKeys(position)));

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_keyboard_row, parent, false);

        return new KeyboardViewHolder(v);
    }
}

/**
 * Adapter for row key
 */
private class KeyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<Keyboard.Key> rowKeys;

    KeyAdapter(List<Keyboard.Key> rowKeys) {
        this.rowKeys = rowKeys;
    }

    class KeyViewHolder extends RecyclerView.ViewHolder {
        private AppCompatImageView keyIcon;
        private KeyTextView keyLabel, keySupportLabel;
        private RelativeLayout key_layout;

        KeyViewHolder(final View itemView) {
            super(itemView);
            key_layout = (RelativeLayout) itemView.findViewById(R.id.key_layout);
            keyIcon = (AppCompatImageView) itemView.findViewById(R.id.keyIcon);
            keyLabel = (KeyTextView) itemView.findViewById(R.id.keyLabel);
            keySupportLabel = (KeyTextView) itemView.findViewById(R.id.keySupportLabel);

            /*
             * if user has defined keyDrawable then no need to apply default animations
             */
            if (keyDrawable == null) {
                itemView.setOnKeyListener(new OnKeyListener() {
                    @Override
                    public boolean onKey(View view, int i, KeyEvent keyEvent) {
                        if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER) {
                            Keyboard.Key clickedKey = rowKeys.get(getAdapterPosition());
                            if(keyEvent.getAction() == KeyEvent.ACTION_UP) {
                                itemView.setBackground(getResources().getDrawable(R.drawable.key_press_gradient));
                                clickedKey.label = keyLabel.getText();
                                if (clickedKey.modifier) {
                                    if(clickedKey.codes[0] == KeyboardHelper.KEY_CODE_CAPITAL_CASE)
                                        toggleKeyCase();
                                    else if(clickedKey.codes[0] == KeyboardHelper.KEY_CODE_SPECIAL_CHARACTER)
                                        toggleSpecialCase(keyLabel);
                                } else {
                                    if (onKeyClickListener != null) {
                                        onKeyClickListener.onKeyClick(clickedKey);
                                    }
                                }
                            }else {
                                if((keyEvent.getAction() == KeyEvent.ACTION_UP) && clickedKey.codes[0] == KeyboardHelper.KEY_CODE_BACK_SPACE) {
                                    if (onKeyClickListener != null) {
                                        onKeyClickListener.onKeyClick(clickedKey);
                                    }
                                }
                            }
                        } else
                            itemView.setBackground(getResources().getDrawable(R.drawable.tv_key_board_key_background));
                        return false;
                    }
                });
            }
        }
    }

    @Override
    public int getItemCount() {
        return rowKeys.size();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        KeyViewHolder _holder = (KeyViewHolder) holder;
        _holder.itemView.setFocusable(true);
        _holder.itemView.setFocusableInTouchMode(true);
        _holder.itemView.setVisibility(VISIBLE);
        Keyboard.Key mKey = rowKeys.get(position);
        if (mKey.icon != null) {
            _holder.keyIcon.setBackgroundDrawable(mKey.icon);
            _holder.keyIcon.setVisibility(View.VISIBLE);
        } else {
            if (keyboardType == KeyboardHelper.KB_YEAR)
                _holder.keyLabel.setText("" + years[position]);
            else
                _holder.keyLabel.setText(mKey.label);
            _holder.keyIcon.setVisibility(View.GONE);
        }

        /*
         * for EMAIL keyboard - gmail, hotmail and yahoo keys label text size has to be changed
         */
        if (keyboardType == KeyboardHelper.KB_EMAIL) {
            int keyCode = mKey.codes[0];
            if (keyCode == KeyboardHelper.KEY_CODE_GMAIL_DOT_COM
                    || keyCode == KeyboardHelper.KEY_CODE_YAHOO_DOT_COM
                    || keyCode == KeyboardHelper.KEY_CODE_HOTMAIL_DOT_COM) {
                _holder.keyLabel.setTextSize(resources.getDimension(R.dimen.key_label_size));
            }
        } else if (keyboardType == KeyboardHelper.KB_MONTH) {
            _holder.keySupportLabel.setText(mKey.text);
            _holder.keySupportLabel.setVisibility(View.VISIBLE);
        } else if(keyboardType == KeyboardHelper.KB_SEARCH && mKey.codes[0] == KeyboardHelper.KEY_CODE_EMPTY) {
            _holder.itemView.setFocusable(false);
            _holder.itemView.setFocusableInTouchMode(false);
            _holder.itemView.setVisibility(INVISIBLE);
        }

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(mKey.width, mKey.height);
        layoutParams.setMargins(mKey.gap, mKey.gap, mKey.gap, mKey.gap);
        _holder.key_layout.setLayoutParams(layoutParams);

        if (keyDrawable != null) {
            _holder.key_layout.setBackground(keyDrawable);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_key, parent, false);

        return new KeyViewHolder(v);
    }
}

/**
 * toggles key case
 * NOTE - only qwerty and email keyboard has change case key and 1,2 and 3'rows will be going to update
 */
private void toggleKeyCase() {
    try {
        if (keyboardAdapter != null) {
            for (int i = 1; i < 4; i++) {
                RecyclerView keyboardRow = (RecyclerView) linearLayoutManager.getChildAt(i);
                LinearLayoutManager keyBoardLayoutManager = (LinearLayoutManager) keyboardRow.getLayoutManager();
                for (int j = 0; j < keyBoardLayoutManager.getItemCount(); j++) {
                    View keyView = keyBoardLayoutManager.getChildAt(j);
                    KeyTextView yuppTextView = (KeyTextView) keyView.findViewById(R.id.keyLabel);
                    if (!areKeysCapital) {
                        yuppTextView.setText(yuppTextView.getText().toString().toUpperCase());
                    } else {
                        yuppTextView.setText(yuppTextView.getText().toString().toLowerCase());
                    }
                }
            }
            areKeysCapital = !areKeysCapital;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * switch keyboard case between special character and alphabet
 * Special characters are available only for KB_QWERTY and KB_EMAIL.
 * only top 4 rows are going to be changed
 */
private void toggleSpecialCase(KeyTextView keyLabel) {
    try {
        if (keyboardAdapter != null
                && mSpecialKeys != null
                && (keyboardType == KeyboardHelper.KB_EMAIL || keyboardType == KeyboardHelper.KB_QWERTY)) {
            startIndex = 0;
            endIndex = 10;
            if(areKeysSpecial) {
                keyLabel.setText("!#$");
                for (int i = 0; i < 4; i++) {
                    RecyclerView keyboardRow = (RecyclerView) linearLayoutManager.getChildAt(i);
                    LinearLayoutManager keyBoardLayoutManager = (LinearLayoutManager) keyboardRow.getLayoutManager();
                    List<Keyboard.Key> rowKeys = mCharKeys.subList(startIndex, endIndex);
                    for (int j = 0; j < keyBoardLayoutManager.getItemCount(); j++) {
                        View keyView = keyBoardLayoutManager.getChildAt(j);
                        KeyTextView yuppTextView = (KeyTextView) keyView.findViewById(R.id.keyLabel);
                        yuppTextView.setText(rowKeys.get(j).label);
                    }
                    startIndex = endIndex;
                    if(i == 2)
                        endIndex = mSpecialKeys.size();
                    else
                        endIndex = endIndex + 10;
                }
                areKeysSpecial = false;
            }else if(!areKeysSpecial) {
                keyLabel.setText("abc");
                for (int i = 0; i < 4; i++) {
                    RecyclerView keyboardRow = (RecyclerView) linearLayoutManager.getChildAt(i);
                    LinearLayoutManager keyBoardLayoutManager = (LinearLayoutManager) keyboardRow.getLayoutManager();
                    List<Keyboard.Key> rowKeys = mSpecialKeys.subList(startIndex, endIndex);
                    for (int j = 0; j < keyBoardLayoutManager.getItemCount(); j++) {

                        View keyView = keyBoardLayoutManager.getChildAt(j);
                        KeyTextView yuppTextView = (KeyTextView) keyView.findViewById(R.id.keyLabel);
                        if (!areKeysSpecial) {
                            yuppTextView.setText(rowKeys.get(j).label);
                        } else {
                            yuppTextView.setText(yuppTextView.getText().toString().toLowerCase());
                        }
                    }
                    startIndex = endIndex;
                    if(i == 2)
                        endIndex = mSpecialKeys.size();
                    else
                        endIndex = endIndex + 10;
                }
                areKeysSpecial = true;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

keyboard_email.xml的示例。提供关键的高度,宽度和间隙。

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:horizontalGap="@dimen/key_gap"
android:keyHeight="@dimen/key_email_default_height"
android:keyWidth="@dimen/key_email_default_width"
android:verticalGap="@dimen/key_gap">
<Row>
    <Key
        android:codes="49"
        android:keyEdgeFlags="left"
        android:keyLabel="1" />
    <Key
        android:codes="50"
        android:keyLabel="2" />
    <Key
        android:codes="51"
        android:keyLabel="3" />
    <Key
        android:codes="52"
        android:keyLabel="4" />
    <Key
        android:codes="53"
        android:keyLabel="5" />
    <Key
        android:codes="54"
        android:keyLabel="6" />
    <Key
        android:codes="55"
        android:keyLabel="7" />
    <Key
        android:codes="56"
        android:keyLabel="8" />
    <Key
        android:codes="57"
        android:keyLabel="9" />
    <Key
        android:codes="48"
        android:keyEdgeFlags="right"
        android:keyLabel="0" />
</Row>
<Row>
    <Key
        android:codes="113"
        android:keyEdgeFlags="left"
        android:keyLabel="q" />
    <Key
        android:codes="119"
        android:keyLabel="w" />
    <Key
        android:codes="101"
        android:keyLabel="e" />
    <Key
        android:codes="114"
        android:keyLabel="r" />
    <Key
        android:codes="116"
        android:keyLabel="t" />
    <Key
        android:codes="121"
        android:keyLabel="y" />
    <Key
        android:codes="117"
        android:keyLabel="u" />
    <Key
        android:codes="105"
        android:keyLabel="i" />
    <Key
        android:codes="111"
        android:keyLabel="o" />
    <Key
        android:codes="112"
        android:keyEdgeFlags="right"
        android:keyLabel="p" />
</Row>
<Row>
    <Key
        android:codes="97"
        android:keyEdgeFlags="left"
        android:keyLabel="a" />
    <Key
        android:codes="115"
        android:keyLabel="s" />
    <Key
        android:codes="100"
        android:keyLabel="d" />
    <Key
        android:codes="102"
        android:keyLabel="f" />
    <Key
        android:codes="103"
        android:keyLabel="g" />
    <Key
        android:codes="104"
        android:keyLabel="h" />
    <Key
        android:codes="106"
        android:keyLabel="j" />
    <Key
        android:codes="107"
        android:keyLabel="k" />
    <Key
        android:codes="108"
        android:keyLabel="l" />

    <Key
        android:codes="45"
        android:keyEdgeFlags="right"
        android:keyLabel="-" />
</Row>
<Row>
    <Key
        android:codes="-1"
        android:isModifier="true"
        android:isSticky="true"
        android:keyEdgeFlags="left"
        android:keyIcon="@drawable/ic_keyboard_capital"
        android:keyWidth="@dimen/key_capital_case_width" />

    <Key
        android:codes="122"
        android:keyLabel="z" />
    <Key
        android:codes="120"
        android:keyLabel="x" />
    <Key
        android:codes="99"
        android:keyLabel="c" />
    <Key
        android:codes="118"
        android:keyLabel="v" />
    <Key
        android:codes="98"
        android:keyLabel="b" />
    <Key
        android:codes="110"
        android:keyLabel="n" />
    <Key
        android:codes="109"
        android:keyLabel="m" />
    <Key
        android:codes="96"
        android:keyEdgeFlags="right"
        android:keyLabel="'" />
</Row>
<Row>
    <Key
        android:codes="54321"
        android:isRepeatable="true"
        android:keyEdgeFlags="left"
        android:keyHeight="@dimen/key_email_domain_height"
        android:keyLabel="@string/hotmail.com"
        android:keyWidth="@dimen/key_email_domain_width" />
    <Key
        android:codes="54320"
        android:isRepeatable="true"
        android:keyHeight="@dimen/key_email_domain_height"
        android:keyLabel="@string/gmail.com"
        android:keyWidth="@dimen/key_email_domain_width" />
    <Key
        android:codes="54319"
        android:isRepeatable="true"
        android:keyEdgeFlags="right"
        android:keyHeight="@dimen/key_email_domain_height"
        android:keyLabel="@string/yahoo.com"
        android:keyWidth="@dimen/key_email_domain_width" />
</Row>
<Row android:rowEdgeFlags="bottom">
    <Key
        android:codes="123123"
        android:keyEdgeFlags="left"
        android:isModifier="true"
        android:isSticky="true"
        android:keyLabel="!#$"
        android:keyWidth="@dimen/key_special_char_width" />

    <Key
        android:codes="85245"
        android:keyLabel="\@"
        android:keyWidth="@dimen/key_special_char_width" />

    <Key
        android:codes="789789"
        android:keyLabel="." />

    <Key
        android:codes="456654"
        android:keyLabel=".com"
        android:keyWidth="@dimen/key_special_char_width" />

    <Key
        android:codes="-5"
        android:isRepeatable="true"
        android:keyEdgeFlags="right"
        android:keyIcon="@drawable/ic_keyboard_delete"
        android:keyWidth="@dimen/key_qwerty_delete_width" />
</Row>
</Keyboard>

Implementation-     RecyclerView qwertyKeyboard = new KeyboardHelper(mActivity,PaymentInfoFragment.this).requestKeyboard(KeyboardHelper.KB_QWERTY);

实施KeyboardHelper.OnKeyClickListener以获取点击事件。

此代码适用于我的要求。