我有一个带有用户密码的TextView,并且必须让他们显示/隐藏它。因此,我创建了一个具有显示/隐藏功能的按钮。
当密码处于隐藏模式时,我需要显示相同数量的点,例如五个点。
我写了下一个代码,但是当密码试图隐藏时,我得到了一个错误。 (写在下面)
有人可以帮我吗?
代码:
public class CustomPasswordTransformation extends PasswordTransformationMethod {
@Override
public CharSequence getTransformation(CharSequence source, View view) {
return new PasswordCharSequence(source);
}
private class PasswordCharSequence implements CharSequence {
private CharSequence mSource;
public PasswordCharSequence(CharSequence source) {
mSource = source;
}
public char charAt(int index) {
return '*';
}
public int length() {
return 5;
}
public CharSequence subSequence(int start, int end) {
return mSource.subSequence(start, end);
}
}
}
这是错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.debug, PID: 19891
java.lang.IndexOutOfBoundsException
at android.graphics.Paint.getRunAdvance(Paint.java:2346)
at android.text.TextLine.handleText(TextLine.java:748)
at android.text.TextLine.handleRun(TextLine.java:898)
at android.text.TextLine.measureRun(TextLine.java:417)
at android.text.TextLine.measure(TextLine.java:296)
at android.text.TextLine.metrics(TextLine.java:270)
at android.text.Layout.getLineExtent(Layout.java:1053)
at android.text.Layout.getLineWidth(Layout.java:1019)
at android.widget.TextView.desired(TextView.java:6811)
at android.widget.TextView.onMeasure(TextView.java:6877)
at android.view.View.measure(View.java:18850)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5963)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1112)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:632)
at android.view.View.measure(View.java:18850)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5963)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18850)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:677)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:480)
at android.view.View.measure(View.java:18850)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5963)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18850)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5963)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1112)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:632)
at android.view.View.measure(View.java:18850)
at android.support.v7.widget.RecyclerView$LayoutManager.measureChildWithMargins(RecyclerView.java:8994)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1585)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3529)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4082)
at android.view.View.layout(View.java:16692)
at android.view.ViewGroup.layout(ViewGroup.java:5449)
at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:606)
at android.view.View.layout(View.java:16692)
at android.view.ViewGroup.layout(ViewGroup.java:5449)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16692)
at android.view.ViewGroup.layout(ViewGroup.java:5449)
at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1769)
at android.view.View.layout(View.java:16692)
at android.view.ViewGroup.layout(ViewGroup.java:5449)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16692)
at android.view.ViewGroup.layout(ViewGroup.java:5449)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(Fram
答案 0 :(得分:0)
您需要返回在输入中输入的字符序列的长度。
在代码中,您需要在长度函数中将5替换为source.length()。
公共类CustomPasswordTransformation扩展了PasswordTransformationMethod {
@Override
public CharSequence getTransformation(CharSequence source, View view) {
return new PasswordCharSequence(source);
}
private class PasswordCharSequence implements CharSequence {
private CharSequence mSource;
public PasswordCharSequence(CharSequence source) {
mSource = source;
}
public char charAt(int index) {
return '*';
}
public int length() {
return source.length();
}
public CharSequence subSequence(int start, int end) {
return mSource.subSequence(start, end);
}
}
}