本文共 2934 字,大约阅读时间需要 9 分钟。
package wifi.app.wei.com.myapplication;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.text.InputType;import android.view.View;import android.widget.EditText;import android.widget.ImageView;public class MainActivity extends AppCompatActivity { private ImageView imageView ; private EditText editText ; boolean eyeOpen = false ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main ); imageView = (ImageView) findViewById( R.id.image) ; editText = (EditText) findViewById( R.id.pass_et); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if ( eyeOpen ){ //密码 TYPE_CLASS_TEXT 和 TYPE_TEXT_VARIATION_PASSWORD 必须一起使用 editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); imageView.setImageResource( R.mipmap.eye_close ); eyeOpen = false ; }else { //明文 editText.setInputType( InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD ); imageView.setImageResource( R.mipmap.eye_open ); eyeOpen = true ; } } }); }}
package wifi.app.wei.com.myapplication;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.text.method.HideReturnsTransformationMethod;import android.text.method.PasswordTransformationMethod;import android.view.View;import android.widget.EditText;import android.widget.ImageView;public class MainActivity extends AppCompatActivity { private ImageView imageView ; private EditText editText ; boolean eyeOpen = false ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main ); imageView = (ImageView) findViewById( R.id.image) ; editText = (EditText) findViewById( R.id.pass_et); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if ( eyeOpen ){ //密码 editText.setTransformationMethod(PasswordTransformationMethod.getInstance()); imageView.setImageResource( R.mipmap.eye_close ); eyeOpen = false ; }else { //明文 editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); imageView.setImageResource( R.mipmap.eye_open ); eyeOpen = true ; } } }); }}
转载地址:http://eakia.baihongyu.com/