Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

自定義一個會提示剩餘字數的 EditText

編輯:關於 Android 程式設計

最近專案有個需求:即時提示使用者還能打幾個字,也就是要監聽 EditText 的文字變化。功能本身不難,但每次都用組合方式重寫一遍實在麻煩,於是就把它包成一個自定義控件,以後直接引用。

先看效果:

自定義控件的佈局

這裡採用「組合控件」的做法:一個 RelativeLayout 裡放 EditText,右下角疊一個 TextView 顯示提示文字。

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#fff" >

    <EditText
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="top|left"
        android:minLines="5" >

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/edit"
            android:layout_alignRight="@id/edit"
            android:layout_marginBottom="3dip"
            android:layout_marginRight="3dip"
            android:hint="還可輸入25字"
            android:textSize="10dip" />
    </EditText>
</RelativeLayout>

HintEditText.java

package com.gc.testcustomedittext;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class HintEditText extends RelativeLayout implements TextWatcher {

    private EditText mEditText;
    private TextView mTextView;
    private int maxLength = 0;
    private RelativeLayout mRelativeLayout;

    public HintEditText(Context context) {
        super(context);
    }

    public HintEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        // 讀出自定義屬性 maxLength
        TypedArray mTypedArray = context.obtainStyledAttributes(
                attrs, R.styleable.HintEditText);
        maxLength = mTypedArray.getInt(R.styleable.HintEditText_maxLength, 0);

        mRelativeLayout = (RelativeLayout) LayoutInflater.from(context)
                .inflate(R.layout.custom_edittext, this, true);
        mEditText = (EditText) mRelativeLayout.findViewById(R.id.edit);
        mTextView = (TextView) mRelativeLayout.findViewById(R.id.text);
        mTextView.setHint("還可輸入" + maxLength + "字");

        // 限制最多可輸入的字元數
        mEditText.setFilters(new InputFilter[]{
                new InputFilter.LengthFilter(maxLength)});
        mEditText.addTextChangedListener(this);
    }

    @SuppressLint("NewApi")
    public HintEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // 文字一變,就刷新剩餘字數提示
        mTextView.setHint("還可輸入" + (maxLength - s.toString().length()) + "字");
    }

    @Override
    public void afterTextChanged(Editable s) {
    }
}

res/values/attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="HintEditText">
        <!-- maxLength 為自定義屬性,宣告後即可在 xml 中直接設定 -->
        <attr name="maxLength" format="integer" />
    </declare-styleable>
</resources>

主佈局 activity_main.xml

使用自定義屬性時,記得在根節點宣告命名空間:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:hintedittext="https://schemas.android.com/apk/res/com.gc.testcustomedittext"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.gc.testcustomedittext.HintEditText
        android:id="@+id/hint_edt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        hintedittext:maxLength="30" />

    <com.gc.testcustomedittext.HintEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        hintedittext:maxLength="50" />
</LinearLayout>

MainActivity.java

package com.gc.testcustomedittext;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

因為邏輯都封裝在控件內部,Activity 這邊幾乎不需要寫任何程式碼——這正是組合式自定義控件的價值所在。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved