Android 输入密码 隐藏显示输入的字符串

首先是xml布局的设计

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <TextView android:id="@+id/tv"
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"
 />
<EditText android:id="@+id/et"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"    
/>
 
<CheckBox android:id="@+id/cb"    
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="显示或隐藏密码"
/>
<Button android:id="@+id/bt"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="显示输入的字符串"
/>
</LinearLayout>

然后是activity

 1 package com.pocketdigi.edit;
 2  
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.text.method.HideReturnsTransformationMethod;
 6 import android.text.method.PasswordTransformationMethod;
 7 import android.view.View;
 8 import android.view.View.OnClickListener;
 9 import android.widget.Button;
10 import android.widget.CheckBox;
11 import android.widget.CompoundButton;
12 import android.widget.EditText;
13 import android.widget.TextView;
14 import android.widget.CompoundButton.OnCheckedChangeListener;
15  
16 public class main extends Activity {
17     /** Called when the activity is first created. */
18     EditText et;
19     CheckBox cb;
20     TextView tv;
21     Button bt;
22     @Override
23     public void onCreate(Bundle savedInstanceState) {
24         super.onCreate(savedInstanceState);
25         setContentView(R.layout.main);
26         et=(EditText)findViewById(R.id.et);
27         et.setTransformationMethod(PasswordTransformationMethod.getInstance());
29         cb=(CheckBox)findViewById(R.id.cb);
30         tv=(TextView)findViewById(R.id.tv);
31         bt=(Button)findViewById(R.id.bt);
32         bt.setOnClickListener(show);
33         cb.setOnCheckedChangeListener(listener);
34     }
35     OnCheckedChangeListener listener=new OnCheckedChangeListener(){
36  
37         @Override
38         public void onCheckedChanged(CompoundButton buttonView,
39                 boolean isChecked) {
40             // TODO Auto-generated method stub
41             if(isChecked){
42                 et.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
43                 //如果选中,显示密码
44             }else{
45                  et.setTransformationMethod(PasswordTransformationMethod.getInstance());
46                  //否则隐藏密码
47             }
48             
49         }
50         
51     };
52     OnClickListener show=new OnClickListener(){
53  
54         @Override
55         public void onClick(View v) {
56             // TODO Auto-generated method stub
57             tv.setText(et.getText());
58             //点Button后,TextView显示输入的字符串
59         }
60         
61     };
62 }

 

posted on 2016-03-23 16:34  ShamSilence  阅读(957)  评论(0编辑  收藏  举报

导航