2016-11-11 2 views
2

Ich versuche Maxline für EditText programmatic zu setzen, es gibt keinen zusätzlichen Platz auf EditText nach dem Löschen von Text. Wenn ich maxLine in XML einstelle, ist der zusätzliche Platz sichtbar, auch wenn in EditText kein Text vorhanden ist.EditText.setMaxLines() programmatisch nicht funktioniert

Wie geben Sie diese zusätzlichen Speicherplatz auf programmatische Weise anstelle von XML. weil ich dieselbe XML für viele Ansichten verwenden muss.

Programmatische Code

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-Code

<?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> 

Wenn ich Android verwenden: minLines = "5" in EditText dann nur zusätzlichen Platz erscheint in EditText auch dort ist kein Text

Problem Screenshot Problem Images

Erwartetes Ergebnis Screenshot Expected Result

+1

Nachdem Sie 'maxLines()' programmatisch gesetzt haben, rufen Sie 'edittext.invalidate();' –

+1

mit 'setLines (5) 'anstelle von' setMaxLines (5) 'auf –

Antwort

1

Satz android: input = "text" in Ihrem xml und entfernen android: minLines = "5".

4

Sie können Ihre EditExt reagiert, meine mit dem folgenden Code in Ihrer

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

machen und

android:minLines="5" 

Grüße entfernen.

0

Sie sollten die Zeile edtDescription.setSingleLine(false); aus Ihrem Code entfernen. Es sollte funktionieren. Suchen Sie nach Details der Antwort here.

Verwandte Themen