gpt4 book ai didi

java - 在用户输入时更改 TextInputLayout 的提示值

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:56:01 25 4
gpt4 key购买 nike

TextInputEditText

这是我在 TextInputLayout 中的 TextInputEditText。您可以看到的提示是在 TextInputLayout 上设置的。

用户必须写255 个字符,否则帖子无效。

我希望数字 255 随着用户键入而减少,当达到 0 时,提示应为空白。

这是我的代码:

private int charactersLeft = 256;onCreate 方法之外声明。

这段代码在 onCreate 方法中:

final TextInputLayout postTextInputBase = findViewById(R.id.post_text_input_base);

final TextInputEditText postTextInput = findViewById(R.id.post_text_input);

postTextInput.addTextChangedListener(new TextWatcher() {

@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 (charactersLeft == 0) {
postTextInputBase.setHint(null);
} else {
postTextInputBase.setHint((charactersLeft - 1) + " characters left");
}
}

@Override
public void afterTextChanged(Editable s) {

}
});

我还尝试将 charactersLeft 设置为 characterLeft -= 1 但是,java 不允许我这样做。

那么,我该怎么做呢?

最佳答案

使用 app:counterEnabled="true"

  • 是否在此布局中启用了字符计数器功能。

也可以使用 app:counterMaxLength="250"

  • 设置在字符计数器上显示的最大长度。

试试这个

<android.support.design.widget.TextInputLayout
android:id="@+id/post_text_input_base"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:counterEnabled="true"
app:counterMaxLength="250">

<EditText
android:id="@+id/post_text_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="UserName" />
</android.support.design.widget.TextInputLayout>

输出

enter image description here

编辑

public class RecyclerViewActivity extends AppCompatActivity {

TextInputLayout postTextInputBase;
EditText postTextInputEditText;

int textLength = 9;
int len = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_view);

postTextInputBase = findViewById(R.id.post_text_input_base);

postTextInputEditText = findViewById(R.id.post_text_input);

postTextInputBase.setHint(textLength + " characters left");

postTextInputEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

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

// first check your EditText is empty or not
if (!TextUtils.isEmpty(postTextInputEditText.getText().toString())) {

int len = postTextInputEditText.getText().toString().length();
// than check that editText text length is less than max length or not
// for test case i have set max length 10
if (len < 9) {
// if length of text is less than max length than use minus one from max length and set error in your text input layout
textLength--;
} else {
textLength++;

}

postTextInputBase.setHint(textLength + " characters left");
} else {
textLength = 9;
}
}

@Override
public void afterTextChanged(Editable s) {

if(postTextInputEditText.getText().toString().isEmpty()){
textLength = 9;
postTextInputBase.setHint(textLength + " characters left");
}
}
});


}


}

输出

enter image description here

EDIT 2 如果你需要设置提示而不是使用这个

postTextInputBase.setHint(textLength + " characters left");

输出

enter image description here

NOTE Don't forgot to set android:maxLength="10" in edittext

关于java - 在用户输入时更改 TextInputLayout 的提示值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52075467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com