EditText 最小长度 &启动新活动 [英] EditText Minimum Length & Launch New Activity

查看:19
本文介绍了EditText 最小长度 &启动新活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Android 中的 EditText 函数有几个疑问.

I have a couple of queries regarding the EditText function in Android.

首先,是否可以在 EditText 字段中设置最小字符数?我知道有一个

First of all, is it possible to set a minimum number of characters in the EditText field? I'm aware that there is an

android:maxLength="*"

但是由于某种原因你不能拥有

however for some reason you can't have

android:minLength="*"

另外,我想知道是否可以在按下键盘上的 Enter 键后启动一个新活动,当我们在 EditText 字段中输入数据时弹出我们?如果是这样,有人可以告诉我怎么做吗?

Also, I was wondering if it is possible to launch a new activity after pressing the enter key on the keyboard that pops us when inputing data into the EditText field? And if so, could someone show me how?

感谢您就这两个问题提供的任何帮助:)

Thanks for any help you could offer regarding either question :)

推荐答案

响应编辑字段中的回车键并在用户输入的文本不足时通知用户:

To respond to an enter key in your edit field and notify the user if they haven't entered enough text:

EditText myEdit = (EditText) findViewById(R.id.myedittext);
    myEdit.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                if (myEdit.getText().length() < minLength) {
                    Toast.makeText(CurrentActivity.this, "Not enough characters", Toast.LENGTH_SHORT);
                } else {
                    startActivity(new Intent(CurrentActivity.this, ActivityToLaunch.class);
                }
                return true;
            }
            return false;
        }
    });

在编辑字段时,没有简单的方法来强制设置最小长度.您将检查输入的每个字符的长度,然后在用户尝试删除超过最小值时抛出击键.这很混乱,这就是为什么没有内置的方法来做到这一点.

There's no simple way to force a minimum length as the field is edited. You'd check the length on every character entered and then throw out keystrokes when the user attempt to delete past the minimum. It's pretty messy which is why there's no built-in way to do it.

这篇关于EditText 最小长度 &amp;启动新活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆