这次的功能比较简单,所以就直奔主题吧
代码实现
package com.example.androidtest;
import android.app.Activity;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
public class MainActivity extends Activity implements OnClickListener {
private EditText et_passwd;
private ImageView iv_showPassword;//密码是否明文显示
private Boolean showPassword = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_passwd = (EditText) findViewById(R.id.et_passwd);
iv_showPassword = (ImageView) findViewById(R.id.iv_showPassword);
iv_showPassword.setImageDrawable(getResources().getDrawable(R.drawable.eye_c));
iv_showPassword.setOnClickListener(this);
}
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
switch (view.getId()) {
case R.id.iv_showPassword:
if (showPassword) {// 显示密码
iv_showPassword.setImageDrawable(getResources().getDrawable(R.drawable.eye_o));
et_passwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
et_passwd.setSelection(et_passwd.getText().toString().length());
showPassword = !showPassword;
} else {// 隐藏密码
iv_showPassword.setImageDrawable(getResources().getDrawable(R.drawable.eye_c));
et_passwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
et_passwd.setSelection(et_passwd.getText().toString().length());
showPassword = !showPassword;
}
break;
default:
break;
}
}
}
布局文件
<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"
tools:context="com.example.androidtest.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@drawable/kuang_login"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:background="@drawable/mobile" />
<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:background="@null"
android:ems="11"
android:hint="请输入账号/手机号/邮箱"
android:maxLines="1"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:background="@drawable/lock" />
<EditText
android:id="@+id/et_passwd"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:background="@null"
android:ems="16"
android:hint="请输入密码"
android:inputType="textPassword"
android:maxLines="1"
android:minEms="6"
android:textSize="12sp" />
<ImageView
android:id="@+id/iv_showPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>