How to change the focus to next edit text in android?(如何将焦点更改为android中的下一个编辑文本?)
问题描述
用户只能在编辑文本中输入一位数字.如果他在 edtText1 中输入值,我希望光标自动移动到 edtText2 等等.用户可以编辑他/她已经输入的文本.我尝试了以下方式.
The User can enter only one digit in the edit text. if he enters the value in edtText1, I want the cursor automatically moves to edtText2 and so on. The user can able to edit the text which he/she has entered already. I tried the following way.
edtPasscode1.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (edtPasscode1.getText().length() == 1)
edtPasscode2.requestFocus();
return false;
}
});
edtPasscode2.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (edtPasscode2.getText().length() == 1)
edtPasscode3.requestFocus();
return false;
}
});
edtPasscode3.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (edtPasscode3.getText().length() == 1)
edtPasscode4.requestFocus();
return false;
}
});
如果用户编辑文本,光标会移动到其他一些editTexts并且无法按预期工作.我怎样才能实现上述目标?
If the user edit the text, The cursor moves to some other editTexts and not working as desired. How can i achieve the above?
推荐答案
试试 TextWatcher 代替 onKeyListener
如果你想修改你的密码,那么TextWatcher会给你更多的处理方法..
B'coz if want to edit your password, in that case TextWatcher will give you more method to dealt with..
已-
StringBuilder sb=new StringBuilder();
edtPasscode1.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
if(sb.length()==0&edtPasscode1.length()==1)
{
sb.append(s);
edtPasscode1.clearFocus();
edtPasscode2.requestFocus();
edtPasscode2.setCursorVisible(true);
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
if(sb.length()==1)
{
sb.deleteCharAt(0);
}
}
public void afterTextChanged(Editable s) {
if(sb.length()==0)
{
edtPasscode1.requestFocus();
}
}
});
希望这项工作.
这篇关于如何将焦点更改为android中的下一个编辑文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将焦点更改为android中的下一个编辑文本?
基础教程推荐
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01