我有5个可动态添加的editTexts片段:
List<TextView> ArrayQuestionsEditTexts =new ArrayList<TextView>();
for(int i=0; i<5; i++){
View to_add = inflater.inflate(R.layout.list_item_add_new_offer,
options_layout,false);
EditText question = (EditText)
to_add.findViewById(R.id.editTextQuestion);
ArrayQuestionsEditTexts.add(question);
options_layout.addView(to_add);
}
我希望当用户单击键盘上的next按钮将焦点传递到ArrayQuestionsEditTexts列表中的下一个edittext时。
我尝试:
for (int i = 0; i < ArrayQuestionsEditTexts.size()-1; i++) {
final int nextItem = i+1;
ArrayQuestionsEditTexts.get(i).setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_NEXT)) {
ArrayQuestionsEditTexts.get(nextItem).requestFocus();
}
return false;
}
});
}
但是此解决方案无效。
答案 0 :(得分:0)
尝试:
List<TextView> ArrayQuestionsEditTexts = new ArrayList<TextView>();
for(int i=0; i<5; i++){
View to_add = inflater.inflate(R.layout.list_item_add_new_offer, options_layout,false);
EditText question = (EditText) to_add.findViewById(R.id.editTextQuestion);
question.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_NEXT)) {
ArrayQuestionsEditTexts.get(nextItem).requestFocus();
}
return false;
}
});
ArrayQuestionsEditTexts.add(question);
options_layout.addView(to_add);
}
或者,换句话说,当您执行第一个循环来增加布局时,为什么不还将侦听器附加到该循环中呢?而不是第二次循环添加侦听器?
答案 1 :(得分:0)
根据官方doc:
您需要在视图上setTag
(此处是编辑文本),然后需要使用findViewWithTag
找到该视图,之后您需要通过requestFocus
专注于编辑文本