Android - 如何在EditText上显示字符数...?

时间:2016-05-02 10:38:28

标签: java android android-edittext charactercount

在我的应用程序中,我使用类显示TextInputLayout下方的实时字符数。 这是班级

public class CharacterCountErrorWatcher
    implements TextWatcher
{
private final TextInputLayout mTextInputLayout;
private final ForegroundColorSpan mNormalTextAppearance;
private final ForegroundColorSpan mErrorTextAppearance;
/*private final AlignmentSpan mAlignmentSpan = new AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE);*/
private final AlignmentSpan mAlignmentSpan = new AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE);
private final SpannableStringBuilder mErrorText = new SpannableStringBuilder();
private int mMinLen;
private int mMaxLen;

public CharacterCountErrorWatcher(TextInputLayout textInputLayout, int minLen, int maxLen)
{
    mTextInputLayout = textInputLayout;
    mNormalTextAppearance = new ForegroundColorSpan(Color.GRAY);
    mErrorTextAppearance=new ForegroundColorSpan(Color.RED);
    mMinLen = minLen;
    mMaxLen = maxLen;
    updateErrorText();
}

private void updateErrorText()
{
    mErrorText.clear();
    mErrorText.clearSpans();
    final int length = mTextInputLayout.getEditText().length();
    if(length >= 0){
        mErrorText.append(String.valueOf(length));
        mErrorText.append(" / ");
        mErrorText.append(String.valueOf(mMaxLen));
        mErrorText.setSpan(mAlignmentSpan, 0, mErrorText.length(),
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        if(hasValidLength()){
            mErrorText.setSpan(mNormalTextAppearance, 0, mErrorText.length(),
                    Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }
    }
    mTextInputLayout.setError(mErrorText);
}

public boolean hasValidLength()
{
    final int length = mTextInputLayout.getEditText().length();
    return (length >= mMinLen && length <= mMaxLen);
}

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

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

@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
    //

}
}

这就是我在TextInputLayout

上设置这个类的方法
text1.getEditText().addTextChangedListener(new CharacterCountErrorWatcher(text1, 0, 100));

更新
我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:ppv="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:background="@color/logo_background"
tools:context="in.net.spectrum.frankstrade.SellActivity_2">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:title="@string/app_name"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    </android.support.design.widget.AppBarLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="#F7F7F7"
            android:focusableInTouchMode="true"
            android:orientation="vertical"
            android:padding="@dimen/activity_horizontal_margin"
            android:weightSum="2">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:weightSum="2"
                android:layout_marginBottom="10dp"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/imv_camera"
                    android:layout_width="wrap_content"
                    android:layout_height="150dp"
                    android:layout_weight="1"
                    android:src="@drawable/camera" />

                <ImageView
                    android:id="@+id/imv_gallery"
                    android:layout_width="wrap_content"
                    android:layout_height="150dp"
                    android:layout_weight="1"
                    android:src="@drawable/gallery" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">

                <HorizontalScrollView
                    android:id="@+id/scroller"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:orientation="horizontal"
                        android:weightSum="2">

                        <LinearLayout
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginRight="5dp"
                            android:orientation="vertical">

                            <android.support.v7.widget.CardView
                                android:id="@+id/img_cv1"
                                android:visibility="gone"
                                android:layout_width="150dp"
                                android:layout_height="150dp"
                                android:layout_marginBottom="5dp"
                                android:layout_marginTop="5dp"
                                android:background="@color/colorCardView"
                                app:cardCornerRadius="5dp">

                                <ImageView
                                    android:id="@+id/iv_photo1"
                                    android:layout_width="150dp"
                                    android:layout_height="150dp"
                                    android:layout_marginRight="5dp"
                                    android:padding="3dp"
                                    android:background="@drawable/border" />


                            </android.support.v7.widget.CardView>

                            <ProgressBar
                                android:id="@+id/progress"
                                android:layout_width="150dp"
                                android:layout_height="30dp"
                                android:layout_gravity="bottom|center_horizontal"
                                android:indeterminate="true" />
                        </LinearLayout>

                        <LinearLayout
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginRight="5dp"
                            android:orientation="vertical">

                            <android.support.v7.widget.CardView
                                android:id="@+id/img_cv2"
                                android:layout_width="150dp"
                                android:layout_height="150dp"
                                android:layout_marginTop="5dp"
                                android:background="@color/colorCardView"
                                android:visibility="gone">

                                <ImageView
                                    android:id="@+id/iv_photo2"
                                    android:layout_width="150dp"
                                    android:layout_height="150dp"
                                    android:layout_marginRight="5dp"
                                    android:padding="3dp"
                                    android:background="@drawable/border" />

                            </android.support.v7.widget.CardView>

                            <ProgressBar
                                android:id="@+id/progress2"
                                android:layout_width="150dp"
                                android:layout_height="30dp"
                                android:layout_gravity="bottom|center_horizontal"
                                android:indeterminate="true" />
                        </LinearLayout>

                        <LinearLayout
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginRight="5dp"
                            android:orientation="vertical">

                            <android.support.v7.widget.CardView
                                android:id="@+id/img_cv3"
                                android:layout_width="150dp"
                                android:layout_height="150dp"
                                android:layout_marginTop="5dp"
                                android:background="@color/colorCardView"
                                android:visibility="gone">

                                <ImageView
                                    android:id="@+id/iv_photo3"
                                    android:layout_width="150dp"
                                    android:layout_height="150dp"
                                    android:layout_marginRight="5dp"
                                    android:padding="3dp"
                                    android:background="@drawable/border" />

                            </android.support.v7.widget.CardView>

                            <ProgressBar
                                android:id="@+id/progress3"
                                android:layout_width="150dp"
                                android:layout_height="30dp"
                                android:layout_gravity="bottom|center_horizontal"
                                android:indeterminate="true" />
                        </LinearLayout>

                        <LinearLayout
                            android:layout_marginRight="5dp"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:orientation="vertical">

                            <android.support.v7.widget.CardView
                                android:id="@+id/img_cv4"
                                android:layout_width="150dp"
                                android:layout_height="150dp"
                                android:layout_marginTop="5dp"
                                android:background="@color/colorCardView"
                                android:visibility="gone">

                                <ImageView
                                    android:id="@+id/iv_photo4"
                                    android:layout_width="150dp"
                                    android:layout_height="150dp"
                                    android:layout_marginRight="5dp"
                                    android:padding="3dp"
                                    android:background="@drawable/border" />

                            </android.support.v7.widget.CardView>

                            <ProgressBar
                                android:id="@+id/progress4"
                                android:layout_width="150dp"
                                android:layout_height="30dp"
                                android:layout_gravity="bottom|center_horizontal"
                                android:indeterminate="true" />
                        </LinearLayout>

                        <LinearLayout
                            android:layout_marginRight="5dp"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:orientation="vertical">

                            <android.support.v7.widget.CardView
                                android:id="@+id/img_cv5"
                                android:layout_width="150dp"
                                android:layout_height="150dp"
                                android:layout_marginTop="5dp"
                                android:background="@color/colorCardView"
                                android:visibility="gone">

                                <ImageView
                                    android:id="@+id/iv_photo5"
                                    android:layout_width="150dp"
                                    android:layout_height="150dp"
                                    android:layout_marginRight="5dp"
                                    android:padding="3dp"
                                    android:background="@drawable/border" />

                            </android.support.v7.widget.CardView>

                        </LinearLayout>

                        <ProgressBar
                            android:id="@+id/progress5"
                            android:layout_width="150dp"
                            android:layout_height="30dp"
                            android:layout_gravity="bottom|center_horizontal"
                            android:indeterminate="true" />
                    </LinearLayout>
                </HorizontalScrollView>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Add upto 5 images, each of max size 4 MB" />

                <android.support.v7.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:background="@color/colorCardView">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:padding="@dimen/cardView_padding">

                        <TextView
                            android:id="@+id/txt_category"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="15dp"
                            android:text="Select Category : "
                            android:textSize="@dimen/textSize" />

                        <Button
                            android:id="@+id/btn_category"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:textColor="@color/textColorPrimary"
                            android:textStyle="bold"
                            android:background="@drawable/button_background"
                            android:text="Select Category" />

                        <Spinner
                            android:id="@+id/sp_category"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_horizontal"
                            android:layout_marginTop="10dp"
                            android:background="@drawable/spinner_background"
                            android:spinnerMode="dialog" />

                    </LinearLayout>
                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:id="@+id/cv"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:background="@color/colorCardView">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:padding="@dimen/cardView_padding">

                        <android.support.design.widget.TextInputLayout
                            android:id="@+id/input_layout_title"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="10dp">

                            <EditText
                                android:id="@+id/edt_title"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:hint="Title(maximum 100 characters)"
                                android:inputType="textNoSuggestions|textVisiblePassword"
                                android:maxLength="100"
                                android:singleLine="true" />
                        </android.support.design.widget.TextInputLayout>


                        <EditText
                            android:id="@+id/edt_desc"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="10dp"
                            android:focusable="false"
                            android:hint="Description(maximum 5000 characters)"
                            android:singleLine="true" />
                    </LinearLayout>
                </android.support.v7.widget.CardView>


                <CheckBox
                    android:id="@+id/cb_stockimage"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:text="Add this image you own to stock image"
                    android:visibility="gone" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="dummy"
                    android:visibility="invisible" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="dummy"
                    android:visibility="invisible" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="bottom|center_horizontal">

    <Button
        android:id="@+id/btn_next_sa2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="@drawable/button_corners"
        android:text="Next"
        android:textColor="@color/textColorPrimary"
        android:textSize="@dimen/textSize"
        android:textStyle="bold" />
</RelativeLayout>

</FrameLayout>

它工作正常。这是我得到的结果

Image

如您所见,计数显示在EditText下方。但是当弹出软键盘时,用户将无法看到计数。所以我希望出现在EditText 上。我知道可以通过在editText上放置另一个textview并使用TextWatcher来完成。但我想使用这种方法,因为它使我的布局文件几乎可读。我没有真正得到spannable字符串的概念。所以我不知道如何在CharacterCount类中做什么。那么我怎么做才能让计数出现在EditText(TextInputLayout)上......?

0 个答案:

没有答案