作用:
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);