2012-02-14 32 views
19

Tengo Listview con editext y textview.Edittext en Listview android

Cuando toco el texto de edición, ¡el texto de edición pierde el foco!

Resolví este problema estableciendo android:windowSoftInputMode="adjustPan"(AndroidManifest.xml). Ahora toco en edittext que editext get focus pero la etiqueta de la aplicación y algunos raw de listview desaparecen (parte superior).

Quiero enfocarme cuando el usuario toca el texto de edición sin etiqueta de pérdida de la aplicación y parte de la vista de lista sin procesar.

El código que he implementado:

continuación de codificación especial tengan cuando tacto de usuario en EditarTexto pero etiqueta de la aplicación y algunos crudos de vista de lista desaparecen cuando up.I teclado pop suaves quieren obtener el foco cuando tacto de usuario en EditarTexto sin pérdida etiqueta de la aplicación y algo sin formato de listview.

1) AndroidManifest.xml

<application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".MyListViewDemoActivity" 
        android:label="@string/app_name" 
        android:windowSoftInputMode="adjustPan" 
        > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

</application> 

2) raw_layout.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <EditText android:id="@+id/mEditText" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 
</LinearLayout> 

3) main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<ListView android:id="@+id/mListView" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
/> 
</LinearLayout> 

4) MyListViewDemoActivity

public class MyListViewDemoActivity extends Activity { 
    private ListView mListView; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     mListView=(ListView)findViewById(R.id.mListView); 
     mListView.setAdapter(new MyAdapter(this)); 
    } 
} 

class MyAdapter extends BaseAdapter { 

    private Activity mContext; 
    private String character[]={"a","b","c","d","e","f","g","h","i","j"}; 
    public MyAdapter(Activity context) 
    { 
     mContext=context; 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return character.length; 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 
private class Holder 
{ 
    EditText mEditText; 
} 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     final Holder holder; 
     if (convertView == null) { 
      holder = new Holder(); 
      LayoutInflater inflater =mContext.getLayoutInflater(); 
      convertView = inflater.inflate(R.layout.raw_layout, null); 
      holder.mEditText = (EditText) convertView 
        .findViewById(R.id.mEditText); 
      convertView.setTag(holder); 
     } else { 
      holder = (Holder) convertView.getTag(); 
     } 
     holder.mEditText.setText(character[position]); 
     holder.mEditText.setOnFocusChangeListener(new OnFocusChangeListener() { 

      @Override 
      public void onFocusChange(View v, boolean hasFocus) { 
       // TODO Auto-generated method stub 
       if (!hasFocus){ 
        final EditText etxt = (EditText) v; 
        holder.mEditText.setText(etxt.getText().toString()); 
       } 

      } 
     }); 
     return convertView; 
    } 

} 
+2

proporcione su código y dar detalles .. –

+0

proporcione más detalles para obtener la ayuda adecuada – Raj

+0

Posible duplicado de [Editar texto enfocado dentro de ListView] (http://stackoverflow.com/questions/2679948/focusable-edittext-inside-listview) – blahdiblah

Respuesta

4

Sin ver su código, ¿cómo podemos sugerir la posible solución a su problema? Por lo tanto, mantenga la práctica de publicar posibles códigos cada vez que haga alguna pregunta.

Sin embargo, aquí he encontrado un tutorial para implementar Android Focusable EditText inside ListView. Repase el ejemplo e intente implementarlo en su camino o descubra la solución a su problema.

-1
 <ListView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:drawSelectorOnTop="false" 
     android:id="@+id/list1" 
     android:layout_weight="1" 
     android:cacheColorHint="#00000000" 
     android:focusable="false"     
     > 
     </ListView> 
3

He resuelto este problema "Poniendo EditText en ListView como un elemento" recientemente. No soy muy bueno en inglés. Entonces, si hay algo que no explico claramente, por favor dígame.

Sabemos que ListView se puede desplazar verticalmente y queremos poner EditText en ListView como un elemento.

Primero: Añadir

androide: windowSoftInputMode = "adjustResize"

en su AndroidManifest.xml en el nodo de actividad.

Segundo: creamos un POJO como fuente de datos modelo para controlar el estado EditarTexto

línea.java

public class Line{ 
    int num; 
    String text; 
    boolean focus; 

    get set method and so on... 
} 

Tercero: Escribimos un adaptador para adaptarse a EditarTexto ListView. item_line.xml

artículo:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/frameLayout" 
    android:layout_width="match_parent" 
    android:layout_height="80dp"> 

    <EditText 
     android:id="@+id/etLine" 
     android:focusable="true" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom"/> 
</FrameLayout> 

adaptador:

@Override 
public View getView(final int position, View convertView, final ViewGroup parent) { 
    final ViewHolder holder; 
    if (convertView == null) { 
     holder = new ViewHolder(); 
     convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_line, parent, false); 
     holder.etLine = (EditText) convertView.findViewById(R.id.etLine); 
     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    final Line line = lines.get(position); 

    // step 1: remove android.text.TextWatcher added in step 5 to make sure android.text.TextWatcher 
    //   don't trigger in step 2; 
    // why? 
    // 
    // note: When an object of a type is attached to an Editable, 
    //  TextWatcher's methods will be called when the EidtText's text is changed. 
    //  
    //  EditText use a ArrayList<TextWatcher> type object to store the listener, so we must 
    //  make sure there's only one TextWatcher object in this list; 
    // 
    // Avoid triggering TextWatcher's method in step 2 we remove it at first time. 
    // 
    if (holder.etLine.getTag() instanceof TextWatcher) { 
     holder.etLine.removeTextChangedListener((TextWatcher) (holder.etLine.getTag())); 
    } 

    // step 2: set text and focus after remove android.text.TextWatcher(step 1); 
    holder.etLine.setHint(position + "."); 

    // set text 
    if (TextUtils.isEmpty(line.getText())) { 
     holder.etLine.setTextKeepState(""); 
    } else { 
     holder.etLine.setTextKeepState(line.getText()); 
    } 

    // set focus status 
    // why? 
    // 
    // note: ListView has a very elegant recycle algorithm. So views in ListView is not reliable. 
    //  Especially in this case, EditText is an item of ListView. Software input window may cause 
    //  ListView relayout leading adapter's getView() invoke many times. 
    //  Above all if we change EditText's focus state directly in EditText level(not in Adapter). 
    //  The focus state may be messed up when the particularly view reused in other position. 
    //  
    //  So using data source control View's state is the core to deal with this problem. 
    if (line.isFocus()) { 
     holder.etLine.requestFocus(); 
    } else { 
     holder.etLine.clearFocus(); 
    } 

    // step 3: set an OnTouchListener to EditText to update focus status indicator in data source 
    // why? 
    // 
    // in step 2, we know we must control view state through data source. We use OnTouchListener 
    // to watch the state change and update the data source when user move up fingers(ACTION_UP). 
    // We don't want to consume the touch event, simply return false in method onTouch(). 
    holder.etLine.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      if (event.getAction() == MotionEvent.ACTION_UP) { 
       check(position); 
      } 
      return false; 
     } 
    }); 

    // step 4: set TextWatcher to EditText to listen text changes in EditText to updating the text in data source 
    // why? 
    // 
    // again, use data source to control view state. 
    // When user edit the text in one EditText item and scroll the ListView. The particularly EditText item will be 
    // reuse in adapter's getView(), this may lead text messed up in ListView. 
    // How to deal with this problem? 
    // Easy! We update the text in data source at the same time when user is editing. TextWatcher is the best way to 
    // do this. 
    final TextWatcher watcher = new SimpeTextWather() { 

     @Override 
     public void afterTextChanged(Editable s) { 
      if (TextUtils.isEmpty(s)) { 
       line.setText(null); 
      } else { 
       line.setText(String.valueOf(s)); 
      } 
     } 
    }; 
    holder.etLine.addTextChangedListener(watcher); 

    // step 5: Set watcher as a tag of EditText. 
    // so we can remove the same object which was setted to EditText in step 4; 
    // Make sure only one callback is attached to EditText 
    holder.etLine.setTag(watcher); 

    return convertView; 
} 

/** 
* change focus status in data source 
*/ 
private void check(int position) { 
    for (Line l : lines) { 
     l.setFocus(false); 
    } 
    lines.get(position).setFocus(true); 
} 

static class ViewHolder { 
    EditText etLine; 
} 

Todo hecho!

Puede leer más detalles en mi github.

Demostración: https://github.com/Aspsine/EditTextInListView

+0

Esto funciona, gracias por explicar todo! –

6

que estaba teniendo el mismo problema. Mi teclado numérico aparecerá momentáneamente antes de ser reemplazado por el teclado qwerty y EditText perderá el foco.

El problema es que el teclado que aparece hace que EditText pierda el foco. Para evitar que esto pone lo siguiente en su AndroidManifest.xml para la actividad pertinente (o actividades):

android:windowSoftInputMode="adjustPan" 

Ver documentation Android:

Cuando aparezca el método de entrada en la pantalla, se reduce la cantidad de espacio disponible para la interfaz de usuario de su aplicación. El sistema toma una decisión sobre cómo debe ajustar la parte visible de su UI, pero puede no ser correcta. Para garantizar el mejor comportamiento para su aplicación, debe especificar cómo le gustaría que el sistema muestre su UI en el espacio restante.

Para declarar su tratamiento preferido en una actividad, use el atributo android:windowSoftInputMode en el elemento <activity> de su manifiesto con uno de los valores de "ajuste".

Por ejemplo, para asegurar que el sistema cambia el tamaño de su diseño al espacio disponible, lo cual garantiza que todo el contenido de la disposición es accesible (aunque probablemente requiere desplazamiento) -uso "adjustResize"

0

ListView recrear la Ver, intenta utilizar un LinearLayout dentro de ScrollView, a continuación, en el código de utilizar un runOnUiThread para llenar su vista en otro hilo como este

public void fillDataTask(Context context, final LinearLayout listView) { 

     getActivity().runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 

       fillView(context, listView); 
      } 
     }); 
    } 



    private void fillView(Context context, LinearLayout listView) { 
      MyAdapter adapter = new MyAdapter(context); 
      final int adapterCount = adapter.getCount(); 
      for (int i = 0; i < adapterCount; i++) { 
       View item = adapter.getView(i, null, null); 
       listView.addView(item); 
      } 

    }