简体   繁体   English

Android:使多行编辑文本可滚动,在垂直滚动视图中禁用

[英]Android: Make multiline edittext scrollable, disable in vertical scroll view

I am developing an application in which i am struct at a point.我正在开发一个应用程序,其中我在某个点上是结构体。

As according to my application requirement i created horizontal scrollview in xml and then vertical scrollview in .java as :根据我的应用程序要求,我在 xml 中创建了水平滚动视图,然后在 .java 中创建了垂直滚动视图,如下所示:

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

Then i created a table view programatically and added it in scrollview.然后我以编程方式创建了一个表视图并将其添加到滚动视图中。 I created multiline edit text and disable it because i want to set text in it run time and added this in table view as a row..我创建了多行编辑文本并禁用它,因为我想在它运行时设置文本并将其添加到表格视图中作为一行..

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

Now i want to make edit text scrollable which i achieve by code:现在我想让编辑文本可滚动,我通过代码实现:

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

But the edittext is not scrolling easily.但是编辑文本不容易滚动。 I need the smooth scrolling.我需要平滑滚动。

How to do that please guide me.如何做到这一点,请指导我。

You can do one thing.你可以做一件事。

Just make edit text focusable false.只需使编辑文本可聚焦为假。 And apply on touch listener.并应用于触摸监听器。

So user is not able to edit text and it will scroll as:因此用户无法编辑文本,它将滚动为:

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

Apply onTouchListner as:将 onTouchListner 应用为:

editTextRemark.setOnTouchListener(touchListener);

and

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

Hope this answer will help you.希望这个回答能帮到你。

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

And in xml add this line in EditText:在 xml 中,在 EditText 中添加这一行:

android:scrollbars = "vertical"

添加到编辑文本

 android:inputType="textMultiLine"

in kotlin use this在 kotlin 中使用这个

editText.setOnTouchListener { v, event ->
    v.parent.requestDisallowInterceptTouchEvent(true)
    when (event.action and MotionEvent.ACTION_MASK) {
        MotionEvent.ACTION_UP ->  v.parent.requestDisallowInterceptTouchEvent(false)
    }
    false
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM