在Android的一个编辑框标签 [英] Label in a editbox in android
问题描述
我的问题是,如何把一个标签中的编辑框在Android的?
My question is, how to put a label in a editBox in android ?
例如像,我想提出若要:在编辑框如果我$屏幕键盘页上$ PSS退格,将不会被删除,即使联系人选择器
Like for example, i want to put "To:" in editbox of a contact picker which will not get deleted even if I press backspace on the onscreen keyboard.
我试着用机器人:提示,但它被删除时,编辑框是焦点或点击
I tried with android:hint, but it gets deleted when the editBox is focus or clicked.
我试着用图像,但它没有看起来很不错。 所以,我需要,我可以实现这个标签的东西的方法。
I tried with image but it's not looking good. So, I need a method by which i can implement this label thing.
请参阅视觉图
See the visual diagram
推荐答案
我给你两个思路来做到这一点:
I give you two ideas to do this :
如果您只需要在这几个地方,你可以使用的FrameLayout /合并有一个TextView在你的EditText。然后,使用在编辑文本填充,你可以把它看起来像TextView的是内部的EditText。
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>
否则,你可以通过编写自己的类inplement你自己的版本的EditText,的。这里有一个基本的例子,你需要调整它一点:
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;
}
这篇关于在Android的一个编辑框标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!