我有三个相邻的EditText字段,每个字段都填充了6个用户选择的字符。我想要的是,只要用户在第一个EditText字段中键入第6个字符,光标就会自动切换到第二个EditText字段。第二个相同 - >第三。我怎么能这样做呢?
答案 0 :(得分:1)
只需为您的第一个EditText
实施KeyListener
(让我们说editTextOne
)。
在按下的每个键上,检查editTextOne
的长度是否等于6,并通过调用第二个editText
上的requestFocus()
方法将光标移动到第二个键。即editTextTwo.requestFocus()
)。
答案 1 :(得分:1)
像那样使用
edittext1.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(edittext1.getText().toString().length()==6) {
edittext2.requestFocus();
}
return false;
}
});
答案 2 :(得分:0)
试试这个。
mEtText.addTextChangedListener(new TextWatcher(){
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
if(mEtText.getText().length() == 6){
secondEditText.requestFocus();
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
答案 3 :(得分:0)
在这种情况下,有必要使用适当的监听器: TextWatcher
与以下代码互动:
package com.example.stackoverflow;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.Menu;
import android.widget.EditText;
import android.widget.Toast;
public class SimpleActivity10 extends Activity {
private final short MAXCHAR = 6;
private EditText editText1, editText2, editText3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity10);
editText1 = (EditText) this.findViewById(R.id.editText1);
editText2 = (EditText) this.findViewById(R.id.editText2);
editText3 = (EditText) this.findViewById(R.id.editText3);
editText1.addTextChangedListener(textWatcher1);
editText2.addTextChangedListener(textWatcher2);
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(MAXCHAR);
editText1.setFilters(FilterArray);
editText2.setFilters(FilterArray);
editText3.setFilters(FilterArray);
}
private TextWatcher textWatcher1 = new TextWatcher() {
@Override
public void afterTextChanged(Editable editable) {
int messageLength = editable.toString().length();
if (messageLength == MAXCHAR) {
Toast.makeText(getApplicationContext(), "EditText 2 get focus!", Toast.LENGTH_SHORT).show();
editText2.requestFocus();
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
};
private TextWatcher textWatcher2 = new TextWatcher() {
@Override
public void afterTextChanged(Editable editable) {
int messageLength = editable.toString().length();
if (messageLength == MAXCHAR) {
Toast.makeText(getApplicationContext(), "EditText 3 get focus!", Toast.LENGTH_SHORT).show();
editText3.requestFocus();
}
}
@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 boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.simple_activity10, menu);
return true;
}
}
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SimpleActivity10" >
<EditText
android:id="@+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" />
<EditText
android:id="@+id/editText3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" />
</LinearLayout>