问题描述
我对 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 最小长度 &启动新活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)