Display non-editable text at end of EditText in an

2019-05-26 15:47发布

问题:

I want to display text at the end of EditText as shown in the image above for URLName.How could I acheive this ?The text cannot be edited.I am using this editText inside TextInputLayout from the design library. thanks in advance

回答1:

  • Create a LinearLayout. An example of a row inside this layout would be a title (i.e. URL name) or a text entry field.

  • Set the background of the LinearLayout for the current row to a custom drawable to create a thin red line. Follow the answers on this post for ideas.

  • For the URL entry field mentioned in your question, create a TextView and an EditText inside another LinearLayout inside the current row.

The TextView will contain ".sitename.com". Set it to wrap_content and give it a weight of 0. For your EditText, set its width to 0dp and its weight to 1. It should now fill all remaining space. Set the background to @null for both.



回答2:

This worked for me :

<RelativeLayout

        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=""
            android:layout_alignParentLeft="true"
            android:id="@+id/youeeditid"
            android:layout_gravity="center"
            />
    <TextView
            android:id="@+id/text_hint"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="HintText"
            android:textSize="18sp"
            android:layout_marginRight="10dp"
            android:textColor="#808080"
            />
</RelativeLayout>

It's little bit tricky but it's works for me.

BUT

If you want to set only EditText then you can do below :

1) Set Gravity of EditText to "Right" and Ellipsize to "End" .

2) Now In onCreate() method write onclicklistener of EditText and set Gravity as "Left".

3) Or you can also set programatically on OnKeyListener , where check if lenght of edittext is equal to Zero set EditText's gravity as Left.

Reference : Setting cursor to the right on an EditText with HINT Gravity to center