KeyCode_Enter 到下一个编辑文本

发布于 2024-09-05 09:04:55 字数 1137 浏览 4 评论 0原文

在编辑文本中,输入“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"/>  

我还在 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;
        }
    });

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
    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

   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;
        }
    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

ま柒月 2024-09-12 09:04:55

您甚至不必使用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"
/>

解决问题。

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

<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.

债姬 2024-09-12 09:04:55

您必须使用 requestFocus 方法。在你的情况下,它将是这样的:

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

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

tCNPCode.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_ENTER) {
            txtCNPCode.requestFocus();
        }
        return false;
    }
});
晌融 2024-09-12 09:04:55

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

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

此属性已弃用,并由 textMultiLine 取代
inputType 属性中的标志。更改现有的时请小心
布局,因为 singleLine 的默认值为 false(多行
mode),但如果为 inputType 指定任何值,则默认为
单线模式。 (如果 singleLine 和 inputType 属性都是
发现,inputType 标志将覆盖 singleLine 的值。)。

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 :)

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

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.).

冷情妓 2024-09-12 09:04:55

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

您唯一需要的就是在每个 EditText 上添加属性 android:imeOptions="actionNext" 。键盘上出现一个 Next 键(以前是 Enter 键),允许导航到下一个 EditText。

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

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

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;
    }
});

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

Consider IME options, good description here http://androidcookbook.com/Recipe.seam;jsessionid=C824437B48F346E23FBE5828969029B4?recipeId=422 and documentation here http://developer.android.com/reference/android/widget/TextView.html#attr_android: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.

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

<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:

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

·深蓝 2024-09-12 09:04:55

使用单行代码 (Java)

tCNPCode.setSingleLine(true);

科特林

tCNPCode.isSingleLine = true

按 Enter 键它将导航到下一个项目

Use Single Line of Code (Java)

tCNPCode.setSingleLine(true);

Kotlin

tCNPCode.isSingleLine = true

It will navigate to the next item on Enter press

意中人 2024-09-12 09:04:55

只需在 xml 布局文件中的 EditText 框中设置最大行数即可:
android:maxLength="1"

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文