編輯:關於 Android 程式設計

這個控件在 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;
}
}
這裡只示範按鈕的回呼,實務上可以在密碼輸入錯誤時呼叫 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>
按鈕的三態背景 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)
Android SystemProperties 系統屬性讀寫機制詳解
SystemProperties 是 Android 系統中常見的一組鍵值對配置機制。它位於 android.os 包下,但上層應用通常不能直接調用,需要借助反射或其他系
Android 縮圖載入優化:以 inSampleSize 與 createScaledBitmap 節省記憶體
為何要載入縮圖 在列表或資訊流中,通常不需要載入原圖,只要顯示縮圖即可。這樣可以明顯降低記憶體使用,避免大圖造成崩潰。 載入流程 可先使用 inJustDecodeB
使用 XML 在 Android 中建立分割線
在 Android 介面中,分割線常用來區隔內容區塊。若沒有美工資源,也可以直接用 XML 建立簡單的線條,讓畫面層次更清楚。 直接建立 View 最直接的方式是在版面配
Android 藍牙源碼分析:Inquiry Result 之後上層做了什麼
前一篇我們追了底層如何解析藍牙晶片回報的搜尋結果,這篇接著往下走:這些裝置資訊往上一層傳之後,framework 與 Settings 應用是怎麼把它變成螢幕上那一列列可