4

几天前,我在我的 android 应用程序中为几个 EditText 实现了自定义键盘。这些 EditText 的数量可能会因情况而异。在我的键盘中,我想实现“Enter”键,这将请求焦点到布局中的下一个 EditText(如在标准 Android 软键盘中)。我正在尝试使用

editText.requestFocus(View.FOCUS_DOWN)

但似乎这种方法不起作用。

这是我的键盘实现:

public class MacKeyboard implements KeyboardView.OnKeyboardActionListener {

    EditText mParentEditText;
    KeyboardView mKeyboardView;

    static final int CODE_BACKSPACE = 16;
    static final int CODE_HIDE_KEYBOARD = 17;
    static final int CODE_NEXT_INPUT = 18;

    static final HashMap<Integer, Integer> codeMap = new HashMap<Integer, Integer>() {{
        put(0, KeyEvent.KEYCODE_0);
        put(1, KeyEvent.KEYCODE_1);
        put(2, KeyEvent.KEYCODE_2);
        put(3, KeyEvent.KEYCODE_3);
        put(4, KeyEvent.KEYCODE_4);
        put(5, KeyEvent.KEYCODE_5);
        put(6, KeyEvent.KEYCODE_6);
        put(7, KeyEvent.KEYCODE_7);
        put(8, KeyEvent.KEYCODE_8);
        put(9, KeyEvent.KEYCODE_9);
        put(10, KeyEvent.KEYCODE_A);
        put(11, KeyEvent.KEYCODE_B);
        put(12, KeyEvent.KEYCODE_C);
        put(13, KeyEvent.KEYCODE_D);
        put(14, KeyEvent.KEYCODE_E);
        put(15, KeyEvent.KEYCODE_F);
    }};

    void handleBackspace() {
        mParentEditText.onKeyDown(
                KeyEvent.KEYCODE_DEL,
                new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)
        );
    }

    public MacKeyboard(KeyboardView keyboardView) {
        mKeyboardView = keyboardView;
    }

    public boolean isVisible() {
        return mKeyboardView.getVisibility() == View.VISIBLE;
    }

    public void hide() {
        mKeyboardView.setVisibility(View.GONE);
    }

    public void show(EditText parentEditText) {
        mKeyboardView.setVisibility(View.VISIBLE);
        mParentEditText = parentEditText;
    }

    public void toggle(EditText parentEditText) {
        switch ( mKeyboardView.getVisibility() ) {
            case View.VISIBLE:
                mKeyboardView.setVisibility(View.GONE);
                break;

            case View.GONE:

            case View.INVISIBLE:
                mKeyboardView.setVisibility(View.VISIBLE);
                mParentEditText = parentEditText;
                break;
        }
    }

    @Override
    public void onPress(int primaryCode) {
        //do nothing
    }

    @Override
    public void onRelease(int primaryCode) {
        //do nothing
    }

    @Override
    public void onKey(int primaryCode, int[] keyCodes) {
        if ( mParentEditText != null ) {
            if ( codeMap.containsKey(primaryCode) ) {
                int actualKeycode = codeMap.get(primaryCode);
                if ( primaryCode < 10 ) {
                    mParentEditText.onKeyDown(
                            actualKeycode,
                            new KeyEvent(KeyEvent.ACTION_DOWN, actualKeycode)
                    );
                } else {
                    mParentEditText.onKeyDown(
                            actualKeycode,
                            new KeyEvent(0, 0, KeyEvent.ACTION_DOWN, actualKeycode, 0, KeyEvent.META_SHIFT_ON)
                    );
                }
            } else {
                switch ( primaryCode ) {

                    case CODE_BACKSPACE:
                        handleBackspace();
                        break;

                    case CODE_HIDE_KEYBOARD:
                        hide();
                        break;

                    case CODE_NEXT_INPUT:

                        mParentEditText.clearFocus();
                        mParentEditText.requestFocus(View.FOCUS_DOWN);

                       /* here i also tried 
                         mParentEditText.onKeyDown(
                            KeyEvent.KEYCODE_ENTER,
                            new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER)
                         );

                         and editText.post(...);
                       */

                }
            }
        }
    }

    @Override
    public void onText(CharSequence text) {
        //do nothing
    }

    @Override
    public void swipeLeft() {
        handleBackspace();
    }

    @Override
    public void swipeRight() {
        //do nothing
    }

    @Override
    public void swipeDown() {
        hide();
    }

    @Override
    public void swipeUp() {
        //do nothing
    }
}

XML 键盘布局

<Keyboard
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:keyWidth="20%p"
        android:keyHeight="60.0dip"
        android:horizontalGap="3.0dip"
        android:verticalGap="2.0dip">

    <Row>
        <Key android:codes="1" android:keyLabel="1"/>
        <Key android:codes="2" android:keyLabel="2"/>
        <Key android:codes="3" android:keyLabel="3"/>
        <Key android:codes="4" android:keyLabel="4"/>
        <Key android:codes="5" android:keyLabel="5"/>
    </Row>

    <Row>
        <Key android:codes="6" android:keyLabel="6"/>
        <Key android:codes="7" android:keyLabel="7"/>
        <Key android:codes="8" android:keyLabel="8"/>
        <Key android:codes="9" android:keyLabel="9"/>
        <Key android:codes="0" android:keyLabel="0"/>
    </Row>

    <Row>
        <Key android:codes="10" android:keyLabel="A"/>
        <Key android:codes="11" android:keyLabel="B"/>
        <Key android:codes="12" android:keyLabel="C"/>
        <Key android:codes="13" android:keyLabel="D"/>
        <Key android:codes="14" android:keyLabel="E"/>
        <Key android:codes="15" android:keyLabel="F"/>
    </Row>

    <Row android:keyWidth="33.3%p">
        <Key android:codes="17" android:keyLabel="."/>
        <Key android:codes="18" android:keyLabel="&gt;"/>
        <Key android:codes="16" android:keyIcon="@drawable/ic_backspace" android:isRepeatable="true"/>

    </Row>

</Keyboard>

主要布局

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="match_parent"
        android:layout_width="match_parent">

    <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/keyboard_mac"
            android:fillViewport="true"
            android:clipChildren="false"
            android:clipToPadding="false">

        <RelativeLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/focusDisabler"
                android:paddingBottom="10dp"
                android:layout_marginTop="5dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_gravity="center_horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusableInTouchMode="true">

            <LinearLayout
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/group_wlanMac"
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:background="@drawable/bcg_button" >

                <TextView
                        android:id="@+id/tview_wlanMac"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="25dp"
                        android:layout_marginRight="20dp"
                        android:layout_marginTop="10dp"
                        android:layout_marginBottom="5dp"
                        android:textColor="#ffffff"
                        android:text="@string/txt_wlanMac"/>

                <RelativeLayout
                        android:layout_height="wrap_content"
                        android:layout_width="match_parent"
                        android:layout_marginLeft="20dp"
                        android:layout_marginRight="20dp"
                        android:layout_marginBottom="10dp">

                    <EditText
                            android:id="@+id/tedit_wlanMac"
                            android:layout_width="match_parent"
                            android:layout_height="40.0dip"
                            android:layout_centerVertical="true"
                            android:layout_toLeftOf="@+id/chbox_enableWlanMac"
                            android:maxLength="12"
                            android:nextFocusDown="@+id/tedit_wpanMac"/>

                    <CheckBox
                            android:id="@+id/chbox_enableWlanMac"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"
                            android:layout_centerVertical="true"
                            android:checked="true"/>

                </RelativeLayout>

            </LinearLayout>

            <LinearLayout
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/group_wpanMac"
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/group_wlanMac"
                    android:background="@drawable/bcg_button" >

                <TextView
                        android:id="@+id/tview_wpanMac"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="25dp"
                        android:layout_marginRight="20dp"
                        android:layout_marginTop="10dp"
                        android:layout_marginBottom="5dp"
                        android:textColor="#ffffff"
                        android:text="@string/txt_wpanMac" />

                <RelativeLayout
                        android:layout_height="wrap_content"
                        android:layout_width="match_parent"
                        android:layout_marginLeft="20dp"
                        android:layout_marginRight="20dp"
                        android:layout_marginBottom="10dp">

                    <EditText
                            android:id="@+id/tedit_wpanMac"
                            android:layout_width="match_parent"
                            android:layout_height="40dp"
                            android:layout_centerVertical="true"
                            android:layout_toLeftOf="@+id/chbox_enableWpanMac"
                            android:inputType="text"/>

                    <CheckBox
                            android:id="@+id/chbox_enableWpanMac"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"
                            android:layout_centerVertical="true"
                            android:checked="true"/>

                </RelativeLayout>

            </LinearLayout>

            <!-- SOME OTHER LAYOUTS... -->

        </RelativeLayout>

    </ScrollView>

    <android.inputmethodservice.KeyboardView
            style="@style/Keyboard"
            android:id="@+id/keyboard_mac"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_alignParentBottom="true"
            android:visibility="gone"/>

</RelativeLayout>

活动

public class _identificationChange extends _Activity
{

    EditText mEdit_wlanMac;
    EditText mEdit_wpanMac;

    MacKeyboard mKeyboardMac = null;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.identification_change);

        mEdit_wlanMac = (EditText) findViewById(R.id.tedit_wlanMac);
        mEdit_wpanMac = (EditText) findViewById(R.id.tedit_wpanMac);

        KeyboardView keyboardView = (KeyboardView) findViewById(R.id.keyboard_mac);
        Keyboard keyboard = new Keyboard(this, R.xml.keyboard_mac);
        keyboardView.setKeyboard(keyboard);
        keyboardView.setEnabled(true);
        keyboardView.setPreviewEnabled(false);

        mKeyboardMac = new MacKeyboard(keyboardView);
        keyboardView.setOnKeyboardActionListener(mKeyboardMac);

        mEdit_wlanMac.setOnFocusChangeListener(
                new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View view, boolean hasFocus) {
                        if ( hasFocus ) {
                            mKeyboardMac.show(mEdit_wlanMac);
                        } else {
                            mKeyboardMac.hide();
                        }
                    }
                }
        );

        mEdit_wlanMac.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mKeyboardMac.show(mEdit_wlanMac);
                    }
                }
        );

    }

    @Override
    public void onBackPressed() {
        if ( mKeyboardMac.isVisible() ) {
            mKeyboardMac.hide();
        } else {
            super.onBackPressed();
        }
    }
}

我将不胜感激。

4

0 回答 0