Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 登入界面實作:帳號密碼輸入框獲得焦點時切換選中背景

編輯:關於 Android 程式設計

我們常在網上看到這樣的效果:當點選帳號或密碼輸入框時,它的背景會變成一種選中狀態,也就是輸入框四周會出現一圈選中顏色的漸變描邊,就像 GitHub 登入界面那樣:

很多時候我們也希望自己的 App 有類似效果——點擊帳號和密碼輸入框時顯示選中狀態。該怎麼實現呢?效果如下圖:

其實思路很簡單,只需建立一個背景 selector 選擇器,在不同的狀態下配置不同的背景即可。

一、建立 bg_edittext_selector.xml 背景選擇器

             

可以看到,當控件處於 state_focused(獲得焦點)狀態時,描邊顏色變為藍色 #2D9FF7;預設狀態則為白色描邊。

二、編寫佈局並將選擇器設為輸入框背景

         

從程式碼可以看出,佈局中每個輸入區都是一個 LinearLayout 包含兩個子控件、水平排列:一個是 TextView 標籤,另一個是帶清除功能的自定義控件 ClearEditText。

為了實現選中效果,我們把選擇器 bg_edittext_selector.xml 設為父容器 LinearLayout 的背景。但這裡有個關鍵細節需要注意:選擇器有很多狀態——預設、選中、聚焦等等,而父容器本身並沒有聚焦或選中狀態,我們真正關心的是帳號、密碼這兩個輸入框是否被選中。那要如何讓子控件的狀態與父容器的狀態聯動呢?

這裡父容器 LinearLayout 有一個非常好用的屬性 android:addStatesFromChildren="true"。這個屬性在 Google 官方 API 中的大意是:當設為 true 時,ViewGroup 的 drawable 狀態可以依據子控件(如 EditText、Button)的焦點狀態發生改變,從而使它們看起來像一個整體,父容器的狀態跟隨子控件一起變動。

最後,附上 ClearEditText 的程式碼:

 /** * @Description: 帶刪除按鈕的輸入框 * @author https://blog.csdn.net/finddreams */ public class ClearEditText extends EditText implements OnFocusChangeListener, TextWatcher { /** 刪除按鈕的引用,可以自定義圖片 */ private Drawable mClearDrawable; public static int SIZE = 0; public ClearEditText(Context context) { this(context, null); } public ClearEditText(Context context, AttributeSet attrs) { // 這裡構造方法也很重要,不加這個很多屬性不能再XML裡面定義 this(context, attrs, android.R.attr.editTextStyle); } public ClearEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { // 獲取EditText的DrawableRight,假如沒有設置我們就使用默認的圖片 mClearDrawable = getCompoundDrawables()[2]; if (mClearDrawable == null) { mClearDrawable = getResources().getDrawable(R.drawable.progresscancelbtn); } mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth() + SIZE, mClearDrawable.getIntrinsicHeight() + SIZE); setClearIconVisible(false); setOnFocusChangeListener(this); addTextChangedListener(this); } /** * 因為我們不能直接給EditText設置點擊事件,所以我們用記住我們按下的位置來模擬點擊事件 */ @Override public boolean onTouchEvent(MotionEvent event) { if (getCompoundDrawables()[2] != null) { if (event.getAction() == MotionEvent.ACTION_UP) { boolean touchable = event.getX() > (getWidth() - getPaddingRight() - mClearDrawable.getIntrinsicWidth()) && (event.getX() < ((getWidth() - getPaddingRight()))); if (touchable) { this.setText(""); } } } return super.onTouchEvent(event); } /** 當ClearEditText焦點發生變化時,設置清除圖標的顯示與隱藏 */ @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { setClearIconVisible(getText().length() > 0); } else { setClearIconVisible(false); } } protected void setClearIconVisible(boolean visible) { Drawable right = visible ? mClearDrawable : null; setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], right, getCompoundDrawables()[3]); } @Override public void onTextChanged(CharSequence s, int start, int count, int after) { setClearIconVisible(s.length() > 0); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { } /** 設置晃動動畫 */ public void setShakeAnimation() { this.setAnimation(shakeAnimation(5)); } public static Animation shakeAnimation(int counts) { Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0); translateAnimation.setInterpolator(new CycleInterpolator(counts)); translateAnimation.setDuration(1000); return translateAnimation; } }
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved