Android中實現密碼的隱藏和顯示的示例
更新時間:2017年09月13日 16:22:58 作者:L展菲Q
本篇文章主要介紹了Android中實現密碼的隱藏和顯示的示例,非常具有實用價值,需要的朋友可以參考下
在Android開發(fā)中,需要密碼的隱藏和顯示,下面就和大家分享一下使用方法:
xml代碼:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/white"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="新密碼"
android:textColor="@color/black"
android:textSize="18dp"
android:gravity="center_vertical"
android:layout_marginLeft="15dp"/>
<EditText
android:id="@+id/newpassword"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:inputType="textPassword"
android:hint="請設置登錄密碼"
android:background="@null"/>
<CheckBox
android:id="@+id/CheckBox"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="15dp"
android:textSize="16dp"
android:text="顯示"
/>
</LinearLayout>
隱藏圖標代碼
android:button="@null"
JAVA代碼:
/**
* Created by fby on 2017/9/11.
*/
public class ChargepsdActivity extends Activity {
private EditText editText;
private CheckBox checkBox;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chargepsd);
editText = (EditText) findViewById(R.id.newpassword);
checkBox = (CheckBox) findViewById(R.id.CheckBox);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
//如果選中,顯示密碼
editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}else{
//否則隱藏密碼
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
}
});
}
}
效果展示:


以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android實現基于ViewPager的無限循環(huán)自動播放帶指示器的輪播圖CarouselFigureView控件
這篇文章主要介紹了Android實現基于ViewPager的無限循環(huán)自動播放帶指示器的輪播圖CarouselFigureView控件,需要的朋友可以參考下2017-02-02
Android TextWatcher三個回調以及監(jiān)聽EditText的輸入案例詳解
這篇文章主要介紹了Android TextWatcher三個回調以及監(jiān)聽EditText的輸入案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-08-08
Android開發(fā)之搜索框SearchView用法示例
這篇文章主要介紹了Android開發(fā)之搜索框SearchView用法,結合實例形式分析了Android搜索框SearchView的基本功能、用法及相關操作注意事項,需要的朋友可以參考下2019-03-03

