3

我正在开发一个应用程序,在该应用程序中我是 struct。

根据我的应用程序要求,我在 xml 中创建了水平滚动视图,然后在 .java 中创建了垂直滚动视图:

// Vertical Scroll view in Linear layout
ScrollView scrollView = new ScrollView(this);
scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

然后我以编程方式创建了一个表格视图并将其添加到滚动视图中。我创建了多行编辑文本并将其禁用,因为我想在运行时设置文本并将其添加到表格视图中作为一行..

EditText editText = new EditText(this);
editText.setId(1);
editText.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,0f));
editText.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
editText.setGravity(Gravity.TOP|Gravity.LEFT);
editText.setHint("Comment");
editText.setSingleLine(false);
editText.setLines(5);
editText.setMaxLines(5);
editText.setText(CommentFromDB);
editText.setEnabled(false);

tableLayout.addView(editText);

// Add table in Horizontal scroll view
scrollView.addView(tableLayout);

现在我想让我通过代码实现的编辑文本可滚动:

editText.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View view, MotionEvent event) {
            if (view.getId() == 1) {
                view.getParent().requestDisallowInterceptTouchEvent(true);
                switch (event.getAction()&MotionEvent.ACTION_MASK){
                case MotionEvent.ACTION_UP:
                    view.getParent().requestDisallowInterceptTouchEvent(false);
                    break;
                case MotionEvent.ACTION_DOWN:
                    view.getParent().requestDisallowInterceptTouchEvent(false);
                    break;
                }
            }
            return false;
        }
    });

但是编辑文本不容易滚动。我需要平滑滚动。

如何做到这一点,请指导我。

4

4 回答 4

9

你可以做一件事。

只需使编辑文本可聚焦为假。并应用于触摸监听器。

因此用户无法编辑文本,它将滚动为:

EditText editText = new EditText(this);
editText.setId(1);
editText.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,0f));
editText.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
editText.setGravity(Gravity.TOP|Gravity.LEFT);
editText.setHint("Comment");
editText.setSingleLine(false);
editText.setLines(5);
editText.setMaxLines(5);
editText.setText(CommentFromDB);
editTextRemark.setFocusable(false);

将 onTouchListner 应用为:

editTextRemark.setOnTouchListener(touchListener);

OnTouchListener touchListener = new View.OnTouchListener(){
    public boolean onTouch(final View v, final MotionEvent motionEvent){
        if(v.getId() == 1){
            v.getParent().requestDisallowInterceptTouchEvent(true);
            switch (motionEvent.getAction() & MotionEvent.ACTION_MASK){
                case MotionEvent.ACTION_UP:
                    v.getParent().requestDisallowInterceptTouchEvent(false);
                break;
            }
        }
        return false;
    }
};

希望这个答案对您有所帮助。

于 2014-04-30T06:21:35.323 回答
7
EditText editText = findViewById(R.id.editText);
editText.setOnTouchListener(new OnTouchListener() {
               public boolean onTouch(View view, MotionEvent event) {
                    // TODO Auto-generated method stub
                    if (view.getId() ==R.id.common_remark) {
                        view.getParent().requestDisallowInterceptTouchEvent(true);
                        switch (event.getAction()&MotionEvent.ACTION_MASK){
                        case MotionEvent.ACTION_UP:
                            view.getParent().requestDisallowInterceptTouchEvent(false);
                            break;
                        }
                    }
                    return false;
                }
        });

并在 xml 中在 EditText 中添加这一行:

android:scrollbars = "vertical"
于 2014-11-12T09:51:44.503 回答
0

添加到编辑文本

 android:inputType="textMultiLine"
于 2014-10-26T19:38:27.893 回答
0

在 kotlin 中使用这个

editText.setOnTouchListener { v, event ->
    v.parent.requestDisallowInterceptTouchEvent(true)
    when (event.action and MotionEvent.ACTION_MASK) {
        MotionEvent.ACTION_UP ->  v.parent.requestDisallowInterceptTouchEvent(false)
    }
    false
}
于 2021-02-02T13:19:42.683 回答