Android 輸入密碼 隱藏顯示輸入的字串

來源:互聯網
上載者:User

標籤:

首先是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     @Override23     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         @Override38         public void onCheckedChanged(CompoundButton buttonView,39                 boolean isChecked) {40             // TODO Auto-generated method stub41             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         @Override55         public void onClick(View v) {56             // TODO Auto-generated method stub57             tv.setText(et.getText());58             //點Button後,TextView顯示輸入的字串59         }60         61     };62 }

 

Android 輸入密碼 隱藏顯示輸入的字串

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.