Move to another EditText when Soft Keyboard Next i

2019-01-03 04:04发布

When I press the 'Next', the focus on the User EditText must be move to the Password. Then, from Password, it must move to the right and so on. Can you help me on how to code it?

enter image description here

<LinearLayout
    android:id="@+id/LinearLayout01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="User Name*" />

    <EditText
        android:id="@+id/txt_User"
        android:layout_width="290dp"
        android:layout_height="33dp"
        android:singleLine="true" />

</LinearLayout>


<LinearLayout
    android:id="@+id/LinearLayout02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Password*" />

    <EditText
        android:id="@+id/txt_Password"
        android:layout_width="290dp"
        android:layout_height="33dp"
        android:singleLine="true"
        android:password="true" />

    <TextView
        android:id="@+id/confirm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Password*" />

    <EditText
        android:id="@+id/txt_Confirm"
        android:layout_width="290dp"
        android:layout_height="33dp"
        android:singleLine="true"
        android:password="true" />

</LinearLayout>

13条回答
Deceive 欺骗
2楼-- · 2019-01-03 04:35

In your onEditorAction handler, keep in mind that you must return a boolean that indicates if you are handling the action (true) or if you applied some logic and want the normal behaviour (false), as in the following example:

EditText te = ...
te.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event){
        if (actionId == EditorInfo.IME_ACTION_NEXT) {
            // Some logic here.
            return true; // Focus will do whatever you put in the logic.
        }
        return false;  // Focus will change according to the actionId
    }
});

I found this when I returned true after performing my logic since focus did not move.

查看更多
家丑人穷心不美
3楼-- · 2019-01-03 04:36

Focus Handling

Focus movement is based on an algorithm which finds the nearest neighbor in a given direction. In rare cases, the default algorithm may not match the intended behavior of the developer.

Change default behaviour of directional navigation by using following XML attributes:

android:nextFocusDown="@+id/.."  
android:nextFocusLeft="@+id/.."    
android:nextFocusRight="@+id/.."    
android:nextFocusUp="@+id/.."  

Besides directional navigation you can use tab navigation. For this you need to use

android:nextFocusForward="@+id/.."

To get a particular view to take focus, call

view.requestFocus()

To listen to certain changing focus events use a View.OnFocusChangeListener


Keyboard button

You can use android:imeOptions for handling that extra button on your keyboard.

Additional features you can enable in an IME associated with an editor to improve the integration with your application. The constants here correspond to those defined by imeOptions.

The constants of imeOptions includes a variety of actions and flags, see the link above for their values.

Value example

ActionNext :

the action key performs a "next" operation, taking the user to the next field that will accept text.

ActionDone :

the action key performs a "done" operation, typically meaning there is nothing more to input and the IME will be closed.

Code example:

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

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="16dp"
        android:imeOptions="actionNext"
        android:maxLines="1"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="24dp"
        android:imeOptions="actionDone"
        android:maxLines="1"
        android:ems="10" />

</RelativeLayout>

If you want to listen to imeoptions events use a TextView.OnEditorActionListener.

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            performSearch();
            return true;
        }
        return false;
    }
});

查看更多
Fickle 薄情
4楼-- · 2019-01-03 04:36

Use the following line

android:nextFocusDown="@+id/parentedit"

parentedit is the ID of the next EditText to be focused.

The above line will also need the following line.

android:inputType="text"

or

android:inputType="number"

Thanks for the suggestion @Alexei Khlebnikov.

查看更多
ゆ 、 Hurt°
5楼-- · 2019-01-03 04:39

just use the following code it will work fine and use inputType for every edittext and the next button will appear in keyboard.

android:inputType="text" or android:inputType="number" etc
查看更多
小情绪 Triste *
6楼-- · 2019-01-03 04:40
android:inputType="text"

should bring the same effect. After hiting next to bring the focus to the next element.

android:nextFocusDown="@+id/.."

use this in addition if you dont want the next view to get the focus

查看更多
兄弟一词,经得起流年.
7楼-- · 2019-01-03 04:40
<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="vertical" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="666dp"
android:background="#1500FFe5"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<TextView
    android:id="@+id/TextView02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editGrWt"
    android:layout_marginTop="14dp"
    android:layout_toLeftOf="@+id/textView3"
    android:ems="6"
    android:text="    Diamond :"
    android:textColor="@color/background_material_dark"
    android:textSize="15sp" />
  <EditText
    android:id="@+id/editDWt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/TextView02"
    android:layout_alignLeft="@+id/editGrWt"
    android:background="@color/bright_foreground_inverse_material_light"
    android:ems="4"
    android:hint="Weight"
    android:inputType="numberDecimal"
    android:nextFocusLeft="@+id/editDRate"
    android:selectAllOnFocus="true"
    android:imeOptions="actionNext"

    />
 <requestFocus />


<TextView
    android:id="@+id/TextView03"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/TextView02"
    android:layout_below="@+id/TextView02"
    android:layout_marginTop="14dp"
    android:ems="6"
    android:text="    Diamond :"
    android:textColor="@color/background_material_dark"
    android:textSize="15sp" />

<EditText
    android:id="@+id/editDWt1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/TextView03"
    android:layout_alignBottom="@+id/TextView03"
    android:layout_alignLeft="@+id/editDWt"
    android:background="@color/bright_foreground_inverse_material_light"
    android:ems="4"
    android:hint="Weight"
    android:inputType="numberDecimal"
    android:text="0"
    android:selectAllOnFocus="true"
    android:imeOptions="actionNext"/>
 <requestFocus />

<TextView
    android:id="@+id/TextView04"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editDWt1"
    android:layout_marginTop="14dp"
    android:layout_toLeftOf="@+id/textView3"
    android:ems="6"
    android:text="         Stone :"
    android:textColor="@color/background_material_dark"
    android:textSize="15sp" />

<EditText
    android:id="@+id/editStWt1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/TextView04"
    android:layout_alignBottom="@+id/TextView04"
    android:layout_alignLeft="@+id/editDWt1"
    android:background="@color/bright_foreground_inverse_material_light"
    android:ems="4"
    android:hint="Weight"
    android:inputType="numberDecimal"
    android:nextFocusForward="@+id/editStRate1"
    android:imeOptions="actionNext" />
 <requestFocus />
  <TextView
     android:id="@+id/TextView05"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignLeft="@+id/TextView04"
     android:layout_below="@+id/editStRate1"
     android:layout_marginTop="14dp"
     android:ems="6"
     android:text="         Stone :"
     android:textColor="@color/background_material_dark"
     android:textSize="15sp" />


</RelativeLayout>

</ScrollView>
查看更多
登录 后发表回答