我有三个相邻的 EditText 字段,每个字段都填充 6 个用户选择的字符。我想要的是,一旦用户在第一个 EditText 字段中键入第 6 个字符,光标就会自动转移到第二个 EditText 字段。第二个->第三个相同。我该怎么做呢?
问问题
922 次
4 回答
1
像这样使用
edittext1.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(edittext1.getText().toString().length()==6) {
edittext2.requestFocus();
}
return false;
}
});
于 2013-06-10T13:11:55.740 回答
1
只需KeyListener
对您的第一个实施 a EditText
(比方说editTextOne
)。
在按下每个键时,检查 的长度editTextOne
是否等于 6,然后通过调用requestFocus()
第二个editText
(即editTextTwo.requestFocus()
)上的方法将光标移动到第二个。
于 2013-06-10T13:01:28.630 回答
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
}
});
于 2013-06-10T13:18:23.807 回答
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>
于 2013-06-10T13:58:56.580 回答