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

Android 登入頁實作:自訂可清空輸入框與抖動提示

編輯:關於 Android 程式設計

先看成果

自訂 ClearEditText

這個控件在 EditText 上疊了一個「清除」圖示:有內容且取得焦點時才顯示,點它就清空。因為 EditText 本身無法直接綁點擊事件,這裡用「記住按下位置是否落在 compound drawable 區域」的方式模擬點擊。

public class ClearEditText extends EditText
        implements View.OnFocusChangeListener, TextWatcher {

    private Drawable mClearDrawable;   // 清除按鈕的 Drawable
    private boolean hasFoucs;

    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() {
        // 先取 XML 上設定的 drawableRight,沒有就用預設圖
        mClearDrawable = getCompoundDrawables()[2];
        if (mClearDrawable == null) {
            mClearDrawable = getResources().getDrawable(R.drawable.selector_ic_delete);
        }
        // getIntrinsicWidth 取的是 Drawable 的實際寬度,不同解析度下數值不同
        mClearDrawable.setBounds(0, 0,
                mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());

        setClearIconVisible(false);
        setOnFocusChangeListener(this);
        addTextChangedListener(this);
    }

    /**
     * 模擬點擊:按下位置落在「寬度 - 右內距 - 圖示寬」到「寬度 - 右內距」之間就算命中
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (getCompoundDrawables()[2] != null) {
                boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight())
                        && (event.getX() < (getWidth() - getPaddingRight()));
                if (touchable) this.setText("");
            }
        }
        return super.onTouchEvent(event);
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        this.hasFoucs = hasFocus;
        setClearIconVisible(hasFocus && getText().length() > 0);
    }

    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) {
        if (hasFoucs) setClearIconVisible(s.length() > 0);
    }

    @Override public void beforeTextChanged(CharSequence s, int a, int b, int c) { }
    @Override public void afterTextChanged(Editable s) { }

    /** 觸發抖動動畫 */
    public void setShakeAnimation() {
        this.setAnimation(shakeAnimation(5));
    }

    /**
     * 抖動動畫
     * @param counts 一秒內左右晃幾次
     */
    public static Animation shakeAnimation(int counts) {
        Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0);
        translateAnimation.setInterpolator(new CycleInterpolator(counts));
        translateAnimation.setDuration(1000);
        return translateAnimation;
    }
}

MainActivity

這裡只示範按鈕的回呼,實務上可以在密碼輸入錯誤時呼叫 setShakeAnimation() 給使用者更明確的回饋。

public class MainActivity extends Activity {
    private Button btnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        btnLogin = (Button) this.findViewById(R.id.btnLogin);
    }

    public void login(View view) {
        Toast.makeText(this, "登陸", Toast.LENGTH_LONG).show();
    }
}

版面檔

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffffff"
    android:orientation="vertical"
    android:padding="4.0dip">

    <com.xuliugen.clearedittext.ClearEditText
        android:id="@+id/etxtEmail"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30.0dip"
        android:drawableLeft="@drawable/icon_reg_name"
        android:drawablePadding="10.0dip"
        android:hint="使用郵箱登陸" />

    <com.xuliugen.clearedittext.ClearEditText
        android:id="@+id/etxtPwd"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20.0dip"
        android:drawableLeft="@drawable/icon_reg_password"
        android:drawablePadding="10.0dip"
        android:hint="輸入登陸密碼"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/btnLogin"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20.0dip"
        android:onClick="login"
        android:text="登陸" />
</LinearLayout>

幾個 selector 資源

按鈕的三態背景 bg_btn_style_green.xml:

<selector xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:state_enabled="false">
        <shape android:shape="rectangle">
            <corners android:radius="2.0dip" />
            <solid android:color="@color/green_btn_color_disable" />
        </shape>
    </item>
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <corners android:radius="2.0dip" />
            <solid android:color="@color/green_btn_color_pressed" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="2.0dip" />
            <solid android:color="@color/green_btn_color_normal" />
        </shape>
    </item>
</selector>

輸入框焦點切換 bg_edittext_selector.xml:

<selector xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/input_bar_bg_active" android:state_focused="true" />
    <item android:drawable="@drawable/input_bar_bg_normal" />
</selector>

整個頁面的視覺細節其實都在這些 selector 與圖片資源裡:焦點態換底色、按下態變色,加上清空圖示與錯誤抖動,登入頁該有的回饋就齊了。

專案免費下載:https://yunpan.cn/cwWymCEMmgTzp (提取碼:317d)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved