Android自定义文本输入文本-光标始终停留在起始位置
我有一个自定义的Android自定义文本输入文本-光标始终停留在起始位置,android,android-edittext,Android,Android Edittext,我有一个自定义的textinputtext,用作密码字段。但是,光标不会停留在原来的位置,而是始终返回到编辑文本的开头 自定义视图如下所示: public class CustomInputEditTextWithSecret extends TextInputEditText { private final String SECRET = "123456"; public CustomInputEditTextWithSecret(Context context) {
textinputtext
,用作密码字段。但是,光标不会停留在原来的位置,而是始终返回到编辑文本的开头
自定义视图如下所示:
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;
}
}
<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>
当我在XML布局中使用它时,它如下所示:
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;
}
}
<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.mp4)
更新:如果我注释掉getText()覆盖,那么它可以正常工作。所以我一定是做错了什么。如何在开发人员每次调用getText()时附加字符串?我在标准的textinputtext
中遇到了这个问题。显式添加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”
也适用于我