Android實(shí)現(xiàn)密碼明密文切換(小眼睛)
本文實(shí)例為大家分享了Android實(shí)現(xiàn)密碼明密文切換的具體代碼,供大家參考,具體內(nèi)容如下
小眼睛在密碼欄右邊!


奉上我使用的素材:


添加圖片到res/darwable中
對安卓的知識掌握的非常淺,只知道 圖片名稱不要大寫,大寫會報錯!
如果格式正確仍會報錯的話,則 在gradle里加上這兩句,俺也不懂為什么,都沒有講原理的。
aaptOptions.cruncherEnabled = false aaptOptions.useNewCruncher = false


編輯登錄頁.xml
文本+可編輯文本框+小眼睛圖片+按鈕
小眼睛只要寫一個ImageView即可

<LinearLayout ? ? ? ? ? ? android:id="@+id/ll_username" ? ? ? ? ? ? android:layout_below="@id/iv" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_marginTop="60dp" ? ? ? ? ? ? android:layout_marginLeft="10dp" ? ? ? ? ? ? android:layout_marginRight="10dp" ? ? ? ? ? ? android:layout_marginBottom="5dp" ? ? ? ? ? ? android:layout_centerVertical="true" ? ? ? ? ? ? android:background="#6B009688"> ? ? ? ? <TextView ? ? ? ? ? ? ? ? android:id="@+id/tv_login_username" ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:text="賬號:" ? ? ? ? ? ? ? ? android:padding="10dp" ? ? ? ? ? ? ? ? android:textSize="20dp" ? ? ? ? ? ? ? ? android:textColor="@color/white"/> ? ? ? ? <EditText ? ? ? ? ? ? ? ? android:id="@+id/et_login_username" ? ? ? ? ? ? ? ? android:maxLines="1" ? ? ? ? ? ? ? ? android:maxLength="16" ? ? ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:layout_marginLeft="10dp" ? ? ? ? ? ? ? ? android:background="@null"/> ? ? </LinearLayout> ? ? <LinearLayout ? ? ? ? ? ? android:id="@+id/ll_password" ? ? ? ? ? ? android:layout_below="@id/ll_username" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_marginLeft="10dp" ? ? ? ? ? ? android:layout_marginRight="10dp" ? ? ? ? ? ? android:layout_centerVertical="true" ? ? ? ? ? ? android:background="#6B009688"> ? ? ? ? <TextView ? ? ? ? ? ? ? ? android:id="@+id/tv_login_password" ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:text="密碼:" ? ? ? ? ? ? ? ? android:padding="10dp" ? ? ? ? ? ? ? ? android:textSize="20dp" ? ? ? ? ? ? ? ? android:textColor="@color/white"/> ? ? ? ? <EditText ? ? ? ? ? ? ? ? android:id="@+id/et_login_password" ? ? ? ? ? ? ? ? android:maxLines="1" ? ? ? ? ? ? ? ? android:maxLength="6" ? ? ? ? ? ? ? ? android:layout_width="255dp" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:layout_marginLeft="10dp" ? ? ? ? ? ? ? ? android:background="@null"/> ? ? ? ? <ImageView android:layout_width="20dp" ? ? ? ? ? ? ? ? ? ?android:layout_height="20dp" ? ? ? ? ? ? ? ? ? ?android:layout_marginTop="14dp" ? ? ? ? ? ? ? ? ? ?android:id="@+id/display_password"/> ? ? </LinearLayout> ? ? <LinearLayout ? ? ? ? ? ? android:id="@+id/ll_btm" ? ? ? ? ? ? android:layout_below="@id/ll_password" ? ? ? ? ? ? android:gravity="center" ? ? ? ? ? ? android:orientation="vertical" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content"> ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/btn_login" ? ? ? ? ? ? ? ? android:layout_width="300dp" ? ? ? ? ? ? ? ? android:layout_height="50dp" ? ? ? ? ? ? ? ? android:layout_marginTop="50dp" ? ? ? ? ? ? ? ? android:text="登錄" ? ? ? ? ? ? ? ? android:textSize="18dp" ? ? ? ? ? ? ? ? android:background="@color/white" ? ? ? ? /> ? ? </LinearLayout>
編輯登錄頁小眼睛功能.java
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
? ? private EditText loginUsername;
? ? private EditText loginPassword;
? ? private Button login;
? ? private ImageView displayPassword;
? ? private boolean isHideFirst = false;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_login);
? ? ? ? ActionBar actionBar = getSupportActionBar();
? ? ? ? if (actionBar != null) {
? ? ? ? ? ? actionBar.hide();
? ? ? ? }
? ? ? ? //隱藏標(biāo)題欄
? ? ? ? login = findViewById(R.id.btn_login);
? ? ? ? loginUsername = findViewById(R.id.et_login_username);
? ? ? ? loginPassword = findViewById(R.id.et_login_password);
? ? ? ? displayPassword = findViewById(R.id.display_password);
? ? ? ? login.setOnClickListener(this);
? ? ? ? displayPassword.setOnClickListener(this);
? ? ? ? displayPassword.setImageResource(R.drawable.open);
? ? }
? ? @Override
? ? public void onClick(View v){
? ? ? ? switch (v.getId()) {
? ? ? ? ? ? case R.id.display_password:{
? ? ? ? ? ? ? ? if (isHideFirst) {
? ? ? ? ? ? ? ? ? ? displayPassword.setImageResource(R.drawable.open);
? ? ? ? ? ? ? ? ? ? HideReturnsTransformationMethod method1 = HideReturnsTransformationMethod.getInstance();
? ? ? ? ? ? ? ? ? ? loginPassword.setTransformationMethod(method1);
? ? ? ? ? ? ? ? ? ? isHideFirst = false;
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? displayPassword.setImageResource(R.drawable.close);
? ? ? ? ? ? ? ? ? ? TransformationMethod method = PasswordTransformationMethod.getInstance();
? ? ? ? ? ? ? ? ? ? loginPassword.setTransformationMethod(method);
? ? ? ? ? ? ? ? ? ? isHideFirst = true;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? int index = loginPassword.getText().toString().length();
? ? ? ? ? ? ? ? loginPassword.setSelection(index);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? case R.id.btn_login: {
?? ??? ??? ??? ?//。。。。。
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? }
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android 實(shí)現(xiàn)密碼輸入框動態(tài)明文/密文切換顯示效果
- Android實(shí)現(xiàn)顯示和隱藏密碼功能的示例代碼
- Android 登錄頁面的實(shí)現(xiàn)代碼(密碼顯示隱藏、EditText 圖標(biāo)切換、限制輸入長度)
- Android中實(shí)現(xiàn)密碼的隱藏和顯示的示例
- Android EditText密碼的隱藏和顯示功能
- Android 密碼 顯示與隱藏功能實(shí)例
- Android中實(shí)現(xiàn)EditText密碼顯示隱藏的方法
- Android文本輸入框(EditText)輸入密碼時顯示與隱藏
- Android實(shí)現(xiàn)動態(tài)顯示或隱藏密碼輸入框的內(nèi)容
相關(guān)文章
ListView實(shí)現(xiàn)頂部和底部內(nèi)容指示器的方法
這篇文章主要介紹了ListView實(shí)現(xiàn)頂部和底部內(nèi)容指示器的方法,需要的朋友可以參考下2015-09-09
Android 中RecyclerView通用適配器的實(shí)現(xiàn)
這篇文章主要介紹了Android 中RecyclerView通用適配器的實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-03-03
Okhttp3實(shí)現(xiàn)爬取驗(yàn)證碼及獲取Cookie的示例
本篇文章主要介紹了Okhttp3實(shí)現(xiàn)爬取驗(yàn)證碼及獲取Cookie的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
Android SurfaceView與TextureView使用方法詳細(xì)講解
SurfaceView和TextureView都繼承View,與普通的View不同的是,它倆可以在獨(dú)立線程中繪制渲染,性能更高,所以常被應(yīng)用在對繪制速率要求比較高的場景,比如相機(jī)預(yù)覽,視頻播放等等2022-10-10
Android屬性動畫實(shí)現(xiàn)圖片從左到右逐漸消失
這篇文章主要介紹了Android屬性動畫實(shí)現(xiàn)圖片從左到右逐漸消失,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11
解決genymotion模擬器無法聯(lián)網(wǎng)的正確方法100%成功
Android編程之控件狀態(tài)配置文件實(shí)例

