2012-03-19 14 views
7

Tengo un problema desagradable. Tengo EditText (8 líneas) dentro de ScrollView. Y cuando estoy tratando de desplazar texto en EditText, su comportamiento no es estable. A veces se desplaza, algunas veces no enfoca.Desplazamiento Editar texto dentro de ScrollView

Este es mi archivo de diseño, para hacer mi pregunta más clara:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="8dp" > 

    <TextView 
     android:id="@+id/wo_task_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/no_description" /> 

    <TextView 
     android:id="@+id/wo_task_description" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/no_description" /> 

    <TextView 
     android:id="@+id/wo_task_comments_label" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:text="@string/comments" /> 

    <Button 
     android:id="@+id/wo_task_select_comment" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/select_comment_from_template" /> 

    <EditText 
     android:id="@+id/wo_task_comments" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="top|left" 
     android:hint="@string/enter_your_comment_here" 
     android:lines="8" 
     android:maxLines="10" 
     android:minLines="6" 
     android:scrollbars="vertical" 
     android:singleLine="false" /> 
</LinearLayout> 

entiendo que estoy teniendo este problema debido a la celebración de un mando desplazable dentro de otro, sin embargo, no sé qué hacer con esto. Entonces, por favor ayudame si puedes.

Gracias de antemano.

public void initComments(final View view) { 
    EditText comment = (EditText) view.findViewById(R.id.wo_task_comments); 

    comment.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(final View v, final MotionEvent motionEvent) { 
      if (view.getId() == R.id.wo_task_comments) { 
       view.getParent().requestDisallowInterceptTouchEvent(true); 
       switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) { 
       case MotionEvent.ACTION_UP: 
        view.getParent().requestDisallowInterceptTouchEvent(
          false); 
        break; 
       } 
      } 
      return false; 
     } 
    }); 

    comment.setText(currentTask.getComment() + "very very long comment" 
      + "very very long comment\n" + "very very long comment\n" 
      + "very very long comment\n" + "very very long comment\n" 
      + "very very long comment"); 
} 

He intentado esto, y no he obtenido ningún resultado. Todavía no puedo desplazar mi editbox.

Respuesta

28

Proveedores:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    ..... 
    EditText et = (EditText)findViewById(R.id.wo_task_comments); 
    et.setOnTouchListener(this); 
    ..... 
} 

@Override 
public boolean onTouch(View view, MotionEvent motionEvent) { 
    if(view.getId() == R.id.wo_task_comments){ 
     view.getParent().requestDisallowInterceptTouchEvent(true); 
     switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) { 
      case MotionEvent.ACTION_UP: 
       view.getParent().requestDisallowInterceptTouchEvent(false); 
       break; 
     } 
    } 
    return false; 
} 

En su caso:

public class MyActivity extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    initComments(findViewById(R.id.YOUR_MAIN_LAYOUT_ID)); 
} 


public void initComments(final View view) { 
    EditText comment = (EditText) view.findViewById(R.id.wo_task_comments); 

    comment.setOnTouchListener(new View.OnTouchListener() { 

     @Override 
     public boolean onTouch(final View v, final MotionEvent motionEvent) { 
      if (v.getId() == R.id.wo_task_comments) { 
       v.getParent().requestDisallowInterceptTouchEvent(true); 
       switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) { 
        case MotionEvent.ACTION_UP: 
         v.getParent().requestDisallowInterceptTouchEvent(
           false); 
         break; 
       } 
      } 
      return false; 
     } 
    }); 

    comment.setText("very very long comment" 
      + "very very long comment\n" + "very very long comment\n" 
      + "very very long comment\n" + "very very long comment\n" 
      + "very very long comment\n" + "very very long comment\n" 
      + "very very long comment\n" + "very very long comment\n" 
      + "very very long comment\n" + "very very long comment\n" 
      + "very very long comment\n" + "very very long comment\n" 
      + "very very long comment\n" + "very very long comment\n" 
      + "very very long comment\n" + "very very long comment\n" 
      + "very very long comment"); 
} 

}

+0

era sólo publicar la misma respuesta, como una adición que tal vez tiene que desactivar otra desplazamiento otherviews.setOnTouchListener (nueva OnTouchListener() { \t \t \t public boolean onTouch (Vista v, evento MotionEvent) { \t \t \t \t return true; \t \t \t} \t \t}); – Ceetn

+0

gracias por la respuesta. Por el momento, todavía no puedo solucionar mi problema. – Orest

+0

He actualizado mi pregunta. – Orest

-1

Una opción es establecer la altura del niño EditText como la altura de las líneas totales max (max_lines * font_height). Por lo tanto, el desplazamiento interno estará desactivado y el único desplazamiento será el desplazamiento principal.

0

El código de Shilkin funciona muy bien. Lo hago un poco más fuerte. Ahora maneja la situación de que el padre directo de EditText no es el ScrollView (por ejemplo, EditText está envuelto en LinearLayout antes de ponerlo en la vista de desplazamiento).

El código de método:

public static void handleEditTextScrollable(EditText editText, final int resId) { 
    editText.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      if (v.getId() == resId) { 
       ViewParent parent = v.getParent(); 
       while (!(parent instanceof ScrollView)) { 
        parent = parent.getParent(); 
       } 
       parent.requestDisallowInterceptTouchEvent(true); 

       switch (event.getAction() & MotionEvent.ACTION_MASK) { 
        case MotionEvent.ACTION_UP: 
         parent.requestDisallowInterceptTouchEvent(false); 
         break; 
       } 
      } 
      return false; 
     } 
    }); 
} 

llamada así:

handleEditTextScrollable(comment, R.id.wo_task_comments); 
Cuestiones relacionadas