Android KeyCode_Enter 到下一个编辑文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3019965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:20:13  来源:igfitidea点击:

KeyCode_Enter to next edittext

androidandroid-edittextsetfocus

提问by soclose

In edittext, after typing 'Enter' key, system make a new line inside it. I'd like to focus on next edittext, no new line. how to code? my code in xml is below

在edittext 中,输入'Enter' 键后,系统在其中创建一个新行。我想专注于下一个编辑文本,没有新行。如何编码?我在 xml 中的代码如下

    <EditText
    android:id="@+id/txtNPCode"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:layout_alignLeft="@+id/lblNPCode"
    android:layout_below="@+id/lblNPCode"
    android:layout_centerHorizontal="true"/>
<EditText
    android:id="@+id/txtCNPCode"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:layout_alignLeft="@+id/lblCNPCode"
    android:layout_below="@+id/lblCNPCode"
    android:layout_centerHorizontal="true"/>  

I also caputer key code in setOnKeyListener

我还在 setOnKeyListener 中捕获关键代码

   tCNPCode.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            if(keyCode == 66) {
                Toast.makeText(S_PCode.this, "Enter Key", Toast.LENGTH_LONG).show();
                //tNPCode.setFocusable(true);
            }
            return false;
        }
    });

采纳答案by Cristian

You have to use the requestFocusmethod. In your case it will be something like:

您必须使用该requestFocus方法。在您的情况下,它将类似于:

tCNPCode.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if(keyCode == 66) {
            txtCNPCode.requestFocus();
        }
        return false;
    }
});

回答by Antek

You don't even have to use OnKeyListener. Simply add line android:singleLine="true"to your EditText. After this operation EditTextbehaves exactly like you want. So, in your particular case:

您甚至不必使用OnKeyListener. 只需将行添加android:singleLine="true"到您的EditText. 此操作后的EditText行为完全符合您的要求。因此,在您的特定情况下:

<EditText
    android:id="@+id/txtNPCode"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:layout_alignLeft="@+id/lblNPCode"
    android:layout_below="@+id/lblNPCode"
    android:layout_centerHorizontal="true"
    android:singleLine="true"
/>
<EditText
    android:id="@+id/txtCNPCode"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:layout_alignLeft="@+id/lblCNPCode"
    android:layout_below="@+id/lblCNPCode"
    android:layout_centerHorizontal="true"
/>

solves the problem.

解决了这个问题。

回答by Stan

The best way is to use one of the options for inputType, for example: android:inputType="textPersonName"This will achieve the wanted effect. Check out the other options too, it'll save you some time in future :)

最好的方法是使用 inputType 的选项之一,例如:android:inputType="textPersonName"这将达到想要的效果。也请查看其他选项,它会在将来为您节省一些时间:)

You shouldn't use android:singleLine="true", since it's deprecated. Here's what the documentations says about it:

您不应该使用 android:singleLine="true",因为它已被弃用。以下是文档对此的说明:

This attribute is deprecated and is replaced by the textMultiLine flag in the inputType attribute. Use caution when altering existing layouts, as the default value of singeLine is false (multi-line mode), but if you specify any value for inputType, the default is single-line mode. (If both singleLine and inputType attributes are found, the inputType flags will override the value of singleLine.).

此属性已弃用,并由 inputType 属性中的 textMultiLine 标志替换。更改现有布局时要小心,因为 singeLine 的默认值为 false(多行模式),但如果为 inputType 指定任何值,则默认值为单行模式。(如果找到 singleLine 和 inputType 属性,则 inputType 标志将覆盖 singleLine 的值。)。

回答by Aliaksei Plashchanski

Consider IME options, good description here http://androidcookbook.com/Recipe.seam;jsessionid=C824437B48F346E23FBE5828969029B4?recipeId=422and documentation here http://developer.android.com/reference/android/widget/TextView.html#attr_android:imeOptions.

考虑 IME 选项,这里有很好的描述http://androidcookbook.com/Recipe.seam;jsessionid=C824437B48F346E23FBE5828969029B4?recipeId=422和文档在这里http://developer.android.com/reference/android/widget/TextViewandroid.html#attr_ :imeOptions

The only thing you need is add attribute android:imeOptions="actionNext" on each EditText. Keyboard appears with a Next key where Enter used to be and allows to navigate to the next EditText.

您唯一需要的是在每个 EditText 上添加属性 android:imeOptions="actionNext"。键盘与 Next 键一起出现在 Enter 曾经所在的位置,并允许导航到下一个 EditText。

But order of "actionNext" is from top to bottom. For example, if you have horizontal layout:

但是“actionNext”的顺序是从上到下。例如,如果您有水平布局:

<LinearLayout android:orientation="horizontal" >
        <EditText android:id="@+id/et1" android:imeOptions="actionNext" />
        <EditText android:id="@+id/et2" android:imeOptions="actionNext" />
</LinearLayout>

et2 never get focus. You should fix it in the code:

et2 永远不会得到焦点。您应该在代码中修复它:

et1.setOnEditorActionListener(new OnEditorActionListener()
{
    @Override
    public boolean onEditorAction(TextView v, int actionId,
            KeyEvent event)
    {
        switch (actionId)
        {
        case EditorInfo.IME_ACTION_NEXT:
            boolean result = false;
            TextView v1 = (TextView) v.focusSearch(View.FOCUS_RIGHT);
            if (v1 != null)
                result = v1.requestFocus(View.FOCUS_RIGHT);
            else if (!result)
            {
                v1 = (TextView) v.focusSearch(View.FOCUS_DOWN);
                if (v1 != null)
                    result = v1.requestFocus(View.FOCUS_DOWN);
            }
            if (!result)
                v.onEditorAction(actionId);
            break;

        default:
            v.onEditorAction(actionId);
            break;
        }
        return true;
    }
});

Original link: https://groups.google.com/forum/?fromgroups=#!topic/android-developers/OykOG6XtE8w

原文链接:https: //groups.google.com/forum/?fromgroups=#!topic/android-developers/ OykOG6XtE8w

回答by user3811671

Just set the maximum number of line to the EditTextbox in xml layout file: android:maxLength="1"

只需将最大行数设置为EditTextxml 布局文件中的框: android:maxLength="1"

回答by Mohsin kazi

Use Single Line of Code

使用单行代码

tCNPCode.setSingleLine(true);

It will navigate to next item on enter press

按下回车键,它将导航到下一个项目