在android的编辑框中添加标签 [英] Label in a editbox in android

查看:59
本文介绍了在android的编辑框中添加标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,如何在 android 的 editBox 中放置标签?

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

例如,我想在联系人选择器的 editbox 中输入To:",即使我按屏幕键盘上的退格键也不会被删除.

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.

我尝试使用 android:hint,但是当编辑框成为焦点或单击时它会被删除.

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.

查看可视化图

推荐答案

我给你两个想法:

如果您只在几个地方需要它,您可以使用 FrameLayout/merge 在您的 EditText 上添加一个 TextView.然后在编辑文本上使用填充,您可以使 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>

否则,您可以通过编写自己的类来实现自己的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆