自定义带隐藏EditText
public class EyeAndClearEditText extends LinearLayout {
private Context context;
private boolean open = true;
private ClearEditText clearEditText;
private ImageView imageView;
public EyeAndClearEditText(Context context) {
this(context, null);
}
public EyeAndClearEditText(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public EyeAndClearEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
this.setOrientation(HORIZONTAL);
initEditText();
initImageView();
}
private void initImageView() {
imageView = new ImageView(context);
imageView.setImageResource(R.drawable.icon_beye_n);
this.addView(imageView);
LinearLayout.LayoutParams layoutParams = (LayoutParams) imageView.getLayoutParams();
layoutParams.gravity = Gravity.CENTER_VERTICAL;
layoutParams.width = LayoutParams.WRAP_CONTENT;
layoutParams.height = LayoutParams.WRAP_CONTENT;
layoutParams.rightMargin = DensityUtil.dip2px(context, 16);
layoutParams.leftMargin = DensityUtil.dip2px(context, 16);
imageView.setLayoutParams(layoutParams);
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (open) {
open = false;
clearEditText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
} else {
open = true;
clearEditText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
}
}
});
}
private void initEditText() {
clearEditText = new ClearEditText(context);
this.addView(clearEditText);
LinearLayout.LayoutParams layoutParams = (LayoutParams) clearEditText.getLayoutParams();
layoutParams.width = LayoutParams.WRAP_CONTENT;
layoutParams.height = LayoutParams.MATCH_PARENT;
layoutParams.weight = 1;
clearEditText.setLayoutParams(layoutParams);
clearEditText.setBackground(null);
clearEditText.setMaxLines(1);
}
}
经验证发现要用
setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);