我知道这个问题很旧了,但今天我也在处理EditText和ListView。我认为浪费时间处理焦点问题或更新数据集已经是最后的选择了。
因此,我创建了一个Activity并动态添加所需的控件。在这个例子中,我添加了两个控件:一个TextView和EditText。
首先,我创建一个类作为控件的“容器”,然后将控件放在ScrollView中。
布局文件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>
“容器”类:
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;
}
}
因此,主要活动:
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");
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());
}
}
}
}
希望这能帮助到某些人。
android:windowSoftInputMode="adjustPan"
在<activity>
中完成了这个。它的效果非常好。 - Kasim Rangwala