Android之EditText 为密码输入框时,密码的显示与隐藏

本文介绍了在Android中如何实现EditText作为密码输入框时,密码的显示与隐藏功能。主要方法包括通过调整android:inputType属性和改变android.text.method.TransformationMethod。详细代码实现和解释帮助开发者理解两种不同的实现方式。
摘要由CSDN通过智能技术生成

实现输入框密码文本的显示与隐藏有两种,一种是通过直接改变android:inputType,一种是通过改变android.text.method.TransformationMethod


方式一:改变android:inputType的值


实现代码如下:

	
	/**
	 * 密码显示或隐藏 (切换)
	 */
	private void showOrHide(EditText etPassword){
		//记住光标开始的位置
		int pos = etPassword.getSelectionStart();
		if(etPassword.getInputType()!= (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD)){//隐藏密码
			etPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
		}else{//显示密码
			etPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
		}
		etPassword.setSelection(pos);
		
	}
	

参见:android:inputType

对应的值与描述如下:

Constant Value Description
none 0x00000000 There is no content type. The text is not editable.
text 0x00000001 Just plain old text. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_NORMAL.
textCapCharacters 0x00001001 Can be combined with text and its variations to request capitalization of all characters. Corresponds to TYPE_TEXT_FLAG_CAP_CHARACTERS.
textCapWords 0x00002001 Can be combined with text and its variations to request capitalization of the first character of every word. Corresponds to TYPE_TEXT_FLAG_CAP_WORDS.
textCapSentences 0x00004001 Can be combined with text and its variations to request capitalization of the first character of every sentence. Corresponds to TYPE_TEXT_FLAG_CAP_SENTENCES.
textAutoCorrect 0x00008001 Can be combined with text and its variations to request auto-correction of text being input. Corresponds to TYPE_TEXT_FLAG_AUTO_CORRECT.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值