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
- 在 appComponent dagger 2 中动态添加测试模块? 2022-01-01
- Android:STATE_SELECTED不工作 2022-01-01
- 如何将多个组件添加到 PickerView? 2022-01-01
- 突出显示朗读文本(在 iPhone 的故事书类型应用程序中) 2022-01-01
- 在 iOS8 中无法获得正确的键盘高度值 2022-01-01
- 可可/目标C(OSX不是iOS)从子对象访问父对象 2022-01-01
- 新的@SystemApi 注解是什么意思,和@hide 有什么区别 2022-01-01
- 我的 UIImageView 的任务 2022-01-01
- - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view 如何工作 2022-01-01
