在ScrollView中滚动EditText

时间:2012-03-19 12:31:39

标签: android layout android-edittext scrollview

我有一个讨厌的问题。我在EditText内有ScrollView(8行)。当我试图在EditText中滚动文本时,它的行为并不稳定。有时它会滚动,有时它不会聚焦。

这是我的布局文件,让我的问题更清晰:

<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>

我知道我遇到这个问题是因为在另一个内部有一个可滚动控件,但我不知道该如何处理。所以,如果可以,请帮助我。

提前致谢。

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

我试过这个,没有结果。我仍然无法滚动我的编辑框。

3 个答案:

答案 0 :(得分:28)

尝试:

@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;
}

在你的情况下:

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

}

答案 1 :(得分:0)

Shilkin的代码效果很好。我使它更强一些。现在它处理的情况是EditText的直接父级不是ScrollView(例如,EditText在放入scrollview之前包装在LinearLayout中)。

方法代码:

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

这样称呼:

handleEditTextScrollable(comment, R.id.wo_task_comments);

答案 2 :(得分:-1)

一个选项是将子EditText的高度设置为总最大行的高度(max_lines * font_height)。因此,内部滚动将关闭,唯一的滚动将是父滚动。