Android-EditText在类型为密码时获取物理长度

Android-EditText在类型为密码时获取物理长度,android,Android,我试图得到EditText值的物理长度(以像素为单位),但当它是密码时,我得到的是密码值的长度,而不是所有点的长度 content = editText.getText(); Rect bounds = new Rect(); Paint textPaint = editText.getPaint(); textPaint.getTextBounds(content, 0, content.length(), bounds); int width = bounds.width();// This

我试图得到EditText值的物理长度(以像素为单位),但当它是密码时,我得到的是密码值的长度,而不是所有点的长度

content = editText.getText();
Rect bounds = new Rect();
Paint textPaint = editText.getPaint();
textPaint.getTextBounds(content, 0, content.length(), bounds);
int width = bounds.width();// This is the the length of the contents in pixels(painted).
例如,我在编辑文本中输入“Hello”,当我尝试获取其长度时,与执行以下操作相同:

paint.measureText("Hello");
但我想知道这个的长度
“Password:”是我添加的前缀,它不在EditText值本身中

如果有人能帮助我,我将不胜感激

Rect bounds = new Rect(); 
Paint textPaint = edittext.getPaint(); 
textPaint.getTextBounds(edittext.getText().toString(), 
0,edittext.getText().length(), bounds); 
int height = bounds.height();
int width = bounds.width(); Toast.makeText(LoginActivity.this, ""+width, 
Toast.LENGTH_SHORT).show();
使用


*不幸的是,它只返回绘制字体的长度,而不是点

content = editText.getText();
Rect bounds = new Rect();
Paint textPaint = editText.getPaint();
textPaint.getTextBounds(content, 0, content.length(), bounds);
int width = bounds.width();// This is the the length of the contents in pixels(painted).

希望有帮助。

*不幸的是,它只返回绘制字体的长度,而不是点

content = editText.getText();
Rect bounds = new Rect();
Paint textPaint = editText.getPaint();
textPaint.getTextBounds(content, 0, content.length(), bounds);
int width = bounds.width();// This is the the length of the contents in pixels(painted).

希望有帮助。

我猜密码编辑文本打印的实际字符将是Unicode项目符号字符之一。例如,如果它是U+2022,那么你可以使用

StringBuilder sb = new StringBuilder();
int n = inputPassword.getText().length();
while (n-- > 0) sb.append('\u2022');

double width = inputPassword.getPaint()
        .measureText(sb.toString());

我猜密码EditText打印的实际字符将是Unicode项目符号字符之一。例如,如果它是U+2022,那么你可以使用

StringBuilder sb = new StringBuilder();
int n = inputPassword.getText().length();
while (n-- > 0) sb.append('\u2022');

double width = inputPassword.getPaint()
        .measureText(sb.toString());


那么,在你的例子中,你想得到“5”(点的数量)?好吧,我没有很好地解释我自己,我想得到像素的长度:)什么的像素长度?整个编辑文本?第一个点的开始到最后一个点的结束?是的,并且您应该知道,
edittext.getText()方法中没有返回“Password:”
method试试这个?那么,在你的例子中,你想得到“5”(点的数量)?好吧,我没有很好地解释我自己,我想得到像素的长度:)什么的像素长度?整个编辑文本?第一个点的开始到最后一个点的结束?是的,并且您应该知道,
edittext.getText()方法中没有返回“Password:”
method试试这个?我已经试过了,但它返回隐藏文本的像素长度,而不是点的长度:/我已经试过了,但它返回隐藏文本的像素长度,而不是点的长度:/I不需要字符数,但像素宽度Rect bounds=new Rect();Paint textPaint=edittext.getPaint();textPaint.getTextBounds(edittext.getText().toString(),0,edittext.getText().length(),bounds);int height=bounds.height();int width=bounds.width();Toast.makeText(LoginActivity.this,“+width,Toast.LENGTH_SHORT.show();我不想要字符数,但像素的宽度Rect bounds=new Rect();Paint textPaint=edittext.getPaint();textPaint.getTextBounds(edittext.getText().toString(),0,edittext.getText().length(),bounds);int height=bounds.height();int width=bounds.width();Toast.makeText(LoginActivity.this,“+width,Toast.LENGTH_SHORT.show();我想这就是我想要的谢谢!但是您知道是否可以检测EditText的最后一个字符何时变成点吗?:)可悲的是,我不知道如何做到这一点。@LeoAso这是一个明智的选择@RayKytto。@我刚刚试过,但即使我更改了值,应用程序等待隐藏文本的时间保持不变。我真的不想改变它隐藏的时间,只是检测它什么时候隐藏我认为这是我想要的谢谢!但是您知道是否可以检测EditText的最后一个字符何时变成点吗?:)可悲的是,我不知道如何做到这一点。@LeoAso这是一个明智的选择@RayKytto。@我刚刚试过,但即使我更改了值,应用程序等待隐藏文本的时间保持不变。我真的不想改变它隐藏的时间,只是检测它何时隐藏