这是我TextInputEditText
中的TextInputLayout
。您可以看到的提示已设置在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
不允许我这样做。
然后,我该怎么做?
答案 0 :(得分:4)
尝试一下
<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>
输出
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");
}
}
});
}
}
输出
postTextInputBase.setHint(textLength + " characters left");
输出
注意,不要忘记在edittext中设置
android:maxLength="10"
答案 1 :(得分:3)
尝试一下。使用您现有的代码。
int charactersLeft = 256;
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) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.length() == 0) {
postTextInputBase.setHint(null);
}else {
postTextInputBase.setHint((charactersLeft - s.length()) + " characters left");
}
}
});
并将android:maxLength="256"
添加到您的TextInputEditText
答案 2 :(得分:1)
您可以在编辑文本上使用setOnKeyListener
以编程方式进行操作。
TextInputLayout postTextInputBase = findViewById(R.id.post_text_input_base);
TextInputEditText postTextInput = findViewById(R.id.post_text_input);
postTextInput.setOnKeyListener((v, keyCode, event) -> {
// Do other things needed
postTextInputBase.setHint("Characters left: " + (256 - postTextInput.getText().length()));
return false;
});
答案 3 :(得分:1)
要仅更改提示文本,只需将焦点更改即可。
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
editText.setHint("Enter name");
else
editText.setHint("Name");
}
});