編輯:關於 Android 程式設計
先看成品:

實作過程中碰到的幾個問題:
1、如何為 EditText 設定自定義背景;
2、進入頁面時 EditText 會自動搶走焦點;
3、hint 應該在取得焦點時清空,而不是等使用者開始輸入才消失;
4、清空按鈕該在什麼時機出現(取得焦點且已有內容)。
以下逐一說明對應的程式碼。
<!-- android:focusable="true" android:focusableInTouchMode="true" 把EditText默認的行為截斷了! --> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ECEDF1" android:focusable="true" android:focusableInTouchMode="true" tools:context="com.dragon.android.qqlogin.MainActivity$PlaceholderFragment" > <ImageView android:id="@+id/imageView1" android:layout_width="70dp" android:layout_height="70dp" android:layout_centerHorizontal="true" android:layout_marginBottom="5dp" android:layout_marginTop="40dp" android:src="@drawable/a" /> <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/imageView1" android:ems="10" android:background="@drawable/bg_edittext" android:inputType="textPersonName" android:gravity="center" android:textColor="#6A6A6C" android:hint="@string/inaccount" android:textColorHint="#ECEDDD"> </EditText> <EditText android:id="@+id/editText2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/editText1" android:ems="10" android:background="@drawable/bg_edittext" android:inputType="textPassword" android:gravity="center" android:textColor="#6A6A6C" android:hint="@string/inpwd" android:textColorHint="#ECEDDD" > </EditText> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="40dp" android:layout_below="@id/editText2" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="10dp" android:background="@drawable/bg_button" android:text="@string/button" android:gravity="center" android:textColor="#F9FAFB" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:padding="10dp" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="@string/faillogin" android:textColor="#0EB1EF" /> <TextView android:id="@+id/textView3" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="right" android:text="@string/regist" android:textColor="#0EB1EF" /> </LinearLayout> <Button android:id="@+id/button2" android:layout_width="16dp" android:layout_height="16dp" android:layout_alignTop="@id/editText1" android:layout_marginTop="15dp" android:layout_alignParentRight="true" android:layout_marginRight="10dp" android:background="@drawable/clear" android:visibility="invisible" /> <Button android:id="@+id/button3" android:layout_width="16dp" android:layout_height="16dp" android:layout_alignTop="@id/editText2" android:layout_marginTop="15dp" android:layout_alignLeft="@+id/button2" android:background="@drawable/clear" android:visibility="invisible" /> </RelativeLayout> fragment_main
問題二的成因值得解釋:在根 RelativeLayout 上寫 focusable 與 focusableInTouchMode,原意是希望父容器搶走初始焦點、避免輸入框自動彈出鍵盤,但實際效果卻把 EditText 的預設行為一起截斷了,導致後續焦點邏輯全部失效。教訓是——處理初始焦點應該從 Activity 或 windowSoftInputMode 下手,而不是在父容器上加這兩個屬性。
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="https://schemas.android.com/apk/res/android" > <stroke android:width="1px" android:color="#00ACED" /> <solid android:color="#00ACED" /> <corners android:radius="10dp" /> </shape> bg_button
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="https://schemas.android.com/apk/res/android" > <stroke android:width="1px" android:color="#ECEDF1" /> <solid android:color="#F9FAFB" /> <corners android:radius="10dp" /> <padding android:top="10dp" android:bottom="10dp"/> </shape> bg_edittext
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">qqloginnew</string> <string name="action_settings">Settings</string> <string name="button">登錄</string> <string name="faillogin">無法登錄?</string> <string name="regist">新用戶注冊</string> <string name="inaccount">QQ號/手機號/郵箱</string> <string name="inpwd">密碼</string> <string name="sucess">登錄成功</string> </resources> strings
自定義背景的做法就是用 shape 畫:stroke 描邊、solid 填色、corners 圓角、padding 內距,四個標籤組出需要的樣子。登入按鈕用 QQ 藍填滿,輸入框則用淺色底加淡色描邊做出層次。
package com.dragon.android.qqloginnew; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnFocusChangeListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { private EditText editText1; private EditText editText2; // private Button button; private Button clearButton1; private Button clearButton2; // 得到strings中的屬性 // private String string2 = getResources().getString(R.string.inaccount); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); editText1 = (EditText) findViewById(R.id.editText1); editText2 = (EditText) findViewById(R.id.editText2); // button = (Button) findViewById(R.id.button1); clearButton1 = (Button) findViewById(R.id.button2); clearButton2 = (Button) findViewById(R.id.button3); // 對EditText進行焦點變更監聽 editText1.setOnFocusChangeListener(new EditTextListener(clearButton1)); editText2.setOnFocusChangeListener(new EditTextListener(clearButton2)); // 對清空按鈕進行點擊監聽 clearButton1.setOnClickListener(new ClearButtonListener()); clearButton2.setOnClickListener(new ClearButtonListener()); // 對EditText進行編輯監聽 editText1.addTextChangedListener(new MyEditTextWatcher(editText1)); editText2.addTextChangedListener(new MyEditTextWatcher(editText2)); } /** * 對EditText的內容進行實時監控 * * @author Auser * */ class MyEditTextWatcher implements TextWatcher { private CharSequence temp; private EditText editText; public MyEditTextWatcher(EditText editText) { this.editText = editText; } @Override // int start開始的位置, int count被改變的舊內容數, int after改變後的內容數量 public void beforeTextChanged(CharSequence s, int start, int count, int after) { // 這裡的s表示改變之前的內容,通常start和count組合,可以在s中讀取本次改變字段中被改變的內容。而after表示改變後新的內容的數量。 } @Override // int start開始的位置, int before改變前的內容數量, int count新增量 public void onTextChanged(CharSequence s, int start, int before, int count) { // 這裡的s表示改變之後的內容,通常start和count組合,可以在s中讀取本次改變字段中新的內容。而before表示被改變的內容的數量。 temp = s; } @Override // 表示最終內容 public void afterTextChanged(Editable s) { if (temp.length() > 0) { // 設置清空按鈕為可見 if (editText == editText1) { clearButton1.setVisibility(View.VISIBLE); } else if (editText == editText2) { clearButton2.setVisibility(View.VISIBLE); } } else { // 設置清空按鈕不可見 if (editText == editText1) { clearButton1.setVisibility(View.INVISIBLE); } else if (editText == editText2) { clearButton2.setVisibility(View.INVISIBLE); } } } } /** * 清空按鈕點擊事件 * * @author * */ class ClearButtonListener implements OnClickListener { @Override public void onClick(View view) { if (view == clearButton1) { editText1.setText(""); } else if (view == clearButton2) { editText2.setText(""); } } } /** * 焦點變更事件 * * @author Auser * */ class EditTextListener implements OnFocusChangeListener { private Button clear; public EditTextListener(Button clear) { this.clear = clear; } @Override public void onFocusChange(View v, boolean hasFocus) { EditText textView = (EditText) v; String hint; if (hasFocus) { // 當獲取焦點時如果內容不為空則清空按鈕可見 if (!textView.getText().toString().equals("")) { clear.setVisibility(View.VISIBLE); } // if (textView == editText2) { // // 設置輸入格式為不可見的密碼格式 // textView.setInputType(InputType.TYPE_CLASS_TEXT // | InputType.TYPE_TEXT_VARIATION_PASSWORD); // } hint = textView.getHint().toString(); // 給TextView添加額外的數據 textView.setTag(hint); textView.setHint(""); } else { // 當失去焦點時清空按鈕不可見 clear.setVisibility(View.INVISIBLE); // if (textView == editText2) { // // 設置輸入格式為可見的密碼格式 // textView.setInputType(InputType.TYPE_CLASS_TEXT // | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); // } // 取出之前添加的額外數據 hint = textView.getTag().toString(); textView.setHint(hint); } } } }
三個監聽器分工明確,值得分別說明:
TextWatcher 負責清空按鈕的顯示時機。afterTextChanged 中判斷 temp 長度是否大於 0,有內容就顯示清除鈕,清空則隱藏,這樣就解決了問題四。
OnFocusChangeListener 的解法比較巧妙,是問題三的核心:取得焦點時,先把 hint 字串存進 setTag,再把 hint 設為空字串,於是提示文字立刻消失;失去焦點時,再從 tag 把 hint 取回來還原。用 tag 暫存而不是硬寫字串,好處是 hint 內容日後從 strings.xml 改了,這裡不用跟著動。
OnClickListener 則是最單純的清空動作,點哪個按鈕就清哪個輸入框。
下一篇會接著介紹 QQ 新用戶註冊頁的實作與其踩到的問題,有興趣可以參考。以上就是本次整理的全部內容,若有疑問歡迎留言。
Picasso 源碼深度解析:從入隊到顯示的完整鏈路
Android 的圖片加載庫我用過不少:UIL、Volley、Picasso、Glide、Fresco,都是不錯的選擇。其中 Picasso 研究得稍多一些。網上關於它的
GitHub 項目解析(十一):一個簡單卻強大的自定義廣告彈窗
上一篇講的是我最近寫的網二維碼掃描庫,核心掃描能力依賴 ZXing。做的過程裡發現集成步驟還是偏繁瑣,於是有了封裝成標準庫的想法——項目名 android-zxingLi
自製 ImageLoader:從 LruCache 到非同步圖片載入的完整實作
先看幾張效果圖: 在載入大量圖片時,我們採用後進先出策略(滑到哪裡就先載入哪裡的圖片),既節省記憶體,使用者體驗也更好。下面就來打造自己的 ImageLoader。
Android Notification 通知欄訊息實作指南
在 Android 中,Notification 是 App 與使用者溝通的重要方式。無論是下載進度、新訊息提醒,還是常駐服務狀態,都可以透過通知欄呈現。本文整理常見通知