实现这个效果其实很简单:
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() { @Override public 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 } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { } }); } @Override public 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;// 是否显示密码 @Override protected 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() { @Override public 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());//光标挪到最后 } }); } @Override public 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>