【发布时间】:2011-07-25 18:40:45
【问题描述】:
这是为我的 Android 应用程序。我有两个 EditText 字段设置为每个字段中最多可以输入一个字符。一旦用户输入第一个字符,我希望焦点自动跳转到第二个 EditText 字段。我在第一个编辑文本上设置了一个 addTextChangedListener 并让它在文本字符串大于 1 时进行侦听。然后我将请求焦点调用到第二个编辑文本上。但是,当我在第一个编辑文本框中输入字符时,我一直收到强制关闭。从我在 StackOverflow 上看到的内容来看,这应该可行。有人知道为什么不是吗?我在下面发布了相关代码。谢谢。
LinearLayout llview = new LinearLayout(this);
llview.setOrientation(LinearLayout.VERTICAL);
EditText character1 = new EditText(this);
EditText character2 = new EditText(this);
character2.setId(2);
character1.addTextChangedListener(new TextWatcher(){
EditText character2 = (EditText) findViewById(2);
@Override
public void afterTextChanged(Editable s) {
if(s.length()>0)
{
character2.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) {}
});
llview.addView(character1);
llview.addView(character2);
this.setContentView(llview);
【问题讨论】:
-
堆栈跟踪说明了什么?
标签: java android focus android-edittext