我想在按下 ImageView 时显示我的密码,当我在那时释放我的触摸时,它应该隐藏。例如,如果我按 imageview 2 分钟,那么密码应该可见 2 分钟,当我释放它时应该隐藏它。
我有以下代码复选框
EditText mEtPwd;
CheckBox mCbShowPwd;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// get the password EditText
mEtPwd = (EditText) findViewById(R.id.etPassword);
// get the show/hide password Checkbox
mCbShowPwd = (CheckBox) findViewById(R.id.cbShowPwd);
// add onCheckedListener on checkbox
// when user clicks on this checkbox, this is the handler.
mCbShowPwd.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// checkbox status is changed from uncheck to checked.
if (!isChecked) {
// show password
mEtPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
} else {
// hide password
mEtPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
}
});
}
CheckBox
仅使用ImageView
并为其setTouchListener()
,为 MotionEvent.ACTION_DOWN
设置mPassField.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
以显示密码。
并mPassField.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
MotionEvent.ACTION_UP
将其藏回去。
例如
imageView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getActionMasked();
switch (action) {
case MotionEvent.ACTION_DOWN:
// TODO show password
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_OUTSIDE:
// TODO mask password
break;
}
return v.onTouchEvent(event);
}
});
您需要更改 EditText 的输入类型。
使其成为密码字段(隐藏密码):
mEtPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
mEtPwd.setSelection(mEtPwd.getText().length());
使其成为普通字段(显示密码):
mEtPwd.setInputType(InputType.TYPE_CLASS_TEXT);
mEtPwd.setSelection(mEtPwd.getText().length());
以下是用于将 editeText 内容转换为位图并在长按时将其放入图像视图的代码。
enter code here
/** Called when the activity is first created. */
static Bitmap bmp;
static EditText et;
static ImageView iv;
static Canvas ivCanvas; // We'll be using our own Canvas.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText et = (EditText) findViewById(R.id.editText1);
ImageView iv = (ImageView) findViewById(R.id.imageView1);
// Move this up to onCreate
Bitmap ab = BitmapFactory.decodeResource(getResources(),
(R.drawable.ger));
bmp = convertToMutable(ab); // Initialize it here with the contents of
// ab. This effectively clones it and makes
// it mutable.
ab = null; // Dispose of ab.
ivCanvas = new Canvas(bmp); // Create our Canvas!
et.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
//ADD HERE ABOUT CUT COPY PASTE
// TODO Auto-generated method stub
updateCanvas();
}
});
public void updateCanvas() {
ivCanvas.drawColor(Color.BLACK);
ivCanvas.drawBitmap(bmp, 0, 0, null);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
ivCanvas.drawText(et.getText().toString(), 10, 10, paint);
// Everything has been drawn to bmp, so we can set that here, now.
iv.setImageBitmap(bmp);
// Removed the "catch" blocks so you can actually know when you're
// getting errors! Feel free to re-add later.
}