Android登录框设置可编辑文本:替换android:hint为android:text无显示
解决EditText替换hint为text后文本不显示的问题
你的EditText只设置了android:textColorHint="@color/white",但没指定android:textColor属性,导致输入的文本颜色和背景(或父布局背景)一致,所以肉眼看不见。另外要确保android:text设置的内容不是空值或空白字符串。
解决步骤:
- 给EditText添加
android:textColor属性,选一个和背景对比明显的颜色(比如@color/black,根据你的UI需求调整) - 确保
android:text属性设置了有效内容(直接写字符串或者引用字符串资源)
修改后的代码示例:
<!-- 用户名输入框 --> <EditText android:textColor="@color/black" <!-- 添加文本颜色设置 --> android:textColorHint="@color/white" android:id="@id/tv_vpn_username" android:background="@drawable/selector_verify_password_fields" android:focusable="true" android:nextFocusDown="@id/tv_vpn_password" android:layout_width="fill_parent" android:layout_height="40.0dip" android:layout_marginBottom="10.0dip" android:text="默认用户名" <!-- 替换hint为具体文本或字符串资源 --> android:layout_below="@id/et_description" android:fontFamily="@string/font_family_regular" android:textDirection="locale" android:paddingStart="10.0dip" android:layout_marginStart="30.0dip" android:layout_marginEnd="30.0dip" /> <!-- 密码输入框 --> <EditText android:textColor="@color/black" <!-- 添加文本颜色设置 --> android:textColorHint="@color/white" android:id="@id/tv_vpn_password" android:background="@drawable/selector_verify_password_fields" android:focusable="true" android:nextFocusUp="@id/tv_vpn_username" android:nextFocusDown="@id/bt_save_and_connect" android:layout_width="fill_parent" android:layout_height="40.0dip" android:layout_marginTop="10.0dip" android:layout_marginBottom="10.0dip" android:text="默认密码" <!-- 替换hint为具体文本或字符串资源 --> android:layout_below="@id/tv_vpn_username" android:fontFamily="@string/font_family_regular" android:textDirection="locale" android:paddingStart="10.0dip" android:layout_marginStart="30.0dip" android:layout_marginEnd="30.0dip" android:inputType="textPassword" /> <!-- 密码框建议添加密码输入类型 -->
额外排查点:
- 检查
@drawable/selector_verify_password_fields的背景颜色,确保文本颜色和背景有足够对比度 - 如果需要动态设置文本,在代码中调用
tv_vpn_username.setText("你的内容")即可 - 确认EditText没有被其他布局元素遮挡(从当前布局来看,这个概率很低)
内容的提问来源于stack exchange,提问作者DanW
相关产品推荐
相关产品推荐

