通过ChekBox设置EditText内容是否呈现密码显示

mac2022-06-30  12

1.效果图

                    

 

 

 

2.代码:

  

package com.example.democheckbox; import android.os.Bundle; import android.app.Activity; import android.text.method.HideReturnsTransformationMethod; import android.text.method.PasswordTransformationMethod; import android.view.Menu; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.EditText; public class DemoCheckBox extends Activity { EditText et; CheckBox cb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_demo_check_box); et = (EditText)findViewById(R.id.ed); cb = (CheckBox)findViewById(R.id.cb); cb.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub //设置EditText中内容可见 if(cb.isChecked()){ et.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); } //设置EditText中内容不可见 else{ et.setTransformationMethod(PasswordTransformationMethod.getInstance()); } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_demo_check_box, menu); return true; } }

3.XML显示布局

   

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="fill_vertical" tools:context=".DemoCheckBox" > <!-- android:password="true" 设置默认输入时输入的 密码显示 --> <EditText android:id="@+id/ed" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:width="200dip" android:hint="请输入密码" android:password="true"/> <CheckBox android:id="@+id/cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="可见" android:layout_below="@id/ed" android:layout_centerHorizontal="true" android:layout_centerVertical="true"/> </RelativeLayout>

 

转载于:https://www.cnblogs.com/merryjd/archive/2012/12/26/2833775.html

相关资源:Android中EditText密码样式设置
最新回复(0)