Label in a editbox in android

前端 未结 9 2285
渐次进展
渐次进展 2020-12-14 10:41

My question is, how to put a label in a editBox in android ?

Like for example, i want to put \"To:\" in editbox of a contact picke

相关标签:
9条回答
  • 2020-12-14 11:16

    Then use this...

    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
    
    
            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TO:"    
                />
        </LinearLayout>
    
    0 讨论(0)
  • 2020-12-14 11:19

    I give you two ideas to do this :

    If you only need this in a couple of places, you can use a FrameLayout / merge to have a TextView over your EditText. Then using a padding on the edit text, you can make it seem like the TextView is "inside" the EditText. :

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    
        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="40dp" >
    
            <requestFocus />
        </EditText>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:paddingLeft="10dp"
            android:text="To : " />
    </FrameLayout>
    

    Else you can inplement your own version of EditText, by writing your own Class. Here's a basic example, you'd need to tweak it a little :

    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Paint;
    import android.graphics.Rect;
    import android.text.TextPaint;
    import android.util.AttributeSet;
    import android.widget.EditText;
    
    public class LabelledEditText extends EditText {
    
        public LabelledEditText(Context context) {
            super(context);
            mPaddingLeft = getPaddingLeft();
        }
    
        public LabelledEditText(Context context, AttributeSet attrs) {
            super(context, attrs);
            mPaddingLeft = getPaddingLeft();
        }
    
        protected void onDraw(Canvas canvas) {
            TextPaint textPaint = getPaint();
            Rect size = new Rect();
            textPaint.getTextBounds(mLabel, 0, mLabel.length(), size);
            setPadding(mPaddingLeft + size.width(), getPaddingTop(), getPaddingRight(), getPaddingBottom());
            super.onDraw(canvas);
    
            canvas.drawText(mLabel, mPaddingLeft + size.left, size.bottom + getPaddingTop(), textPaint);
        }
    
    
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    
        private String mLabel = "To :  ";
        private int mPaddingLeft;
    
    }
    
    0 讨论(0)
  • 2020-12-14 11:20

    Try this

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp" >
    
        <RelativeLayout
            android:id="@+id/linearlayout2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:text="To:" />
    
            <EditText
                android:id="@+id/editTextTo"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="20dp"
                android:background="@android:color/transparent"
                android:hint="Type your text..."
                android:singleLine="true"
                android:textColor="#000000" />
        </RelativeLayout>
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-14 11:20
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TO:"    
            />
    
    0 讨论(0)
  • 2020-12-14 11:21

    You can add like this

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_screen);
    
    EditText edittext = (EditText)getViewById(R.id.edittextid); 
    edittext.setText("To:") 
    }
    

    Then you will see the text when ever you starting the activity.

    I hope it helps....

    0 讨论(0)
  • 2020-12-14 11:21

    You can use Here FrameLayout, Within which you can use 3 widgets :

    1.TextView with set Text "TO"

    1. Next to TextView You can have Edittext where you can enter the Data

    2. Last is ImageButton Showing Cross Sign...

    Set Background image of the FrameLayout with rounded rectangle,,,,

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