Create a multiline EditText programmatically

前端 未结 8 1021
無奈伤痛
無奈伤痛 2021-01-03 21:58

I am trying to create a multiline EditText by code. This is what I use:

EditText txt = new EditText(this);    
lp = new LinearLayout.LayoutParams(LayoutParam         


        
相关标签:
8条回答
  • 2021-01-03 22:56

    Combining all above answers was the correct answer so here it is:

    texInput.setSingleLine(false);
    texInput.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION);
    texInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
    texInput.setLines(5);
    texInput.setMaxLines(10);
    texInput.setVerticalScrollBarEnabled(true);
    texInput.setMovementMethod(ScrollingMovementMethod.getInstance());
    texInput.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
    
    0 讨论(0)
  • 2021-01-03 22:56

    In addition to the above suggestions - you might want to set the below additional parameters if you are not able to get this working

    texInput.setHorizontallyScrolling(false);
    texInput.setHorizontalScrollBarEnabled(false);
    

    If you are changing the height of the edittext programmatically then you might want to request a layout too by calling requestLayout().

    0 讨论(0)
提交回复
热议问题