How to scroll the edittext inside the scrollview

前端 未结 12 1993
南笙
南笙 2020-12-02 10:27

I have a scrollview inside which there is a editext which is multiline. I want to scroll the edittext to see the lower content but it can\'t be done.

12条回答
  •  盖世英雄少女心
    2020-12-02 10:58

    Below Answer will help you to make Edit Text scrollable inside Scroll View and also show Count of it.

    1.Make one rectangle_with_border_gray.xml file in @drawable folder.

     
        
            
            
            
        
    

    2.Then, In @layout write below code in scroll view.

    
    
                 
    
                        
    
                            
    
                        
    
                        
    
             
     
    

    3.Then, Inside your Activity or Fragment write below code:

    TextView tv_description_count = (TextView) view.findViewById(R.id.tv_description_count);
    
    EditText et_optional_message = (EditText) findViewById(R.id.et_optional_message);
    
    private void makeScrollable(){
                et_optional_message.addTextChangedListener(mTextEditorWatcher);
                et_optional_message.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View view, MotionEvent motionEvent) {
                        view.getParent().requestDisallowInterceptTouchEvent(true);
                        switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
                            case MotionEvent.ACTION_SCROLL:
                                view.getParent().requestDisallowInterceptTouchEvent(false);
                                return true;
                            case MotionEvent.ACTION_BUTTON_PRESS:
                                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                                imm.showSoftInput(et_optional_message, InputMethodManager.SHOW_IMPLICIT);
                        }
                        return false;
                    }
                });
            }
    
     private final TextWatcher mTextEditorWatcher = new TextWatcher() {
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }
    
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    //This sets a textview to the current length
                    tv_description_count.setText(String.valueOf(s.length()));
                }
    
                public void afterTextChanged(Editable s) {
                }
            };
    

    Happy Coding......

提交回复
热议问题