Kou*_*lya 8 android android-studio
我为 EditText 设置可绘制的权利,如下所示,
editText.setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_clear_black_24dp), null);
Run Code Online (Sandbox Code Playgroud)
我正在 xml 中为 EditText 设置可绘制左侧。我想将其可见性设置为可见或隐藏。我如何以编程方式执行此操作。
我有用于搜索的 EditText。当开始打字时,我正在以编程方式设置清晰的图标。
清除图标将清除 EditText 中的文本。当单击没有文本的清晰图标时,我想关闭键盘并隐藏清晰图标。下面是我的代码,
editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setCursorVisible(true);
editText.setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_clear_black_24dp), null);
}
});
editText.setOnTouchListener(new View.OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
if(editText.getCompoundDrawables()[2]!=null){
if(event.getX() >= (editText.getRight()- editText.getLeft() - editText.getCompoundDrawables()[2].getBounds().width())) {
if(!editText.getText().toString().equals("")) {
editText.setText("");
}
else {
// getWindow().setSoftInputMode(
// WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
closeKeyboard();
editText.setCursorVisible(false);
}
}
}
}
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
我想以编程方式隐藏它。
只需传入null方法setCompoundDrawablesWithIntrinsicBounds()即可对您隐藏 DrawableeditText
示例代码
editText.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
Run Code Online (Sandbox Code Playgroud)
请按onTouchListener如下方式更改您的。我们不再需要捕获ACTION_UPgo for ,因为它将首先从那里ACTION_DOWN调用,因为现在我们不需要关闭键盘。onTouchreturn trueonClick
editText.setOnTouchListener(new View.OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
if(editText.getCompoundDrawables()[2]!=null){
if(event.getX() >= (editText.getRight()- editText.getLeft() - editText.getCompoundDrawables()[2].getBounds().width())) {
if(!editText.getText().toString().equals("")) {
editText.setText("");
}
else {
// getWindow().setSoftInputMode(
// WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
editText.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
closeKeyboard();
editText.setCursorVisible(false);
return true;
}
}
}
}
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8882 次 |
| 最近记录: |