Android Study 之分分钟让你玩转EditText右下角实时显示输入字数

LZ-Say:有时候觉得,开发真心不容易。想做一个好的开发,不仅仅会敲代码,造轮子,更多个人觉得调整心态,毕竟人和人是不一样的。。。心塞

前言

今天为大家带来一个简单的小玩意,没什么技术含量,做这个的初衷是个人嫌弃UI给的设计图,另一方便是希望app能更人性化,大家可以一起来看下UI给的图。

这里写图片描述

大体一看,大家可能会说,没啥毛病啊,不就是一个输入框么?

是的,如果按照我之前的想法,我个人是绝对会老老实实按照UI给定的图来,但是经过一些事之后,我却不这么想了。

那么,我们看看,这个东西是不是缺点啥呢?

有的兄弟们就说了,在右下角加一个显示字数的呗。

嘿嘿,,,说干就干~

简单分析及Coding

干之前,我们先来简单分析下我们要做的东西,先给大家简单画个效果—江湖人称UI设计图~

这里写图片描述

如果最多用户只能输入140个字符,并且当输入字符个数等于140个时,提示一下。

实现这个,主要分以下几步:

1. 首先编写一个shape文件,这里面当然要指定圆角弧度以及边框颜色宽度;

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

    <stroke
        android:width="@dimen/dp_1"
        android:color="@color/color_c9"/>

    <corners android:radius="@dimen/dp_3"/>

</shape>

2. 编写我们布局文件。内容为:相对布局中包含EditText以及TextView,具体如下:

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/shape_circle_while_bg">

            <EditText
                android:id="@+id/id_editor_detail"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:background="@null"
                android:gravity="top"
                android:hint="@string/string_editor_detail_hint"
                android:maxLength="140"
                android:minLines="6"
                android:padding="@dimen/dp_10"
                android:textColor="@color/color_c6"
                android:textColorHint="@color/color_c9"
                android:textSize="@dimen/sp_14"/>

            <TextView
                android:id="@+id/id_editor_detail_font_count"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/id_editor_detail"
                android:paddingBottom="@dimen/dp_5"
                android:paddingRight="@dimen/dp_15"
                android:text="@string/string_editor_detail_default_font"
                android:textColor="@color/color_c9"
                android:textSize="@dimen/sp_14"/>
        </RelativeLayout>

3.activity逻辑校验

由于LZ项目中使用的是黄油刀,下面就直接从项目拷贝了~

有兴趣的同志可以看看之前写的有关黄油刀基本使用,地址如下:

Android Study 之 初识ButterKnife(8.5.1)及简单运用

    @OnTextChanged(value = R.id.id_editor_detail, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    public void editTextDetailChange(Editable editable) {
        int detailLength = editable.length();
        idEditorDetailFontCount.setText(detailLength + "/140");
        if (detailLength == 139) {
            islMaxCount = true;
        }
        // 不知道为什么执行俩次,所以增加一个标识符去标识
        if (detailLength == 140 && islMaxCount) {
            UIHelper.getShortToast(self, (String) StringUtils.getResourceContent(self, Convention.RESOURCE_TYPE_STRING, R.string.string_editor_detail_input_limit));
            islMaxCount = false;
        }
    }

4.来来来,一起看效果~

4.1 当用户输入时:

这里写图片描述

4.2 当用户删除时:

这里写图片描述

4.3 当输入达到上限时:

这里写图片描述

结束

基本介绍到此结束~

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 14
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HLQ_Struggle

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值