Label in a editbox in android

一个人想着一个人 提交于 2019-11-30 04:05:23

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;

}

You could always have a TextView + EditText in a LinearLayout that looks like an EditText like below:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:drawable/edit_text" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="To:" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="3dp"
        android:background="@null" />
</LinearLayout>

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>

In android the label is known as TextView:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

            <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="To:"
            />

            <EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
            />
        </LinearLayout>

EditText has no label so you need both. Thankfully ViewGroups make this relatively painless for the developer. The EditText uses fill_parent attribute so is presented tight to the "To: " label.

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>

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

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

Rounded Text View


<LinearLayout
    android:id="@+id/linearLayoutSearch"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:padding="5dp" >

    <RelativeLayout
        android:id="@+id/linearlayout2"
        android:layout_width="fill_parent"
        android:layout_height="36dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="@drawable/rounded_textview"
        android:paddingLeft="5dp" >

        <TextView
            android:layout_width="22dp"
            android:layout_height="22dp"
            android:layout_centerVertical="true"
            android:alignParentLeft="true"
            android:text="To:" />

        <EditText
            android:id="@+id/search"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="25dp"
            android:background="@android:color/transparent"
            android:hint="Enter your text..."
            android:imeOptions="actionDone"
            android:singleLine="true"
            android:textColor="#000000" />
        <ImageView
            android:layout_width="22dp"
            android:layout_height="22dp"
            android:layout_centerVertical="true"
            android:alignParentRight="true"
            android:src="@drawable/stop" />
    </RelativeLayout>
</LinearLayout>

rounded_textview.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle" >

<solid android:color="#FFFFFF" />

<corners
    android:bottomLeftRadius="15dp"
    android:bottomRightRadius="15dp"
    android:topLeftRadius="15dp"
    android:topRightRadius="15dp" />

</shape>
Anu
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TO:"    
        />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!