Android 左右箭头键充当后退和上一个按钮

Android Left and Right Arrow Keys Act as Back and Previous Buttons

我正在开发一个包含多个片段的应用程序。在一个片段下,我有一些文本可编辑字段。每次用户单击此字段、输入文本并单击向左或向右箭头键时,post 在该字段中导航,向左或向右箭头键用作导航到 [= 的后退或上一个按钮21=] 片段(next/previous 屏幕)。

我有以下 xml 字段和一些自定义

<com.UI.widget.UIEditText
            android:id="@+id/userinfo_lastnameinput"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textCursorDrawable="@null"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:ems="10"
            android:maxLength="20"
            android:textColor="@color/white"
            android:imeOptions="actionDone"
            android:singleLine="true" 
            android:inputType="textPersonName" android:paddingTop="5dp"/>

对应的Java代码为:

// Initialize the variable 
    mLastNameInput = (UIEditText) this.findViewById(R.id.userinfo_lastnameinput);
    mLastNameInput.clearFocus();
    lastNameTextChangeListener();

private void lastNameTextChangeListener() {
    mLastNameInput.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {
            if (mInitialLoad) {
                return;
            }

            mLastNameVar.setValue(mLastNameInput.getText().toString().trim());
            mLastNameInput.setIsPending(mLastNameVar.isPending());


        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                                      int arg2, int arg3) {
            // Do nothing

        }

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                  int arg3) {
            // Do nothing

        }
    });
}

如何限制这种行为,使箭头键只能在可编辑字段内移动。

您也可以像这样动态创建片段

// Create new fragment and     
transaction Fragment newFragment = new ExampleFragment();     
FragmentTransaction transaction = getFragmentManager().beginTransaction();    

// 用此片段替换 fragment_container 视图中的任何内容, // 如果需要,将事务添加到后台堆栈

transaction.replace(R.id.fragment_container, newFragment);     

transaction.addToBackStack(null); 

// 提交事务

transaction.commit();

并且只需使用 getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment, "class and/or increment"); 最好根据按钮设计。只需使用您的 XML 编码片段作为容器