首页 > 解决方案 > 如何在android上的edittext上设置最大长度消息

问题描述

我想要做的是在编辑文本上设置按摩或最大字符。到目前为止,我已经找到了 TextInputLayout,但这并不是我所需要的。编辑文本是一个很大的字段,所以我试图在编辑文本的右下角设置一条消息,上面写着“最大输入 120”,但在编辑文本内部,而不是在外部。这是我到目前为止所拥有的

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/til_title"
        android:layout_width="match_parent"
        android:layout_height="350dp"
        app:counterEnabled="true"
        app:counterMaxLength="120"
        >
    <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/edtComnent"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:hint="write your comments here"
            android:textSize="@dimen/font_size_2"
            android:gravity="top"
            android:padding="15dp"
            android:maxLength="120"/>
</com.google.android.material.textfield.TextInputLayout>

但就像我说的,它不太符合我的需求。任何帮助或建议都会很棒,谢谢

标签: androidlayoutandroid-edittext

解决方案


我认为,我们在android中没有内置系统。我可以建议的一件事是创建一个 TextView,它可以在 EditText 上随心所欲地保留。在代码中,实现 TextWatcher 以计算键入的字符数并相应地更新计数器 TextView。

您可以执行以下操作:您可以根据需要调整文本大小、边距、填充。这只是一个想法。

<EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:paddingRight="15dp"
        />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="@id/editText"
        app:layout_constraintEnd_toEndOf="@id/editText"
        android:text="0"
        android:textSize="10sp"
        android:layout_marginBottom="10dp"
        android:layout_marginRight="5dp"/>

在代码中:如果要显示键入的字符数,请使用以下代码:

editText = findViewById(R.id.editText)
textView = findViewById(R.id.textView)

var characterCount = 0

editText.addTextChangedListener(object : TextWatcher{
    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

    }

    override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
        characterCount++
        textView.text = characterCount.toString()
    }

    override fun afterTextChanged(p0: Editable?) {

    }

})

结果:

点击查看截图

或者 如果你想显示像'max:300'这样的文本:

editText = findViewById(R.id.editText)
textView = findViewById(R.id.textView)

var textToShow = "max : 300"

editText.addTextChangedListener(object : TextWatcher{
    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
        textView.text = textToShow
        //Here you might need to set up the padding for edittext accordingly
        //so as not to be typed over the text 'max : 300'
    }

    override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
        
    }

    override fun afterTextChanged(p0: Editable?) {

    }

})

或者 ,如果您想显示如下内容:剩余 53 个字符

editText = findViewById(R.id.editText)
textView = findViewById(R.id.textView)

var maxAllowed = 300
var characterCount = 0

editText.addTextChangedListener(object : TextWatcher{
    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
       
    }

    override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
         characterCount++
         var characterRemaining = maxAllowed - characterCount
         textView.text = "$characterRemaining characters remaining"
    }

    override fun afterTextChanged(p0: Editable?) {

    }

})

推荐阅读