如何在按下第一个字符后显示 "Clear button" 并在文本为“”时隐藏它?
How to show a "Clear button" after the first character pressed and hide it when the text is ""?
我需要在编辑文本的右侧显示一个清除按钮,并在文本为“”时隐藏它。我该怎么做。
我只需要知道当文本长度>0或=0时如何显示和隐藏,仅此而已。
设置它的可见性从 xml。
If(editText1.getText.toString.length>0)
{
if(button1.getVisibility == View.GONE)
{
button1.setVisibility(View.VISIBLE)
}
}
else
{
if(button1.getVisibility == View.VISIBLE)
{
button1.setVisibility(View.GONE)
}
}
试试这个代码
Field1.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {}
@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(s.length() != 0)
// set the visibility
}
});
您可以使用 TextWatcher
,一种监听器类型:
yourEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
//Here, you can check for text size...
int length = editText.toString().length();
[...]
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
}
}
摘自 this question.
Here就是你的答案。
使用代码并通过检查字符长度在 textchange 上打开和关闭按钮可见性。
看看下面的代码,它可能对你有帮助:
EditText etSearch = (EditText) view.findViewById(R.id.etSearch);
etSearch.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) {
String str = etSearch.getText().toString();
if (str.isEmpty()) {
btnClear.setVisibility(View.INVISIBLE);
} else {
btnClear.setVisibility(View.VISIBLE);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
文本观察器可能适合您
If(editText1.getText.toString.length>0)
{
if(button1.getVisibility == View.GONE)
{
button1.setVisibility(View.VISIBLE)
}
}
else
{
if(button1.getVisibility == View.VISIBLE)
{
button1.setVisibility(View.GONE)
}
}
我需要在编辑文本的右侧显示一个清除按钮,并在文本为“”时隐藏它。我该怎么做。
我只需要知道当文本长度>0或=0时如何显示和隐藏,仅此而已。
设置它的可见性从 xml。
If(editText1.getText.toString.length>0)
{
if(button1.getVisibility == View.GONE)
{
button1.setVisibility(View.VISIBLE)
}
}
else
{
if(button1.getVisibility == View.VISIBLE)
{
button1.setVisibility(View.GONE)
}
}
试试这个代码
Field1.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {}
@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(s.length() != 0)
// set the visibility
}
});
您可以使用 TextWatcher
,一种监听器类型:
yourEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
//Here, you can check for text size...
int length = editText.toString().length();
[...]
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
}
}
摘自 this question.
Here就是你的答案。
使用代码并通过检查字符长度在 textchange 上打开和关闭按钮可见性。
看看下面的代码,它可能对你有帮助:
EditText etSearch = (EditText) view.findViewById(R.id.etSearch);
etSearch.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) {
String str = etSearch.getText().toString();
if (str.isEmpty()) {
btnClear.setVisibility(View.INVISIBLE);
} else {
btnClear.setVisibility(View.VISIBLE);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
文本观察器可能适合您
If(editText1.getText.toString.length>0)
{
if(button1.getVisibility == View.GONE)
{
button1.setVisibility(View.VISIBLE)
}
}
else
{
if(button1.getVisibility == View.VISIBLE)
{
button1.setVisibility(View.GONE)
}
}