android實(shí)現(xiàn)密碼框右側(cè)顯示小眼睛
本文實(shí)例為大家分享了android實(shí)現(xiàn)密碼框右側(cè)顯示小眼睛的具體代碼,供大家參考,具體內(nèi)容如下



實(shí)現(xiàn)效果
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="12dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:paddingTop="10dp"
android:background="@drawable/selector_sign"
android:id="@+id/et_usertel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:hint="@string/editText_phont"
android:inputType="phone"
android:paddingLeft="90dp"
android:singleLine="true"
android:textColorHint="#DDDDDD"
android:textSize="16sp" />
<EditText
android:id="@+id/edt_chosed_country_num"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/et_usertel"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:background="@color/white"
android:inputType="number"
android:maxLength="5"
android:text="+86"
android:textColor="#353535"
android:textSize="16sp" />
</RelativeLayout>
<LinearLayout
android:id="@+id/linear_password_code"
android:visibility="visible"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:background="@drawable/selector_sign"
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:paddingTop="10dp"
android:hint="@string/editText_code"
android:paddingLeft="90dp"
android:singleLine="true"
android:textColorHint="#DDDDDD"
android:textSize="16sp" />
<Button
android:id="@+id/bu_register_code"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignRight="@+id/et_password"
android:layout_marginRight="2dp"
android:background="@drawable/shape_border_color_primary_sign"
android:text="@string/verification_code"
android:textColor="@drawable/selector_text_color_primary"
android:textSize="16sp" />
<!-- <View
android:layout_marginTop="1dp"
android:layout_alignBottom="@+id/bu_code"
android:layout_width="290dp"
android:layout_height="1dp"
android:background="@color/light_grey"/>-->
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/password_liner"
android:visibility="gone"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:background="@drawable/selector_sign"
android:id="@+id/et_password_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:paddingTop="10dp"
android:hint="請輸入密碼登錄"
android:paddingLeft="90dp"
android:singleLine="true"
android:textColorHint="#DDDDDD"
android:inputType="textPassword"
android:textSize="16sp" />
<ImageView
android:id="@+id/imageView"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_margin="10dp" />
</RelativeLayout>
</LinearLayout>
<Button
android:textColor="@color/background"
android:layout_marginTop="10dp"
android:background="@drawable/shape_color_primary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登錄"/>
<LinearLayout
android:layout_marginTop="2dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textStyle="bold"
android:gravity="center"
android:text="使用密碼登錄 >"
android:id="@+id/tv_register_login_password"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
//主要代碼如下
<LinearLayout
android:id="@+id/password_liner"
android:visibility="gone"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:background="@drawable/selector_sign"
android:id="@+id/et_password_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:paddingTop="10dp"
android:hint="請輸入密碼登錄"
android:paddingLeft="90dp"
android:singleLine="true"
android:textColorHint="#DDDDDD"
android:inputType="textPassword"
android:textSize="16sp" />
<ImageView
android:id="@+id/imageView"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_margin="10dp" />
</RelativeLayout>
</LinearLayout>
重要代碼
private boolean isHideFirst = true;// 輸入框密碼是否是隱藏的,默認(rèn)為true
if (isHideFirst == true) {
imageView.setImageResource(R.drawable.open);
//密文
HideReturnsTransformationMethod method1 = HideReturnsTransformationMethod.getInstance();
et_password_code.setTransformationMethod(method1);
isHideFirst = false;
} else {
imageView.setImageResource(R.drawable.close);
//密文
TransformationMethod method = PasswordTransformationMethod.getInstance();
et_password_code.setTransformationMethod(method);
isHideFirst = true;
}
// 光標(biāo)的位置
int index = et_password_code.getText().toString().length();
et_password_code.setSelection(index);
package com.ynrd.entrepreneur.activity;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.ynrd.entrepreneur.R;
import com.ynrd.entrepreneur.utils.CountDownTimerUtils;
public class RegisterActivity extends BaseCommonActivity implements View.OnClickListener {
private Button bu_register_code;
private TextView tv_register_login_password;
private boolean isHideFirst = true;// 輸入框密碼是否是隱藏的,默認(rèn)為true
private boolean isFirst = true;// 是否密碼登錄,默認(rèn)是驗(yàn)證碼登錄
private ImageView imageView;
private EditText et_password_code;
private LinearLayout linear_password_code,password_liner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
}
@Override
protected void initViews() {
super.initViews();
//獲取驗(yàn)證碼
bu_register_code = findViewById(R.id.bu_register_code);
tv_register_login_password = findViewById(R.id.tv_register_login_password);
imageView = findViewById(R.id.imageView);
et_password_code = findViewById(R.id.et_password_code);
linear_password_code = findViewById(R.id.linear_password_code);
password_liner = findViewById(R.id.password_liner);
}
@Override
protected void initListeners() {
super.initListeners();
bu_register_code.setOnClickListener(this);
tv_register_login_password.setOnClickListener(this);
imageView.setOnClickListener(this);
imageView.setImageResource(R.drawable.close);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bu_register_code:
//驗(yàn)證碼方法 后期調(diào)用服務(wù)器
CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(bu_register_code, 30000, 1000);
mCountDownTimerUtils.start();
break;
case R.id.tv_register_login_password:
if (isFirst == true){
//切換成密碼登錄
linear_password_code.setVisibility(View.GONE);
password_liner.setVisibility(View.VISIBLE);
tv_register_login_password.setText("使用短信驗(yàn)證碼登錄 >");
isFirst = false;
}else{
//切換成驗(yàn)證碼登錄
linear_password_code.setVisibility(View.VISIBLE);
password_liner.setVisibility(View.GONE);
tv_register_login_password.setText("使用密碼登錄 >");
isFirst = true;
}
break;
case R.id.imageView:
if (isHideFirst == true) {
imageView.setImageResource(R.drawable.open);
//密文
HideReturnsTransformationMethod method1 = HideReturnsTransformationMethod.getInstance();
et_password_code.setTransformationMethod(method1);
isHideFirst = false;
} else {
imageView.setImageResource(R.drawable.close);
//密文
TransformationMethod method = PasswordTransformationMethod.getInstance();
et_password_code.setTransformationMethod(method);
isHideFirst = true;
}
// 光標(biāo)的位置
int index = et_password_code.getText().toString().length();
et_password_code.setSelection(index);
break;
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義View實(shí)現(xiàn)圓形加載進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)圓形加載進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
Android 側(cè)滑關(guān)閉Activity的實(shí)例
這篇文章主要介紹了Android 側(cè)滑關(guān)閉Activity的實(shí)例的相關(guān)資料,好的手機(jī)現(xiàn)在沒有物理返回鍵,或者說統(tǒng)一Android 與IOS 軟件功能的時(shí)候,需要側(cè)滑關(guān)閉,需要的朋友可以參考下2017-07-07
Android ListView的item中嵌套ScrollView的解決辦法
有時(shí)候,listview 的item要顯示的字段比較多,考慮到顯示問題,item外面不得不嵌套ScrollView來實(shí)現(xiàn),糾結(jié)怎么解決此問題呢?下面小編給大家分享下Android ListView的item中嵌套ScrollView的解決辦法,感興趣的朋友一起看看吧2016-10-10
Android Studio 中aidl的自定義類的使用詳解
這篇文章主要介紹了Android Studio 中aidl的自定義類的使用詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android常用三方庫混淆規(guī)則整理(小結(jié))
這篇文章主要介紹了Android常用三方庫混淆規(guī)則整理(小結(jié)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07
詳解Android Studio正式簽名進(jìn)行調(diào)試的實(shí)現(xiàn)步驟
這篇文章主要介紹了詳解Android Studio正式簽名進(jìn)行調(diào)試的實(shí)現(xiàn)步驟的相關(guān)資料,需要的朋友可以參考下2017-07-07
Android HTTP發(fā)送請求和接收響應(yīng)的實(shí)例代碼
Android HTTP請求和接收響應(yīng)實(shí)例完整的Manifest文件如下,感興趣的朋友可以參考下哈,希望對大家有所幫助2013-06-06
深入理解Android中Scroller的滾動(dòng)原理
今天給大家講解的是Scroller類的滾動(dòng)實(shí)現(xiàn)原理,可能很多朋友不太了解該類是用來干嘛的,但是研究Launcher的朋友應(yīng)該對他很熟悉,Scroller類是滾動(dòng)的一個(gè)封裝類,可以實(shí)現(xiàn)View的平滑滾動(dòng)效果,而我們今天就來探究一下為什么Scroller能夠?qū)崿F(xiàn)平滑滾動(dòng)。2016-08-08

