編輯:關於 Android 開發

在做登錄界面時,密碼框需要支持「明文/密文」的即時切換,這是很實用的交互細節。下面我整理了布局文件以及兩種實現思路。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="liu.basedemo.MainActivity">
<EditText
android:id="@+id/etUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:hint="請輸入用戶名"
android:textColor="#000000"
android:textColorHint="#55000000"
android:textSize="20sp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:hint="請輸入密碼"
android:inputType="textPassword"
android:textColor="#000000"
android:textColorHint="#55000000"
android:textSize="20sp"/>
<CheckBox
android:checked="false"
android:id="@+id/cbDisplayPassword"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:button="@drawable/selector_password"/>
</RelativeLayout>
</LinearLayout>
切換按鈕的 selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/cb_checked" android:state_checked="true"/>
<item android:drawable="@mipmap/cb_normaled" android:state_checked="false"/>
</selector>
private void initListener() {
mCbDisplayPassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d(TAG, "onCheckedChanged: " + isChecked);
if (isChecked) {
// 選中狀態:顯示明文
mEtPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
} else {
// 默認狀態:顯示密碼,需一起寫才能生效
mEtPassword.setInputType(InputType.TYPE_CLASS_TEXT
| InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
}
});
}
private void initListener() {
mCbDisplayPassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d(TAG, "onCheckedChanged: " + isChecked);
if (isChecked) {
// 選中狀態:顯示明文
mEtPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
} else {
// 默認狀態:顯示密碼
mEtPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
}
});
}
XListView 下拉刷新與上拉加載的使用方法
XListView 下拉刷新與上拉加載的使用方法 第一步:到官方網站下載第三方套件,複製到自己的專案中。 https://github.com/Maxwin-z/XLi
單機搭建 Android 開發環境(五):Android Studio、SDK 與 NDK
單機搭建 Android 開發環境:Android Studio、SDK 與 NDK 本系列前文已介紹 Android 系統開發環境,本文接續說明應用開發環境的建置。相較
Android 實作:透過瀏覽器點擊連結開啟本地 App 並接收傳遞的資料
目標:點擊瀏覽器中的 URL 連結,啟動特定的 App。 網頁端 先做成 HTML 頁面,內容格式如下: 啟動應用程式 各欄位含義: scheme:判別啟動的 App(
Android 手機衛士實作教學(七):打造黑名單,攔截來電與簡訊
Android 手機衛士實作教學(七):打造黑名單,攔截來電與簡訊 本篇要替手機衛士加上最實用的防騷擾功能:黑名單攔截。我們會先建立資料庫、封裝資料存取邏輯,再透過有序