EditText Minimum Length amp; Launch New Activity(EditText 最小长度 amp;启动新活动)
问题描述
我对 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 最小长度 &启动新活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:EditText 最小长度 &启动新活动
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01