LayoutInflater作用及使用--自定义EditText,自带清除内容按钮
2014-08-20 11:07
429 查看
作用:
1、对于一个没有被载入或者想要动态载入的界面, 都需要使用inflate来载入.
2、对于一个已经载入的Activity, 就可以使用实现了这个Activiyt的的findViewById方法来获得其中的界面元素.
举例:定义了一个控件类CleanableEditText,实现在焦点变化时和输入内容发生变化时均要判断是否显示右边clean图标
后台调用自定义控件的时候需要LayoutInflater来载入(见第三段代码)。
自定义控件:
调用xml
后台调用:
1、对于一个没有被载入或者想要动态载入的界面, 都需要使用inflate来载入.
2、对于一个已经载入的Activity, 就可以使用实现了这个Activiyt的的findViewById方法来获得其中的界面元素.
举例:定义了一个控件类CleanableEditText,实现在焦点变化时和输入内容发生变化时均要判断是否显示右边clean图标
后台调用自定义控件的时候需要LayoutInflater来载入(见第三段代码)。
自定义控件:
package com.utils; import android.content.Context; import android.graphics.drawable.Drawable; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.EditText; /** * 在焦点变化时和输入内容发生变化时均要判断是否显示右边clean图标 */ public class CleanableEditText extends EditText { private Drawable mRightDrawable; private boolean isHasFocus; public CleanableEditText(Context context) { super(context); init(); } public CleanableEditText(Context context, AttributeSet attrs) { super(context, attrs); init(); } public CleanableEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init(){ //getCompoundDrawables: //Returns drawables for the left, top, right, and bottom borders. Drawable [] drawables=this.getCompoundDrawables(); //取得right位置的Drawable //即我们在布局文件中设置的android:drawableRight mRightDrawable=drawables[2]; //设置焦点变化的监听 this.setOnFocusChangeListener(new FocusChangeListenerImpl()); //设置EditText文字变化的监听 this.addTextChangedListener(new TextWatcherImpl()); //初始化时让右边clean图标不可见 setClearDrawableVisible(false); } /** * 当手指抬起的位置在clean的图标的区域 * 我们将此视为进行清除操作 * getWidth():得到控件的宽度 * event.getX():抬起时的坐标(改坐标是相对于控件本身而言的) * getTotalPaddingRight():clean的图标左边缘至控件右边缘的距离 * getPaddingRight():clean的图标右边缘至控件右边缘的距离 * 于是: * getWidth() - getTotalPaddingRight()表示: * 控件左边到clean的图标左边缘的区域 * getWidth() - getPaddingRight()表示: * 控件左边到clean的图标右边缘的区域 * 所以这两者之间的区域刚好是clean的图标的区域 */ @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_UP: boolean isClean =(event.getX() > (getWidth() - getTotalPaddingRight()))&& (event.getX() < (getWidth() - getPaddingRight())); if (isClean) { setText(""); } break; default: break; } return super.onTouchEvent(event); } private class FocusChangeListenerImpl implements OnFocusChangeListener{ @Override public void onFocusChange(View v, boolean hasFocus) { isHasFocus=hasFocus; if (isHasFocus) { boolean isVisible=getText().toString().length()>=1; setClearDrawableVisible(isVisible); } else { setClearDrawableVisible(false); } } } //当输入结束后判断是否显示右边clean的图标 private class TextWatcherImpl implements TextWatcher{ @Override public void afterTextChanged(Editable s) { boolean isVisible=getText().toString().length()>=1; setClearDrawableVisible(isVisible); } @Override public void beforeTextChanged(CharSequence s, int start, int count,int after) { } @Override public void onTextChanged(CharSequence s, int start, int before,int count) { } } //隐藏或者显示右边clean的图标 protected void setClearDrawableVisible(boolean isVisible) { Drawable rightDrawable; if (isVisible) { rightDrawable = mRightDrawable; } else { rightDrawable = null; } //使用代码设置该控件left, top, right, and bottom处的图标 setCompoundDrawables(getCompoundDrawables()[0],getCompoundDrawables()[1], rightDrawable,getCompoundDrawables()[3]); } }
调用xml
<com.utils.CleanableEditText android:id="@+id/login_name" android:layout_width="match_parent" android:layout_height="@dimen/logineditview_hight" android:layout_marginLeft="16dp" android:hint="@string/user_name" android:layout_toRightOf="@id/img_login_name" android:background="@null" android:singleLine="true" android:imeOptions="actionNext" android:drawableRight="@drawable/clean" />
后台调用:
final View getView = LayoutInflater.from(this.getContext()).inflate(R.layout.login, null); ed_loginCode = (CleanableEditText) getView.findViewById(R.id.login_name);
相关文章推荐
- LayoutInflater作用及使用--自定义EditText,自带清除内容按钮
- 自定义EditText,自带清除内容按钮
- 自定义EditText,自带清除内容按钮
- 自定义EditText实现清除文本内容按钮
- Android 实现自定义自带删除按钮的EditText(带有搜索接口)
- 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)
- 自定义View之自定义EditText带删除内容按钮控件
- Android自定义EditText——带一键清除和密码明文切换按钮,支持多样式选择
- 自定义EditText—带有清除功能,在显示多行文本时清除按钮位于整个EditText的右下角
- 自定义EditText:带清除按钮的EditText
- LayoutInflater作用及使用
- LayoutInflater作用及使用
- 【Android 界面效果23】LayoutInflater作用及使用
- LayoutInflater作用及使用
- LayoutInflater作用及使用
- Android自定义View带有删除按钮的EditText
- Android自定义View带有删除按钮的EditText
- [好用的小控件] 自带清除的EditText
- LayoutInflater作用及使用
- 使用ImageView 加上 Edittext做出自定义的输入框(EditText美化)