简体   繁体   English

在EditText中输入数字后,下一个按钮键盘会自动将用户带到另一个EditText

[英]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. 我正在使用Genymotion模拟器。

Here is the part of the screen I'm having issues with: 这是我遇到问题的屏幕部分: 在此处输入图片说明

When I click on an EditText number field, the numberpad comes up 当我单击EditText数字字段时,数字键盘出现 在此处输入图片说明

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. 另外,这很奇怪,因为如果我在两个底部的EditText中输入数字,它们将立即结束,而如果我在顶部的EditText中输入一个数字,它们会将我带到底部的EditText之一。

Here's the XML file. 这是XML文件。

    <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. 如果要跳过下一个字段,则可以使用nextFocusDown属性在editText自身中提及ID。

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. 当您想要在SoftKeyboard上的特定视图上显示完成时,请使用android:imeOptions =“ actionDone”属性。

Set ime options to your EditText: 将ime选项设置为您的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 请参阅文档: 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--

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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