Android實現(xiàn)密碼明密文切換(小眼睛)
本文實例為大家分享了Android實現(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(); ? ? ? ? } ? ? ? ? //隱藏標題欄 ? ? ? ? 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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android開發(fā)AsmClassVisitorFactory使用詳解
這篇文章主要為大家介紹了Android開發(fā)AsmClassVisitorFactory使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06Android不使用自定義布局情況下實現(xiàn)自定義通知欄圖標的方法
這篇文章主要介紹了Android不使用自定義布局情況下實現(xiàn)自定義通知欄圖標的方法,實例分析了Android通知欄圖標的創(chuàng)建技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-12-12Android實現(xiàn)TextView兩端對齊的方法
這篇文章主要介紹了Android實現(xiàn)TextView兩端對齊的方法,需要的朋友可以參考下2016-01-01Android開發(fā)控制ScrollView滑動速度的方法
這篇文章主要介紹了Android開發(fā)控制ScrollView滑動速度的方法,結合實例形式分析了Android編程中ScrollView滑動事件相關操作技巧,需要的朋友可以參考下2017-02-02淺談Android應用安全防護和逆向分析之a(chǎn)pk反編譯
我們有時候在某個app上見到某個功能,某個效果蠻不錯的,我們想看看對方的思路怎么走的,這時候,我們就可以通過反編譯來編譯該apk,拿到代碼,進行分析。2021-06-06