实现这个效果其实很简单:

1,重写EditText在后面加一个drawable

2, 显示隐藏密码通过调用setTransformationMethod方法来实现


1,自定义EditText

package com.example.myhandler;import android.content.Context;import android.graphics.Rect;import android.graphics.drawable.Drawable;import android.text.Editable;import android.text.TextUtils;import android.text.TextWatcher;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.widget.EditText;/** * 给EditText末尾加删除按钮 *  * @author 清风徐来 *  */public class CustomEditText extends EditText {private Drawable mDeleteImage;// 删除的按钮public CustomEditText(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}public CustomEditText(Context context, AttributeSet attrs) {super(context, attrs);init();}public CustomEditText(Context context) {this(context, null);}private void init() {addTextChangedListener(new TextWatcher() {@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {mDeleteImage = TextUtils.isEmpty(s) ? null : getContext().getResources().getDrawable(R.drawable.delete);setCompoundDrawablesWithIntrinsicBounds(null, null, mDeleteImage, null);//添加drawable , position = right}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Overridepublic void afterTextChanged(Editable s) {}});}@Overridepublic boolean onTouchEvent(MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_UP:if (mDeleteImage != null && !TextUtils.isEmpty(getText())) {//如果删除图片显示,并且输入框有内容if(event.getX() > ( getWidth() - getTotalPaddingRight()) && event.getX() < (getWidth() - getPaddingRight()))//只有在这区域能触发清除内容的效果getText().clear();}break;}return super.onTouchEvent(event);}}

附图说明点击区域怎么计算的:



------------------------------------------------------------------------------------------------------------------

2,实现隐藏显示密码

package com.example.myhandler;import android.os.Bundle;import android.os.Message;import android.text.TextUtils;import android.text.method.HideReturnsTransformationMethod;import android.text.method.PasswordTransformationMethod;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.EditText;public class MainActivity extends BaseActivity {private EditText mEditText;// 输入框private CheckBox mCheckBox;// 是否显示密码@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mEditText = (EditText) findViewById(R.id.et_input_password);mCheckBox = (CheckBox) findViewById(R.id.check_isShown);mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked)mEditText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//显示密码else {mEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());//隐藏密码}mEditText.setSelection(TextUtils.isEmpty(mEditText.getText()) ? 0 : mEditText.length());//光标挪到最后}});}@Overridepublic void handleMessage(Message message) {}}

最后贴上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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.myhandler.MainActivity"    android:orientation="vertical"     >    <com.example.myhandler.CustomEditText        android:id="@+id/et_input_password"        android:layout_width="match_parent"        android:layout_height="40dp"        android:inputType="textPassword"         android:singleLine="true"        android:imeOptions="actionDone"        android:background="@drawable/input_type"        /><CheckBox     android:id="@+id/check_isShown"    android:layout_width="wrap_content"    android:layout_height="wrap_content"        /></LinearLayout>


BaseActivity是为了防止Handler内存泄漏而写的,采用了弱引用,如果有需要请留言

更多相关文章

  1. android软键盘遮挡布局
  2. android键盘的Done按钮
  3. [置顶] Android(安卓)按钮点击两次触发不同的事件
  4. android学习笔记一
  5. DataBinding 填坑总结
  6. Android(安卓)AlertDialog对话框详解及实例
  7. AndroidGUI14:各种Dialog常用技巧
  8. Android(安卓)系统原生dialog使用
  9. Android(安卓)各控件的使用 - 按钮(Button)

随机推荐

  1. 外行人都能看懂的SpringCloud,错过了血亏!
  2. 图说yield
  3. 开发者必备Linux命令
  4. 翻译社重大改版
  5. JVM故障诊断和处理工具
  6. Linux防火墙Firewall和Iptables的使用
  7. macrotask与microtask
  8. 开发者必备Docker命令
  9. [一些勘误]ubuntu16.04的Python版本,Pytho
  10. 【MySQL】 explicit_defaults_for_timest