代码改变EditText的password属性值并可查看密码
2016-03-31 15:40
453 查看
代码改变EditText的password属性值并可查看密码,就是在设置密码的时候有的时候需要显示密码,但是有的时候需要隐藏密码
效果去下:
但是在勾选了显示密码时就会显示密码
不废话,看代码。
先是xml代码部分:
<LinearLayout 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:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="请输入密码"
android:textSize="20dp" />
<EditText
android:id="@+id/ed_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码" >
<requestFocus />
</EditText>
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:layout_marginLeft="10dip"
android:layout_marginTop="10dip"
android:text="显示密码" />
</LinearLayout>
下面是java代码部分:
package com.eulav.dialog;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.Selection;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
public class MainActivity extends Activity implements OnCheckedChangeListener {
EditText ed_password;
CheckBox checkbox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed_password = (EditText) findViewById(R.id.ed_password);
ed_password.setInputType(InputType.TYPE_CLASS_TEXT
| InputType.TYPE_TEXT_VARIATION_PASSWORD);
checkbox = (CheckBox) findViewById(R.id.checkBox);
checkbox.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// 显示为普通文本
ed_password
.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
// 使光标始终在最后位置
Editable etable = ed_password.getText();
Selection.setSelection(etable, etable.length());
} else {
// 显示为密码
ed_password.setInputType(InputType.TYPE_CLASS_TEXT
| InputType.TYPE_TEXT_VARIATION_PASSWORD);
// 使光标始终在最后位置
Editable etable = ed_password.getText();
Selection.setSelection(etable, etable.length());
}
}
}
源码免积分下载:点击打开链接
效果去下:
但是在勾选了显示密码时就会显示密码
不废话,看代码。
先是xml代码部分:
<LinearLayout 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:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="请输入密码"
android:textSize="20dp" />
<EditText
android:id="@+id/ed_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码" >
<requestFocus />
</EditText>
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:layout_marginLeft="10dip"
android:layout_marginTop="10dip"
android:text="显示密码" />
</LinearLayout>
下面是java代码部分:
package com.eulav.dialog;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.Selection;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
public class MainActivity extends Activity implements OnCheckedChangeListener {
EditText ed_password;
CheckBox checkbox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed_password = (EditText) findViewById(R.id.ed_password);
ed_password.setInputType(InputType.TYPE_CLASS_TEXT
| InputType.TYPE_TEXT_VARIATION_PASSWORD);
checkbox = (CheckBox) findViewById(R.id.checkBox);
checkbox.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// 显示为普通文本
ed_password
.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
// 使光标始终在最后位置
Editable etable = ed_password.getText();
Selection.setSelection(etable, etable.length());
} else {
// 显示为密码
ed_password.setInputType(InputType.TYPE_CLASS_TEXT
| InputType.TYPE_TEXT_VARIATION_PASSWORD);
// 使光标始终在最后位置
Editable etable = ed_password.getText();
Selection.setSelection(etable, etable.length());
}
}
}
源码免积分下载:点击打开链接
相关文章推荐
- 超全面的.NET GDI+图形图像编程教程
- javaMail创建邮件和发送邮件总结
- Java中 一个类实现了某接口 则必须实现该接口中的所有方法么?
- 使用github出现的问题
- base64转码工具类
- EditPlus配置代码快速行注释和取消行注释
- JAVA编程思想第四版中net.mindview.util.*包的导入
- JAVA多文件压缩函数
- java学习一:java写程序的一些基本的注意事项
- Kmeans算法java实现
- 11个Visual Studio代码性能分析工具
- PHP操作FTP类 (上传、下载、移动、创建等)
- python webdriver 简单框架
- C++ STL 容器、迭代器、适配器
- Struts2 入门干货
- java字符串操作
- 主要几个发行版的 gcc 对 c++ 新标准的支持情况区别
- Eclipse Java EE透视图下看不见Referenced Libraries解决办法
- Spring MVC控制器用@ResponseBody声明返回json数据报406的问题
- 深刻理解Python中的元类(metaclass)