1

在我的应用程序中,我使用一个类在 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>

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

图片

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

4

0 回答 0