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 }
时间: 2024-07-30 10:17:03

Android 输入密码 隐藏显示输入的字符串的相关文章

extjs 密码遮罩插件,输入密码先显示输入字符,然后变为*,以插件形式

2294508843qq extjs 密码遮罩插件,输入密码先显示输入字符,然后变为*,以插件形式

Android输入密码时显示与隐藏

在登录或者注册的时候,有些软件中,需求要求密码可以查看和隐藏,其实实现起来也很简单. 首先定义布局: <LinearLayout android:layout_width="fill_parent" android:layout_height="50.0dip" android:layout_marginLeft="15dip" android:layout_marginRight="15dip" android:bac

Android Fragment隐藏显示调用的方法

//在Activity中加载显示隐藏Fragment import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.content.Intent; import android.os.Bundle; import com.commons.Config; import com.geluyawangluo.withtheni

Android 动态隐藏显示导航栏,状态栏

Talk is cheap, show me the code. --Linus Torvalds Okay, here: 一.导航栏: [java] view plain copy private void hideNavigationBar() { View decorView = getWindow().getDecorView(); int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULL

python getpass模块:隐藏不显示输入的密码

不知道为什么,本机测试必须要在debug模式下才正常运行.. import getpass #用于隐藏用户输入的字符串,常用来接收密码 def checkuser(user,passwd): if user == 'chenhang' and passwd == '123456': return True else: return False if __name__ == "__main__": userr = input('Input the user:') passwdd = ge

第一个app-进阶:1、让EditText屏蔽软键盘仍能光标闪动:2、EditText 的inputType参数类型(密码方式-隐藏显示、只允许输入数字等等)

EditText屏蔽软键盘后,光标消失问题 开始我有个需求:屏蔽系统软键盘 于是找到并使用这个方法: //edit_text1.setInputType(InputType.TYPE_NULL); //屏蔽软键盘弹出//edit_text2.setInputType(InputType.TYPE_NULL); //屏蔽软键盘弹出//edit_text3.setInputType(InputType.TYPE_NULL); //屏蔽软键盘弹出 让三个EditText都成功地没有软键盘弹出. 后来我

Android 仿QQ浏览器WebView,滑动隐藏显示ActionBar效果

Android 仿QQ浏览器,滑动隐藏显示ActionBar效果. 往上推,是一个ScrollView会将,actionbar以及内容往上推,当actionbar消失后,将滚动Webview的内容. 此效果是基于QuickReturnHeader源码,修改而来的,代码也不多,实现方法比较简单. 直接上demo:http://download.csdn.net/detail/xufeifandj/8388493 直接看效果图:

angularjs2 ng2 密码隐藏显示

代码结合ionic2开发移动端项目,注册页的密码的input一般用password, 但是用户输入密码时可能会输入错误,需要显示成text. 如图: 首先,输入框的类型判断: 1 <ion-input type="{{pwshow?'text':'password'}}" placeholder="输入密码"></ion-input> 然后,添加眼睛的点击事件,ngClass判断图标样式: 1 <a href="javascr

Android中EditText显示明文与密文的两种方式

效果图   布局文件 <?xml version="1.0" encoding="utf-8"?> <!-- Android中EditText显示明文与密文的两种方式 --> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/to