如何在编辑文本框的右下角显示编辑文本字符限制??下面的例子

How show editText character limit in buttom right corner of editText box?? Example below

上图显示了一个编辑文本,其文本限制显示在右下角 250 个字符。当用户在 editText 中键入字符时,“250”会减少。

我一直在尝试将其应用到我的编辑文本中,但这有点具有挑战性。我尝试过各种东西。 只是想让你知道我正在使用一个 alertDialog,它里面有 editText。 alertDialog 然后显示一个 xml,其中包含 editText 属性。

感谢帮助!!!

android material 这很容易。希望对您有所帮助:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/textField"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:counterEnabled="true" --this makes counter enable
    app:counterMaxLength="20" -- this is the limittion 
    android:hint="@string/label">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    />

</com.google.android.material.textfield.TextInputLayout>

或者如果您只想要一个 EditeText,您可以使用 TextWatcher:

private TextView mTextView;
private EditText mEditText;
mTextView.setText("250");
private final TextWatcher mTextEditorWatcher = new TextWatcher() {
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
           //This sets a textview to the current length
           mTextView.setText(String.valueOf(250-s.length()));
        }

        public void afterTextChanged(Editable s) {
        }
}; 


mEditText.addTextChangedListener(mTextEditorWatcher);
//xml 

     <EditText
                    android:id="@+id/et_comment"
                    style="@style/BusinessEditText"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/margin_160"
                    android:layout_marginTop="@dimen/margin_10"
                    android:background="@drawable/bg_border_comment"
                    android:gravity="top"
                    android:hint="Type here - Include your Order ID if it is connected to your order."
                    android:maxLength="500"
                    android:textColor="@color/black"
                    android:textColorHint="@color/text_gray"
                    android:textSize="16sp" />
    
                <TextView
                    android:id="@+id/tv_count"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="0/500"
                    android:layout_gravity="end" />



// inside java class            
        
        @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        
            }
        
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
        
                if (count == 0) {
                    currentLine = currentLine - before;
                } else {
                    currentLine = currentLine + count;
                }
                Log.d("new line ", currentLine + "");
        
                dataBiding.tvCount.setText(currentLine+"/500");
        
        
            }
        
            @Override
            public void afterTextChanged(Editable s) {
        
            }