ListView中的EditText不会保持专注

时间:2012-10-04 15:25:12

标签: android listview focus android-edittext

我在EditText的每一行中都有一个ListView。出于某种原因,当我点击EditText时,它会短暂地获得焦点但然后立即失去它。

我尝试过这些东西:

listView.setItemsCanFocus(true);
editText.setFocusable(true);


虽然EditText是(简要)重点关注,但Enter键表示Nextauto-correct bar存在。当它失去焦点时,软键盘会保持向上,但Enter键变为Return Arrowauto-correct bar消失。

3 个答案:

答案 0 :(得分:44)

如果您希望列表项目成为焦点,那么它非常直接: 使列表视图无法调焦,并说它的焦点是afterDescendants,这使得EditText和其他可聚焦项目成为焦点。

<ListView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="afterDescendants"
    android:focusable="false" />

答案 1 :(得分:9)

EditText内的

ListView非常棘手。如果可能的话,我建议你像瘟疫一样避开它们。当我弄乱他们的时候,this answer虽然很有帮助。如果您只有一些项目,则可以随时使用ScrollView

答案 2 :(得分:2)

我知道这个问题很老,但我今天也在与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>

“containter”类:

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");

// 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());                
                }
            }
        }
    }

我希望它有所帮助。