如何解决如何使用十字x按钮创建EditText?
| 是否有像EditText
这样的小工具包含一个十字按钮,或者是否有EditText
的任何属性可以自动创建?我希望十字按钮删除任何用“ 0”写的文本。
解决方法
使用以下布局:
<FrameLayout
android:layout_width=\"fill_parent\"
android:layout_height=\"wrap_content\"
android:layout_marginTop=\"9dp\"
android:padding=\"5dp\">
<EditText
android:id=\"@+id/calc_txt_Prise\"
android:layout_width=\"fill_parent\"
android:layout_height=\"wrap_content\"
android:inputType=\"numberDecimal\"
android:layout_marginTop=\"20dp\"
android:textSize=\"25dp\"
android:textColor=\"@color/gray\"
android:textStyle=\"bold\"
android:hint=\"@string/calc_txt_Prise\"
android:singleLine=\"true\" />
<Button
android:id=\"@+id/calc_clear_txt_Prise\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:layout_marginRight=\"10dp\"
android:layout_gravity=\"right|center_vertical\"
android:background=\"@drawable/delete\" />
</FrameLayout>
您还可以使用按钮的ID并对其onClickListener方法执行所需的任何操作。
, 如果您碰巧使用了DroidParts,那么我刚刚添加了ClearableEditText。
这是从ActionBarSherlock设置为ѭ4clear的自定义背景和清除图标的外观:
, 这是kotlin解决方案。将此辅助方法放在一些kotlin文件中-
fun EditText.setupClearButtonWithAction() {
addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(editable: Editable?) {
val clearIcon = if (editable?.isNotEmpty() == true) R.drawable.ic_clear else 0
setCompoundDrawablesWithIntrinsicBounds(0,clearIcon,0)
}
override fun beforeTextChanged(s: CharSequence?,start: Int,count: Int,after: Int) = Unit
override fun onTextChanged(s: CharSequence?,before: Int,count: Int) = Unit
})
setOnTouchListener(View.OnTouchListener { _,event ->
if (event.action == MotionEvent.ACTION_UP) {
if (event.rawX >= (this.right - this.compoundPaddingRight)) {
this.setText(\"\")
return@OnTouchListener true
}
}
return@OnTouchListener false
})
}
然后按照onCreate
方法中的方法使用它,那么您应该很好用-
yourEditText.setupClearButtonWithAction()
顺便说一句,您必须首先添加R.drawable.ic_clear
或清除图标。这是来自google- https://material.io/tools/icons/?icon=clear&style=baseline
, 通过适用于Android的Material Components解决方案的2019年解决方案:
将材料组件添加到gradle设置中:
implementation \'com.google.android.material:material:1.1.0-alpha09\'
或者,如果您尚未更新为使用AndroidX库,则可以通过以下方式添加它:
implementation \'com.android.support:design:28.0.0\'
然后
<com.google.android.material.textfield.TextInputLayout
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:hint=\"@string/hint_text\"
app:endIconMode=\"clear_text\">
<com.google.android.material.textfield.TextInputEditText
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"/>
</com.google.android.material.textfield.TextInputLayout>
注意:app:endIconMode = \“ clear_text \”
如此处所述材料设计文档
, Android的支持库有一个SearchView
类来完成此任务。 (尽管不是从ѭ0派生的,所以必须使用SearchView.OnQueryTextListener
而不是TextWatcher
)
像这样在XML中使用:
<android.support.v7.widget.SearchView
android:id=\"@+id/searchView\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:iconifiedByDefault=\"false\"
android:queryHint=\"@string/SearchHint\"
app:iconifiedByDefault=\"false\"
app:queryHint=\"@string/SearchHint\" />
, Drawable x = getResources().getDrawable(R.drawable.x);
x.setBounds(0,x.getIntrinsicWidth(),x.getIntrinsicHeight());
mEditText.setCompoundDrawables(null,null,x,null);
其中,x为:
, 对于drawable resource
,您可以使用标准的android图像:
http://androiddrawables.com/Menu.html
例如 :
android:background=\"@android:drawable/ic_menu_close_clear_cancel\"
, 如果您不想使用自定义视图或特殊布局,则可以使用9-patch制作(X)按钮。
示例:http://postimg.org/image/tssjmt97p/(我的积分不足,无法在StackOverflow上发布图像)
右侧和底部黑色像素的交集表示内容区域。该区域之外的任何内容都在填充。因此,要检测用户是否单击了x,可以像这样设置OnTouchListener:
editText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view,MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_UP){
if (motionEvent.getX()>(view.getWidth()-view.getPaddingRight())){
((EditText)view).setText(\"\");
}
}
return false;
}
});
根据您的需求,该解决方案在某些情况下可以更好地工作。我宁愿保持xml的简单性。如果您想在左侧有一个图标,这也很有帮助,因为您只需将其包含在9补丁中即可。
, 我做了如下的UI部分:
<RelativeLayout
android:layout_width=\"fill_parent\"
android:layout_height=\"50dp\"
android:layout_marginTop=\"9dp\"
android:padding=\"5dp\">
<EditText
android:id=\"@+id/etSearchToolbar\"
android:layout_width=\"fill_parent\"
android:layout_height=\"match_parent\"
android:textSize=\"13dp\"
android:padding=\"10dp\"
android:textColor=\"@android:color/darker_gray\"
android:textStyle=\"normal\"
android:hint=\"Search\"
android:imeOptions=\"actionSearch\"
android:inputType=\"text\"
android:background=\"@drawable/edittext_bg\"
android:maxLines=\"1\" />
<ImageView
android:id=\"@+id/ivClearSearchText\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:layout_centerVertical=\"true\"
android:layout_marginRight=\"6dp\"
android:src=\"@drawable/balloon_overlay_close\"
android:layout_alignParentRight=\"true\"
android:layout_alignParentEnd=\"true\" />
</RelativeLayout>
edittext_bg.xml
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<solid android:color=\"#FFFFFF\" />
<stroke
android:width=\"1dp\"
android:color=\"#C9C9CE\" />
<corners
android:bottomLeftRadius=\"15dp\"
android:bottomRightRadius=\"15dp\"
android:topLeftRadius=\"15dp\"
android:topRightRadius=\"15dp\" />
Balloon_overlay_close.png
十字/清除按钮隐藏/显示:
searchBox.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence,int i,int i1,int i2) {}
@Override
public void onTextChanged(CharSequence charSequence,int i2) {
if(charSequence.length() > 0){
clearSearch.setVisibility(View.VISIBLE);
}else{
clearSearch.setVisibility(View.GONE);
}
}
@Override
public void afterTextChanged(Editable editable) {}
});
处理搜索内容(即,当用户单击软键盘上的搜索时)
searchBox.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v,int actionId,KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
String contents = searchBox.getText().toString().trim();
if(contents.length() > 0){
//do search
}else
//if something to do for empty edittext
return true;
}
return false;
}
});`
清除/交叉按钮
clearSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
searchBox.setText(\"\");
}
});
, 采用
android:drawableRight=\"@android:drawable/ic_input_delete\"
, 这是带有小部件的完整库:
https://github.com/opprime/EditTextField
要使用它,您应该添加依赖项:
compile \'com.optimus:editTextField:0.2.0\'
在layout.xml文件中,您可以使用小部件设置:
xmlns:app=\"http://schemas.android.com/apk/res-auto\"
app:clearButtonMode,可以具有以下值:
决不
总是
在编辑时
除非编辑
app:clearButtonDrawable
实际示例:
, 只需在您的EditText
中像drawableEnd
那样交叉放置:
<EditText
...
android:drawableEnd=\"@drawable/ic_close\"
android:drawablePadding=\"8dp\"
... />
并使用扩展名处理点击(或直接在EditText
上使用OnTouchListener
):
fun EditText.onDrawableEndClick(action: () -> Unit) {
setOnTouchListener { v,event ->
if (event.action == MotionEvent.ACTION_UP) {
v as EditText
val end = if (v.resources.configuration.layoutDirection == View.LAYOUT_DIRECTION_RTL)
v.left else v.right
if (event.rawX >= (end - v.compoundPaddingEnd)) {
action.invoke()
return@setOnTouchListener true
}
}
return@setOnTouchListener false
}
}
扩展程序用法:
editText.onDrawableEndClick {
// TODO clear action
etSearch.setText(\"\")
}
, 您可以将此代码段与Jaydip答案一起使用多个按钮。在获得对ET和Button元素的引用后,只需调用它即可。我使用了vecotr按钮,因此您必须将Button元素更改为ImageButton:
private void setRemovableET(final EditText et,final ImageButton resetIB) {
et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v,boolean hasFocus) {
if (hasFocus && et.getText().toString().length() > 0)
resetIB.setVisibility(View.VISIBLE);
else
resetIB.setVisibility(View.INVISIBLE);
}
});
resetIB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
et.setText(\"\");
resetIB.setVisibility(View.INVISIBLE);
}
});
et.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 before,int count) {
if(s.length() != 0){
resetIB.setVisibility(View.VISIBLE);
}else{
resetIB.setVisibility(View.INVISIBLE);
}
}
});
}
, 如果您使用的是框架布局,或者可以创建框架布局,那么我尝试了另一种方法。
<TextView
android:id=\"@+id/inputSearch\"
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:drawableRight=\"@drawable/ic_actionbar\"
android:layout_alignParentBottom=\"true\"
android:layout_toRightOf=\"@+id/back_button\"/>
<Button
android:id=\"@+id/clear_text_invisible_button\"
android:layout_width=\"30dp\"
android:layout_height=\"30dp\"
android:layout_gravity=\"right|center_vertical\"
android:background=\"@color/transparent\"
android:layout_alignBaseline=\"@+id/inputSearch\"
android:layout_alignBottom=\"@+id/inputSearch\"
android:layout_alignRight=\"@+id/inputSearch\"
android:layout_alignEnd=\"@+id/inputSearch\"
android:layout_marginRight=\"13dp\"
/>
这是一个编辑文本,在其中我将一个十字形图标放置为右侧可绘制对象,然后,在UPON中,我放置了一个透明按钮以清除文本。
, <EditText
android:id=\"@+id/idSearchEditText\"
android:layout_width=\"match_parent\"
android:layout_height=\"@dimen/dimen_40dp\"
android:drawableStart=\"@android:drawable/ic_menu_search\"
android:drawablePadding=\"8dp\"
android:ellipsize=\"start\"
android:gravity=\"center_vertical\"
android:hint=\"Search\"
android:imeOptions=\"actionSearch\"
android:inputType=\"text\"
android:paddingStart=\"16dp\"
android:paddingEnd=\"8dp\"
/>
EditText mSearchEditText = findViewById(R.id.idSearchEditText);
mSearchEditText.addTextChangedListener(this);
mSearchEditText.setOnTouchListener(this);
@Override
public void afterTextChanged(Editable aEditable) {
int clearIcon = android.R.drawable.ic_notification_clear_all;
int searchIcon = android.R.drawable.ic_menu_search;
if (aEditable == null || TextUtils.isEmpty(aEditable.toString())) {
clearIcon = 0;
searchIcon = android.R.drawable.ic_menu_search;
} else {
clearIcon = android.R.drawable.ic_notification_clear_all;
searchIcon = 0;
}
Drawable leftDrawable = null;
if (searchIcon != 0) {
leftDrawable = getResources().getDrawable(searchIcon);
}
Drawable rightDrawable = null;
if (clearIcon != 0) {
rightDrawable = getResources().getDrawable(clearIcon);
}
mSearchEditText.setCompoundDrawablesWithIntrinsicBounds(leftDrawable,rightDrawable,null);
}
@Override
public boolean onTouch(View aView,MotionEvent aEvent) {
if (aEvent.getAction() == MotionEvent.ACTION_UP){
if (aEvent.getX() > ( mSearchEditText.getWidth() -
mSearchEditText.getCompoundPaddingEnd())){
mSearchEditText.setText(\"\");
}
}
return false;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。