Android ListView 中的 EditText 不会保持专注
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12730610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EditText inside ListView will not stay focused
提问by Matt
I have an EditText
inside each row of a ListView
. For some reason, when I tap the EditText, it briefly gains focus but then immediately loses it.
我在 a 的EditText
每一行里面都有一个ListView
。出于某种原因,当我点击 EditText 时,它会短暂获得焦点,但随后立即失去焦点。
I have tried these things:
我试过这些东西:
listView.setItemsCanFocus(true);
editText.setFocusable(true);
While the EditText
is (briefly) focused, the Enter
key says Next
and the auto-correct bar
is present. When it loses focus, the soft keyboard stays up, but the Enter
key becomes a Return Arrow
, and the auto-correct bar
disappears.
虽然EditText
(简短地)聚焦,但Enter
关键说Next
并且auto-correct bar
存在。当它失去焦点时,软键盘会保持向上,但Enter
键变为Return Arrow
,然后auto-correct bar
消失。
采纳答案by dmon
EditText
s within ListView
s are extremely tricky. I'd suggest you avoid them like the plague if possible. When I was messing with them, this answerwas helpful though. If you only have a few items you can always just use a ScrollView
.
EditText
s 中的ListView
s 非常棘手。如果可能的话,我建议你像躲避瘟疫一样避开它们。当我和他们混在一起时,这个答案很有帮助。如果您只有几个项目,您可以随时使用ScrollView
.
回答by Aswin Kumar
If you want list items to take focus, its pretty straight forward:
Make the listview not focusable, and say that its focus is afterDescendants
which lets EditText
s and other focusable items take focus.
如果您希望列表项获得焦点,则非常简单:使列表视图不可聚焦,并说它的焦点是afterDescendants
让EditText
s 和其他可聚焦项获得焦点。
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:descendantFocusability="afterDescendants"
android:focusable="false" />
回答by Joubert Vasconcelos
I know this question is old, but I was also fighting with EditText and ListView today. I think lose time with focus problem or updating dataset is the end of the line.
我知道这个问题很老,但我今天也在与 EditText 和 ListView 作斗争。我认为在焦点问题上浪费时间或更新数据集是行的结束。
So, I created an Activity and dynamically added the controls I need. In this example I added two controls: a textview and edittext.
因此,我创建了一个 Activity 并动态添加了我需要的控件。在这个例子中,我添加了两个控件:textview 和 edittext。
First I create a class to be a "container" of the controls and them I put the controls on a ScrollView.
首先,我创建一个类作为控件的“容器”,然后将控件放在 ScrollView 上。
The layout.xml file:
layout.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ScrollView
android:id="@+id/svMain"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/llForm"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</LinearLayout>
The "containter" class:
“容器”类:
import android.widget.EditText;
public class RespostasArray {
private int pergunta;
private int formulario;
private int form_coord;
private int coordenada;
private int form_resposta;
private String questao;
private String resposta;
private EditText respostaEdit;
public RespostasArray() {
respostaEdit = null;
}
public int getPergunta() {
return pergunta;
}
public void setPergunta(int pergunta) {
this.pergunta = pergunta;
}
public int getFormulario() {
return formulario;
}
public void setFormulario(int formulario) {
this.formulario = formulario;
}
public int getForm_coord() {
return form_coord;
}
public void setForm_coord(int form_coord) {
this.form_coord = form_coord;
}
public int getCoordenada() {
return coordenada;
}
public void setCoordenada(int coordenada) {
this.coordenada = coordenada;
}
public int getForm_resposta() {
return form_resposta;
}
public void setForm_resposta(int form_resposta) {
this.form_resposta = form_resposta;
}
public String getQuestao() {
return questao;
}
public void setQuestao(String questao) {
this.questao = questao;
}
public String getResposta() {
return resposta;
}
public void setResposta(String resposta) {
this.resposta = resposta;
}
public EditText getRespostaEdit() {
return respostaEdit;
}
public void setRespostaEdit(EditText respostaEdit) {
this.respostaEdit = respostaEdit;
}
}
So, the main activity:
所以,主要活动:
import java.util.ArrayList;
import java.util.List;
import jv.android.getpoint.R;
import jv.android.getpointlib.data.Formularios;
import jv.android.getpointlib.data.GetPointDataHelper;
import jv.android.getpointlib.data.Respostas;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.WindowManager.LayoutParams;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class RespostasActivity extends Activity {
private Intent intent;
private ArrayList<RespostasArray> listItems = null;
private GetPointDataHelper db = null;
private int coordenada = -1;
private int formulario = -1;
private LinearLayout llRespostas;
private TextView tvRespostasHeader;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout.xml);
llRespostas = (LinearLayout)findViewById(R.id.llRespostas);
tvHeader = (TextView)findViewById(R.id.tvHeader);
listItems = new ArrayList<RespostasArray>();
db = new GetPointDataHelper(this, false);
intent = getIntent();
if (intent != null)
{
Bundle params = intent.getExtras();
if (params != null) {
coordenada = params.getInt("coordenada");
formulario = params.getInt("formulario");
tvHeader.setText("Some header");
// Load the fields I need from SQLite. Change it to your needs.
List<Respostas> respostas = db.selectAllRespostaDaCoordenada (coordenada, formulario);
if (respostas != null && respostas.size() > 0) {
for (int i = 0; i < respostas.size(); i++) {
TextView tv = new TextView(getApplicationContext());
tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tv.setText(respostas.get(i).getQuestao().trim() + (respostas.get(i).getQuestao().trim().endsWith(":") ? "" : ":"));
llRespostas.addView(tv);
EditText et = new EditText(getApplicationContext());
et.setText(respostas.get(i).getResposta().trim());
et.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
llRespostas.addView(et);
RespostasArray ra = new RespostasArray();
ra.setCoordenada(respostas.get(i).getCoordenada());
ra.setForm_coord(respostas.get(i).getForm_coord());
ra.setForm_resposta(respostas.get(i).getForm_resposta());
ra.setFormulario(respostas.get(i).getFormulario());
ra.setPergunta(respostas.get(i).getPergunta());
ra.setQuestao(respostas.get(i).getQuestao());
ra.setResposta(respostas.get(i).getResposta());
ra.setRespostaEdit(et);
listItems.add(ra);
}
}
}
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
doProcessResult();
}
return super.onKeyDown(keyCode, event);
}
private void doProcessResult() {
for (int i = 0; i < listItems.size(); i++) {
if (listItems.get(i).getForm_resposta() == 0) {
db.insertResposta(listItems.get(i).getFormulario(), listItems.get(i).getForm_coord(), listItems.get(i).getPergunta(), listItems.get(i).getRespostaEdit().getText().toString());
} else {
db.updateResposta(listItems.get(i).getForm_resposta(), listItems.get(i).getRespostaEdit().getText().toString());
}
}
}
}
I hope it helps someone.
我希望它可以帮助某人。