Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 在ScrollView中滚动EditText大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个讨厌的问题.我在ScrollView中有EditText(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");
}

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

解决方法

尝试:
@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");
}

}

大佬总结

以上是大佬教程为你收集整理的android – 在ScrollView中滚动EditText全部内容,希望文章能够帮你解决android – 在ScrollView中滚动EditText所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。