有3个EditText,在第一个输入长度到2个后自动聚焦在第二个EditText,第二个输入长度达2个后自动跳转到第3个EditText。
可以理解为在软键盘连续输入6个数字正好填满3个控件。每个EditText长度我已经限定为2。
为了方便有心人研究,我写了简单的测试代码。这个研究了很久,跪求仁兄驻足!
package hyz.com.test;import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
public class TestActivity extends Activity implements TextWatcher
{
private EditText edt_hour;
private EditText edt_minute;
private EditText edt_second;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.countdown);
edt_hour = (EditText) findViewById(R.id.hour);
edt_hour.addTextChangedListener(this);
edt_minute = (EditText) findViewById(R.id.minute);
edt_minute.addTextChangedListener(this);
edt_second = (EditText) findViewById(R.id.second);
edt_second.addTextChangedListener(this);
}
@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(s.toString().length() == 2)
{
if(edt_hour.isFocused())
{
edt_hour.clearFocus();
}
else
if(edt_minute.isFocused())
{
edt_minute.clearFocus();
}
else
if(edt_second.isFocused())
{
edt_second.clearFocus();
}
}
}
}count.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:id="@+id/hour"
android:layout_width="50dip"
android:layout_height="50dip"
android:text="00"
android:maxLength="2"
android:inputType="number"
android:selectAllOnFocus="true"/>
<EditText
android:id="@+id/minute"
android:layout_width="50dip"
android:layout_height="50dip"
android:text="00"
android:maxLength="2"
android:inputType="number"
android:selectAllOnFocus="true"/>
<EditText
android:id="@+id/second"
android:layout_width="50dip"
android:layout_height="50dip"
android:text="00"
android:maxLength="2"
android:inputType="number"
android:selectAllOnFocus="true"/>
</LinearLayout>
可以理解为在软键盘连续输入6个数字正好填满3个控件。每个EditText长度我已经限定为2。
为了方便有心人研究,我写了简单的测试代码。这个研究了很久,跪求仁兄驻足!
package hyz.com.test;import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
public class TestActivity extends Activity implements TextWatcher
{
private EditText edt_hour;
private EditText edt_minute;
private EditText edt_second;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.countdown);
edt_hour = (EditText) findViewById(R.id.hour);
edt_hour.addTextChangedListener(this);
edt_minute = (EditText) findViewById(R.id.minute);
edt_minute.addTextChangedListener(this);
edt_second = (EditText) findViewById(R.id.second);
edt_second.addTextChangedListener(this);
}
@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(s.toString().length() == 2)
{
if(edt_hour.isFocused())
{
edt_hour.clearFocus();
}
else
if(edt_minute.isFocused())
{
edt_minute.clearFocus();
}
else
if(edt_second.isFocused())
{
edt_second.clearFocus();
}
}
}
}count.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:id="@+id/hour"
android:layout_width="50dip"
android:layout_height="50dip"
android:text="00"
android:maxLength="2"
android:inputType="number"
android:selectAllOnFocus="true"/>
<EditText
android:id="@+id/minute"
android:layout_width="50dip"
android:layout_height="50dip"
android:text="00"
android:maxLength="2"
android:inputType="number"
android:selectAllOnFocus="true"/>
<EditText
android:id="@+id/second"
android:layout_width="50dip"
android:layout_height="50dip"
android:text="00"
android:maxLength="2"
android:inputType="number"
android:selectAllOnFocus="true"/>
</LinearLayout>
解决方案 »
- 【高分求讨论】3D控件开发
- 【求教】:新浪微博开发获取授权后如何保存,accessToken?
- 用什么方法能够结束一个应用程序?
- ListView疑难
- 求教Android JNIEnv 多线程的问题
- 关于AutoCompleteTextView使用数据库的问题
- 新人自学中
- 大家有人在做百度地图的开发吗?谁能帮我看看PopupOverlay这里的showPopup怎么了,核心代码和异常已贴上,感激不尽
- cordova platform add android error
- Android studio 模拟器屏幕很小
- Android 如何逐渐显示一张圆形图片?
- 我想问下各位,我写C语言程序如果内存什么的没控制好,我会不会破坏系统?
第1个问题
edt_hour.addTextChangedListener(this);
这里不应该指定this,应该指定到textwatcher的实例第2个问题
if(edt_hour.isFocused())
{
edt_hour.clearFocus();
}
把hour的焦点清除之后,你要用requestFocus来指定后面的输入框的焦点以下是我修改后的代码,可以运行的import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import java.lang.CharSequence;public class ActivityMain extends Activity{
private EditText edt_hour;
private EditText edt_minute;
private EditText edt_second;
TextWatcher tw = 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(s.toString().length() == 2)
{
if(edt_hour.isFocused())
{
edt_hour.clearFocus();
edt_minute.requestFocus();
}
else if(edt_minute.isFocused())
{
edt_minute.clearFocus();
edt_second.requestFocus();
}
else if(edt_second.isFocused())
{
edt_second.clearFocus();
}
}
}
};
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edt_hour = (EditText) findViewById(R.id.hour);
edt_hour.addTextChangedListener(tw);
edt_minute = (EditText) findViewById(R.id.minute);
edt_minute.addTextChangedListener(tw);
edt_second = (EditText) findViewById(R.id.second);
edt_second.addTextChangedListener(tw);
}
}<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
<EditText
android:id="@+id/hour"
android:text="00"
android:maxLength="2"
android:inputType="number"
android:selectAllOnFocus="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/minute"
android:text="00"
android:maxLength="2"
android:inputType="number"
android:selectAllOnFocus="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<EditText android:maxLength="2"
android:inputType="number"
android:text="00"
android:id="@+id/second"
android:selectAllOnFocus="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
但人家用this 当前类实现了TextWatcher接口 性质不一样的吗
其实关键是楼主没找到这个方法requestFocus()