2016-11-11 23 views
2

Я пытаюсь установить Maxline для программы EditText, это не дает дополнительного места в EditText после очистки текста. Если я устанавливаю maxLine в xml, появляется дополнительное пространство, даже если в EditText нет текста.EditText.setMaxLines() программно не работает

Как предоставить дополнительное пространство программным способом вместо использования xml. потому что мне нужно использовать один и тот же xml для многих представлений.

Programmatic код

public void showDescriptionDialog() { 
    final Dialog dialog = new Dialog(context); 
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    dialog.setContentView(R.layout.dialog_upload_description); 
    final EditText edtDescription = (EditText) dialog.findViewById(R.id.edt_upload_description); 
    edtDescription.setHint(context.getString(R.string.description)); 
    edtDescription.setSingleLine(false); 
    edtDescription.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION); 
    edtDescription.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE); 
    edtDescription.setMaxLines(5); 
    edtDescription.setVerticalScrollBarEnabled(true); 
    edtDescription.setMovementMethod(ScrollingMovementMethod.getInstance()); 
    edtDescription.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET); 
    if (description != null) { 
     edtDescription.setText(description); 
    } 
    dialog.show(); 


    dialog.findViewById(R.id.img_upload_description_back).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      dialog.dismiss(); 
     } 
    }); 

    dialog.findViewById(R.id.img_upload_description_done).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      description = edtDescription.getText().toString(); 
      dialog.dismiss(); 
     } 
    }); 

    dialog.findViewById(R.id.btn_upload_description_done).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      description = edtDescription.getText().toString(); 
      dialog.dismiss(); 
     } 
    }); 
} 

XML код

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center_horizontal" 
    android:orientation="vertical" 
    android:paddingBottom="30dp"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/colorPrimary" 
     android:padding="10dp"> 

     <ImageView 
      android:id="@+id/img_upload_description_back" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentStart="true" 
      android:layout_centerVertical="true" 
      android:layout_marginEnd="10dp" 
      android:contentDescription="@string/upload" 
      android:src="@drawable/ic_back_white" /> 

     <TextView 
      android:id="@+id/title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:padding="10dp" 
      android:singleLine="true" 
      android:ellipsize="end" 
      android:text="@string/title_description" 
      android:textColor="@android:color/white" 
      android:textSize="@dimen/font_size_large" /> 

     <ImageView 
      android:id="@+id/img_upload_description_done" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentEnd="true" 
      android:layout_centerVertical="true" 
      android:layout_marginEnd="10dp" 
      android:contentDescription="@string/done" 
      android:padding="8dp" 
      android:background="?android:attr/selectableItemBackground" 
      android:src="@drawable/ic_tick_white" /> 

    </RelativeLayout> 

    <EditText 
     android:id="@+id/edt_upload_description" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="40dp" 
     android:layout_marginRight="40dp" 
     android:hint="@string/txt_upload_description_hint" 
     android:gravity="top" 
     android:textSize="@dimen/font_size_small" 
     android:layout_marginTop="30dp" 
     android:minLines="5" /> 

    <Button 
     android:id="@+id/btn_upload_description_done" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="30dp" 
     android:layout_marginRight="30dp" 
     android:layout_marginTop="20dp" 
     android:background="@color/btn_magenta" 
     android:text="@string/txt_done" 
     android:textColor="@android:color/white" /> 


</LinearLayout> 

Если я использую андроид: minLines = "5" в EditText только тогда дополнительное пространство появляется в EditText даже есть нет текста

Проблема Скриншот Problem Images

Ожидаемый результат Скриншот Expected Result

+1

после установки 'maxLines()' программно, вызовите 'edittext.invalidate();' –

+1

попробуйте использовать 'setLines (5)' вместо 'setMaxLines (5)' –

ответ

1

набор андроида: inputType = "текст" в вашем XML и удалить андроида: minLines = "5".

4

Вы можете сделать editext отзывчивым мой, используя следующий код в

android:inputType="textMultiLine" 
android:layout_height="wrap_content" 

и удалить

android:minLines="5" 

С уважением.

0

Вы должны удалить строку edtDescription.setSingleLine(false); с вашего кода. Он должен работать. Ищите деталь ответа here.

Смежные вопросы