2012-03-25 11 views
1

Ich habe dieses Problem schon oft gesehen, und ich denke, es ist nicht so schwer zu beheben; Noch habe ich mehrere Stunden ausprobiert, kann nicht verstehen wie es funktioniert.Android: Warum verliert mein EditText-Feld den Fokus?

Einfaches Problem: Ich habe 2 EditText in einem ListView, und beide verlieren sofort den Fokus verlieren, sobald ich ihr Feld berühren.

Muss etwas Hierarchie-Zeug sein. Eigentlich kann ich es mit adjustpan Eigenschaft beheben, aber meine ListView scrollt nicht mehr, während SoftKeyboard eingeschaltet ist.

ist hier mein Code:

package com.android.activity; 

import android.content.Context; 
import android.database.Cursor; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.CursorAdapter; 
import android.widget.EditText; 
import android.widget.TextView; 

public class NotesCursorAdapter extends CursorAdapter{ 

    private Context context; 
    // private Cursor cursor; 
    private int addNoteTopPosition; 
    private int addNoteBottomPosition; 
    private LayoutInflater inflater; 
    private boolean deleteMode = false; 

    private static final int TYPE_ITEM = 0; 
    private static final int TYPE_ADD_NOTE_BOTTOM = 1; 
    private static final int TYPE_ADD_NOTE_TOP = 2; 
    private static final int TYPE_MAX_COUNT = TYPE_ADD_NOTE_TOP + 1; 

    public NotesCursorAdapter (Context context, Cursor cursor, int flag, boolean enableDelete){ 
     super(context, cursor); 
     this.context = context; 
     addNoteTopPosition = 0; 
     addNoteBottomPosition = cursor.getCount()+1; 

     inflater = LayoutInflater.from(this.context); 

     deleteMode = enableDelete; 

    } 

    @Override 
    public int getCount() { 
     return super.getCount() + 2; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent){ 
     ViewHolder holder = null; 
     int type = getItemViewType(position); 
     if (convertView == null) { 
      holder = new ViewHolder(); 
      switch (type) { 

      case TYPE_ADD_NOTE_TOP: 
       convertView = inflater.inflate(R.layout.add_note_top, null); 
       holder.view = (EditText)convertView.findViewById(R.id.add_note_top_id); 
       break; 
      case TYPE_ITEM: 
       convertView = inflater.inflate(R.layout.row_note, null); 
       holder.delete = (Button)convertView.findViewById(R.id.delete); 
       if (deleteMode){ 
        holder.delete.setVisibility(View.VISIBLE); 
       }else{ 
        holder.delete.setVisibility(View.GONE); 
       } 
       holder.delete.setOnClickListener(new OnClickListener() { 

        @Override 
        public void onClick(View v) { 

        } 
       }); 

       holder.view = (TextView)convertView.findViewById(R.id.note); 
       getCursor().moveToPosition(position - 1); 
       ((TextView) holder.view).setText(getCursor().getString(getCursor().getColumnIndex("content_note"))); 
       break; 
      case TYPE_ADD_NOTE_BOTTOM: 
       convertView = inflater.inflate(R.layout.add_note_bottom, null); 
       holder.view = (EditText)convertView.findViewById(R.id.add_note_bottom_id); 
       break; 
      } 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder)convertView.getTag(); 
     } 

     return convertView; 
    } 

    @Override 
    public int getItemViewType(int position) { 
     int type; 
     if (position == addNoteTopPosition){ 
      type = TYPE_ADD_NOTE_TOP; 
     } else if (position == addNoteBottomPosition){ 
      type = TYPE_ADD_NOTE_BOTTOM; 
     }else { 
      type = TYPE_ITEM; 
     } 
     return type; 
    } 

    @Override 
    public int getViewTypeCount() { 
     return TYPE_MAX_COUNT; 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    public static class ViewHolder { 
     public View delete; 
     public View view; 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 

    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     return null; 
    } 

} 

Hier sind XML-Dateien:

main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/rootLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/background_diary_big" 
    android:focusable="false" 
    android:orientation="vertical" > 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="60dp" 
     android:layout_gravity="right" 
     android:gravity="fill_vertical" 
     android:orientation="horizontal" 
     android:paddingRight="10dp" 
     android:paddingTop="10dp" > 

     <LinearLayout 
      android:id="@+id/linearLayout1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:orientation="vertical" > 

      <Button 
       android:id="@+id/calendar" 
       android:layout_width="50dp" 
       android:layout_height="50dp" 
       android:layout_gravity="right" 
       android:background="@drawable/calendar_up" /> 
     </LinearLayout> 

     <TextView 
      android:id="@+id/currentDay" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_gravity="center_vertical|center_horizontal" 
      android:layout_weight="0.5" 
      android:gravity="center_vertical|center_horizontal" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

     <LinearLayout 
      android:id="@+id/linearLayout2" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:gravity="center_vertical|center_horizontal" 
      android:orientation="vertical" > 

      <Button 
       android:id="@+id/done" 
       android:layout_width="50dp" 
       android:layout_height="50dp" 
       android:background="@drawable/pen_blue_big" /> 
     </LinearLayout> 
    </LinearLayout> 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:focusable="false" 
     android:orientation="vertical" > 

     <ListView 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/main_list" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_marginLeft="10dip" 
      android:layout_marginRight="10dip" 
      android:cacheColorHint="#00000000" 
      android:descendantFocusability="beforeDescendants" > 
     </ListView> 
    </LinearLayout> 

</LinearLayout> 

add_note_top.xml:

<?xml version="1.0" encoding="utf-8"?> 
<EditText xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/add_note_top_id" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="#00000000" 
    android:hint="@string/add_note" 
    android:textColor="#000000" > 

</EditText> 

add_note_bottom.xml :

<EditText 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/add_note_bottom_id" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/add_note" 
     android:background="#00000000" 
     android:textColor="#000000" > 
    </EditText> 

row_note.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layoutContainer" 
    android:layout_width="fill_parent" 
    android:layout_height="60dp" 
    android:layout_gravity="right" 
    android:gravity="fill_vertical" 
    android:orientation="horizontal" 
    android:paddingRight="10dp" 
    android:paddingTop="10dp" > 

    <Button 
     android:id="@+id/delete" 
     android:layout_width="21dp" 
     android:layout_height="22dp" 
     android:background="@drawable/sens_interdit" /> 

    <TextView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/note" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:duplicateParentState="true" 
     android:focusable="false" 
     android:textColor="#000000" > 
    </TextView> 

</LinearLayout> 

Ich glaube, ich die wichtige setzen, wenn Sie Jungs brauchen etwas mehr wie Aktivitätscode oder etwas anderes, einfach fragen.

Danke fürs Lesen.

+1

Ihr Hintergrund und Textfarbe gleich zu setzen. Ich frage mich, ob das das Problem ist. – Siddharth

+0

Nun, das ist ein guter Punkt, ahah. Aber ich habe das Problem herausgefunden, ich habe meinen StackOverflow-Post vergessen. Nur für Leute, die das wissen möchten, ist dies ein häufiges Problem mit listview-edittext. Wenn Sie im adjustResize-Modus den Fokus auf einen Bearbeitungstext legen, wird der Fokus aufgrund der Größenanpassung bei der Softkeyboard-Darstellung verloren gehen. Daher habe ich eine nicht gut aussehende Problemumgehung gefunden, die den Fokus auf diesen Editext nach dem Erscheinen der Tastatur legt. Konkret wird der Aufruf von edittext.requestfocus() auf einem Listener für die Aktivität, die auf jedem Softkeyboard aufgerufen wird, angezeigt. Trotzdem war dein Kommentar nützlich und bemerkte einen Fehler, danke =) –

Antwort

0

Versuchen

android:windowSoftInputMode="adjustPan" 

für Ihre Tätigkeit in der AndroidManifest.xml

Verwandte Themen