2012-10-04 42 views
23

Tôi có một EditText bên trong mỗi hàng của một ListView. Vì một lý do nào đó, khi tôi nhấn EditText, nó sẽ nhanh chóng lấy nét nhưng sau đó ngay lập tức mất nó.EditText bên trong ListView sẽ không được tập trung

Tôi đã thử những điều sau:

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


Trong khi EditText là (tóm tắt) tập trung, phím Enter nói Nextauto-correct bar có mặt. Khi bị mất tiêu điểm, bàn phím mềm vẫn ở lại, nhưng phím Enter sẽ trở thành Return Arrowauto-correct bar sẽ biến mất.

+0

tôi giả sử bạn đã quên mất cách chế độ xem được tái chế. gợi ý: không sử dụng editText trong mục danh sách. – njzk2

+1

Tại sao tái chế chúng lại quan trọng? – Matt

+2

vì có rất ít văn bản được sử dụng luân phiên để vẽ từng hàng và chúng đang nhận được tất cả loại thay đổi trạng thái lấy nét cùng một lúc – njzk2

Trả lời

8

EditText s trong vòng ListView s cực kỳ phức tạp. Tôi muốn đề nghị bạn tránh chúng như bệnh dịch hạch nếu có thể. Khi tôi đã rối tung với họ, this answer là hữu ích mặc dù. Nếu bạn chỉ có một vài mục, bạn luôn có thể sử dụng một số ScrollView.

+0

Vâng, sử dụng một linearlayout dọc bên trong một scrollview đã làm các trick. Một điều tôi nhận thấy là nếu nó là một ListActivity, các EditTexts trong ListView làm việc tốt. Nhưng không phải nếu đó là một ListView trong một hoạt động thường xuyên. – Matt

+1

Nếu chúng ta tránh chúng, thay thế là gì? –

+0

"Bạn luôn có thể sử dụng ScrollView" – dmon

0

Tôi biết câu hỏi này là cũ, nhưng tôi cũng đã chiến đấu với EditText và ListView hôm nay. Tôi nghĩ rằng mất thời gian với vấn đề tập trung hoặc cập nhật tập dữ liệu là kết thúc của dòng.

Vì vậy, tôi đã tạo Hoạt động và tự động thêm các điều khiển tôi cần. Trong ví dụ này tôi đã thêm hai điều khiển: một textview và edittext.

Trước tiên, tôi tạo một lớp là "vùng chứa" của các điều khiển và chúng tôi đặt các điều khiển trên một ScrollView.

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> 

Các "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; 
    } 

} 

Vì vậy, các hoạt động chính:

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

Tôi hy vọng nó sẽ giúp một ai đó.

42

Nếu bạn muốn các mục danh sách cần lấy nét, hướng thẳng về phía trước: Làm cho chế độ xem danh sách không thể lấy nét và nói rằng tiêu điểm của nó là afterDescendants cho phép các mục EditText s và các tiêu điểm có thể lấy nét khác lấy nét.

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

dường như không hoạt động –

+1

Nó làm việc cho tôi ... cảm ơn :) –

+0

Cảm ơn câu trả lời có giá trị của bạn :) – Subho

Các vấn đề liên quan