天天看點

Android打造帶删除的EditText并且實作輸入框密碼顯示、隐藏

Android打造帶删除的EditText并且實作輸入框密碼顯示、隐藏

實作這個效果流程如下:

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.cb_show_pwd);
		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/cb_show_pwd"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"	    
	    />
</LinearLayout>