Android开发,下面这个EditText右下角的文字如何实现?

Android开发,下面这个EditText右下角的文字如何实现?,第1张

可以使用相对布局

大概的XML布局代码

<RelativeLayout

    android:layout_width="match_parent"

    android:layout_height="200dp">

    <EditText

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:gravity="top"

        android:hint="你想说的是..."

        android:minLines="5"/>

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentBottom="true"

        android:layout_alignParentRight="true"

        android:layout_marginRight="20dp"

        android:layout_marginBottom="20dp"

        android:text="0/25"/>

</RelativeLayout>

在EditText内部显示提示文字,这部分被锁,不可删除或修改,文字在后面输入。

1.

一般可透过android:drawableLeft属性来实现。首先做一个带有提示文字的图片,即提示文字当作图片来显示,透过android:drawableLeft指定资源的ID。这种方法不灵活,不可随意更改提示文字,需要更换图片。

2.

2.透过android:paddingLeft属性和Canvas来实现。具体是:编写一个类继承自EditText,覆盖其中的onDarw()方法,在该方法中编写提示文字。

3.

protected

void

onDraw(Canvas

canvas){

4.

Paint

paint

=

new

Paint()

5.

paint.setTestSize(18)

6.

paint.setColor(Color.GRAY)

7.

//编写提示文字。

8.

canvas.drawText(“提示文字:”,2,getHeight()/2+5,paint);

9.

super.onDraw(canvas)

10.

}

编写完提示文字后,在提示文字后面输入文字,需要使用android:paddingLeft属性根据提示文字的宽度设定开始输入文字的位置。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11451491.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存