4个edittext切换焦点怎么做?

czl0325 2018-12-04 11:26:56
想做一个类似验证码输入框的控件,从左到右摆放了4个edittext,现在监听当一个edittext输入字符后,就吧焦点跳到下一个edittext,如果点击键盘删除键,把光标切换到前一个edittext,但是效果做不出来。有没有人帮我看看



public class MainActivity1 extends AppCompatActivity {
private EditText edit1;
private EditText edit2;
private EditText edit3;
private EditText edit4;
private List<EditText> edits = new ArrayList<>();

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

edit1 = findViewById(R.id.edit1);
edit2 = findViewById(R.id.edit2);
edit3 = findViewById(R.id.edit3);
edit4 = findViewById(R.id.edit4);

edit1.addTextChangedListener(textWatcher);
edit1.setOnKeyListener(onKeyListener);
edit2.addTextChangedListener(textWatcher);
edit2.setOnKeyListener(onKeyListener);
edit3.addTextChangedListener(textWatcher);
edit3.setOnKeyListener(onKeyListener);
edit4.addTextChangedListener(textWatcher);
edit4.setOnKeyListener(onKeyListener);

edits.add(edit1);
edits.add(edit2);
edits.add(edit3);
edits.add(edit4);
}

private TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {
for (int i=0; i<edits.size(); i++) {
EditText e1 = edits.get(i);
if (e1.getText().toString().length() > 0 && i < edits.size()-1) {
EditText e2 = edits.get(i+1);
e2.requestFocus();
}
}
}
};

private View.OnKeyListener onKeyListener = new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
for (int i=edits.size()-1; i>=0; i--) {
EditText e1 = edits.get(i);
if (e1.getText().toString().length() > 0 ) {
e1.setText("");
if (i > 0) {
EditText e2 = edits.get(i-1);
e2.requestFocus();
}
}
}
}
return false;
}
};
}

...全文
411 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Eiter 2018-12-06
  • 打赏
  • 举报
回复
你好像应该用edits.get(i)=findviewById();这样来找id
春华-秋实 2018-12-06
  • 打赏
  • 举报
回复
逻辑可看,就是代码写的不太好看。
YXTS122 2018-12-04
  • 打赏
  • 举报
回复
把逻辑理出来就可以写得出来了。。。。。。。。。
键盘舞者113 2018-12-04
  • 打赏
  • 举报
回复
我直接写了一个Demo,我运行了,符合你的需求


public class LauncherActivity extends AppCompatActivity {

EditText et_one,et_two,et_three,et_four;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);

et_one=(EditText)findViewById(R.id.et_one);
et_two=(EditText)findViewById(R.id.et_two);
et_three=(EditText)findViewById(R.id.et_three);
et_four=(EditText)findViewById(R.id.et_four);


et_two.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {

if(TextUtils.isEmpty(s.toString())&&et_two.isFocusable()){
et_two.setFocusable(false);
et_one.setFocusable(true);
et_one.setFocusableInTouchMode(true);
et_one.requestFocus();
}
}
});

et_three.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {

if(TextUtils.isEmpty(s.toString())&&et_three.isFocusable()){
et_three.setFocusable(false);
et_two.setFocusable(true);
et_two.setFocusableInTouchMode(true);
et_two.requestFocus();
}
}
});


et_four.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {

if(TextUtils.isEmpty(s.toString())&&et_four.isFocusable()){
et_four.setFocusable(false);
et_three.setFocusable(true);
et_three.setFocusableInTouchMode(true);
et_three.requestFocus();
}
}
});

}
}






<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".two.LauncherActivity"
android:orientation="vertical">

<EditText
android:id="@+id/et_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNone"
android:singleLine="true"/>

<EditText
android:id="@+id/et_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNone"
android:singleLine="true"/>

<EditText
android:id="@+id/et_three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNone"
android:singleLine="true"/>

<EditText
android:id="@+id/et_four"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true"/>

</LinearLayout>


80,471

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧