Android中如何从一个EditText跳转到另一个EditText

Renvlskoca 2012-02-15 02:48:09
有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>
...全文
755 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Renvlskoca 2012-03-03
  • 打赏
  • 举报
回复
2楼辛苦了,4楼正解!感谢各位同仁给予的帮助!
shaoqianqian 2012-02-18
  • 打赏
  • 举报
回复
普及一下。
踏月 2012-02-18
  • 打赏
  • 举报
回复
擦哦,看资料学习呗
wyf35429827 2012-02-17
  • 打赏
  • 举报
回复
2楼的兄弟很热心啊,代码自己写了一遍
但人家用this 当前类实现了TextWatcher接口 性质不一样的吗
其实关键是楼主没找到这个方法requestFocus()
坏脾气的猪 2012-02-17
  • 打赏
  • 举报
回复
楼上牛x
zabaglione 2012-02-16
  • 打赏
  • 举报
回复
你写的代码有2处问题:
第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>
BuleRiver 2012-02-16
  • 打赏
  • 举报
回复
研究一下源码吧,看它是怎么实现的。

80,362

社区成员

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

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