简体   繁体   中英

After inputting number in EditText, next button keyboard takes user to a different EditText automatically

Making a simple calorie converter.

Here's the problem in pictures.

I'm on a Genymotion emulator.

Here is the part of the screen I'm having issues with: 在此处输入图片说明

When I click on an EditText number field, the numberpad comes up 在此处输入图片说明

But there's a next arrow I must click which takes me to the next input field 在此处输入图片说明

Before I can finally just get out of the numberpad.

What's going on? After I type in a number into one edit text I want it to end right there, not jump to the next Edit Text. Also, it's weird because if I enter a number in the two bottom EditTexts they end right away whereas if I enter a number in the top EditTexts they take me to one of the bottom EditTexts.

Here's the XML file.

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/editText2"
        android:layout_below="@+id/imageView2"
        android:layout_alignEnd="@+id/imageView2"
        android:layout_alignStart="@+id/imageView2" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/editText3"
        android:layout_alignBottom="@+id/editText2"
        android:layout_alignEnd="@+id/imageView4"
        android:layout_alignStart="@+id/imageView4" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/editText4"
        android:layout_marginBottom="32dp"
        android:layout_alignParentBottom="true"
        android:layout_alignStart="@+id/imageView5"
        android:layout_alignEnd="@+id/imageView5" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/editText5"
        android:layout_alignTop="@+id/editText4"
        android:layout_alignStart="@+id/imageView3"
        android:layout_toStartOf="@+id/editText2" />

Thank you!!!

If you want to jump next field you mentioned the id in editText itself using nextFocusDown attribute.

try this....

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:ems="10"
            android:id="@+id/editText2"
            android:layout_below="@+id/imageView2"
            android:layout_alignEnd="@+id/imageView2"
            android:layout_alignStart="@+id/imageView2" 
            android:nextFocusDown="@+id/editText3"/>

when you want to show done on SoftKeyboard at particular view use android:imeOptions="actionDone" attribute.

Set ime options to your EditText:

 <EditText
    android:imeOptions="actionDone"
    ... />

You can choose from various options. See the documentation: http://developer.android.com/reference/android/widget/TextView.html#attr_android:imeOptions

The only way to truly handle what happens when the next (enter) key is pressed is to make a listener for it at tell it what to do according to the current state of the application.

final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        // If the event is a key-down event on the "enter" button
        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
            (keyCode == KeyEvent.KEYCODE_ENTER)) {
          // Perform action on key press
          return true;
        }
        return false;
    }
});

To select a edittext you need to focus it like this:

editText.requestFocus();

Simple solution would be to add:

android:imeOptions="actionNext"
android:nextFocusDown="@+id/..." <!--Obviously with the next edittext ID--

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM