我有一个自定义的TextInputEditText
,我用它作为密码字段。但是,光标不会保持在原位并始终返回到edittext
的开头。
自定义视图如下所示:
public class CustomInputEditTextWithSecret extends TextInputEditText {
private final String SECRET = "123456";
public CustomInputEditTextWithSecret(Context context) {
super(context);
}
public CustomInputEditTextWithSecret(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomInputEditTextWithSecret(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public Editable getText() {
Editable s = (Editable) super.getText();
if(s!=null && s.length()>0) {
return new SpannableStringBuilder(SECRET+s);
}
return s;
}
}
当我在XML布局中使用它时,它看起来像这样:
<android.support.design.widget.TextInputLayout
android:id="@+id/password_layout"
style="@style/FirebaseUI.TextInputLayout.PasswordField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/fui_field_padding_vert"
app:layout_constraintTop_toBottomOf="@+id/welcome_back_password_body"
app:passwordToggleEnabled="true">
<com.firebase.ui.auth.ui.customviews.CustomInputEditTextWithSecret
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/password"
/>
</android.support.design.widget.TextInputLayout>
if you need the entire layout here it is:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
style="@style/FirebaseUI.WrapperStyle"
android:layout_height="wrap_content">
<TextView
android:id="@+id/heading"
style="@style/FirebaseUI.Text.Heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/fui_welcome_back_email_header"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/welcome_back_password_body"
style="@style/FirebaseUI.Text.BodyText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="false"
app:layout_constraintTop_toBottomOf="@+id/heading"
tools:text="@string/fui_welcome_back_password_prompt_body" />
<android.support.design.widget.TextInputLayout
android:id="@+id/password_layout"
style="@style/FirebaseUI.TextInputLayout.PasswordField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/fui_field_padding_vert"
app:layout_constraintTop_toBottomOf="@+id/welcome_back_password_body"
app:passwordToggleEnabled="true">
<com.firebase.ui.auth.ui.customviews.CustomInputEditTextWithSecret
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/password"
/>
</android.support.design.widget.TextInputLayout>
<TextView
android:id="@+id/trouble_signing_in"
style="@style/FirebaseUI.Text.Link"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/fui_field_padding_vert"
android:text="@string/fui_trouble_signing_in"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button_done"
app:layout_constraintTop_toBottomOf="@+id/password_layout" />
<Button
android:id="@+id/button_done"
style="@style/FirebaseUI.Button"
android:text="@string/fui_sign_in_default"
app:layout_constraintStart_toEndOf="@+id/trouble_signing_in"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/trouble_signing_in" />
</android.support.constraint.ConstraintLayout>
</ScrollView>
它是密码字段的一些安全功能吗?我究竟做错了什么?在自定义视图中,我要做的是在每个密码的末尾附加一个字符串。
这是发生了什么的demo video(该视频称为demo.mp4)。
更新:如果我注释掉getText()覆盖,那么它工作正常。所以我必须在那里做错事。每次开发人员调用getText()时如何附加字符串?
我有标准的TextInputEditText
这个问题。明确添加android:inputType="text"
修复它给我:
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/login_username_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/login_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
android:inputType="text" /> <!-- explicit inputType fixes it for me -->
</com.google.android.material.textfield.TextInputLayout>
看起来也许你正在使用密码字段..设置android:intputType="textPassword"
也适用于我。