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 最小长度 &启动新活动


- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01