Scrolling EditText inside ScrollView

前端 未结 3 1924
礼貌的吻别
礼貌的吻别 2020-11-29 09:27

I have a nasty problem. I have EditText(8 lines) inside ScrollView. And when I\'m trying to scroll text in EditText it\'s behavior is

相关标签:
3条回答
  • 2020-11-29 10:12

    Shilkin's code works great.I make it a little more stronger.Now it handle the situation that the EditText's direct parent is not the ScrollView(eg, EditText is wrapped in LinearLayout before it put into the scrollview).

    The method code:

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

    Call it like this:

    handleEditTextScrollable(comment, R.id.wo_task_comments);
    
    0 讨论(0)
  • 2020-11-29 10:14

    One Option is to set the height of the child EditText as the height of the total max lines (max_lines * font_height). So, the internal scroll will be off and the only scroll will be the parent scroll.

    0 讨论(0)
  • 2020-11-29 10:20

    Try:

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

    In your case:

    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 讨论(0)
提交回复
热议问题