【问题标题】:EditText loses background on selectionEditText 在选择时失去背景
【发布时间】:2023-03-19 16:29:01
【问题描述】:

我有一个具有自定义 drawable 背景的 EditText 框,但是每当您选择 EditText 输入内容时,背景就会消失并且无法阅读文本(它与应用背景颜色相同)

这就是我要说的:

背景消失了,我不知道为什么会这样。这是我的.xml

    <EditText
    android:id="@+id/enterCode"
    android:layout_width="237sp"
    android:layout_height="55sp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="6dp"
    android:layout_marginBottom="8dp"
    android:autofillHints="true"
    android:background="@drawable/rect_yellow_border"
    android:ems="10"
    android:hint="Enter Game PIN"
    android:inputType="text"
    android:textAlignment="center"
    android:textColor="@color/colorPrimary"
    android:textColorHint="#40292929"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.503"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/products_toolbar"
    app:layout_constraintVertical_bias="0.309" />

我也尝试在Java 中设置背景,但是当我尝试这样做时问题也没有解决。有什么帮助吗?

这是我的drawable 文件。这有点不必要和奇怪,因为我刚刚编辑了我的一个圆形的代码并将它变成了一个正方形

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" >
            <shape android:shape="rectangle"  >
            <stroke android:width="3dip" android:color="#ffc847" />
            <gradient android:angle="-180" android:startColor="#ffffff" android:endColor="#ffffff"  />
        </shape>
    </item>
    <item android:state_focused="true">
        <shape android:shape="rectangle"  >
        </shape>
    </item>
    <item >
        <shape android:shape="rectangle"  >
            <stroke android:width="3dip" android:color="#ffc847" />
            <stroke android:width="3dip" android:color="#ffc847" />
            <solid android:color="#ffc847"/>
            <gradient android:angle="-45" android:startColor="#ffffff" android:endColor="#ffffff" />
        </shape>
    </item>


</selector>

在我的Activity 中,我为定义这个EditText 所做的一切是这样的:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_private_room_lobby);
    final Button createPrivateGame = findViewById(R.id.createPrivateGame);
    final Button joinPrivateRoom = findViewById(R.id.joinPrivateGame);
    final EditText pinEntry = findViewById(R.id.enterCode);

    joinPrivateRoom.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String pin = pinEntry.getText().toString().trim();
            if(TextUtils.isEmpty(pin) || pin.length() > 6 || pin.length() < 6)
            {
                Toast.makeText(PrivateRoomLobby.this, "Please enter a 6 digit PIN to enter the room", Toast.LENGTH_LONG).show();
            } else {
                joinGame(pin);
            }
        }
    });

【问题讨论】:

  • 分享 rect_yellow_border 可绘制代码和您的活动代码
  • @AD10 更新了帖子

标签: xml android-edittext android-drawable


【解决方案1】:

选择器用于根据view组件的各种状态定义其背景颜色或背景图像。更多详情请查看this

你需要使用唯一的shape

    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >



    <stroke android:width="3dip" android:color="#ffc847" />

    <solid android:color="#ffffff"/>
  <!--  <gradient android:angle="-45" android:startColor="#ffffff" android:endColor="#ffffff" />-->

</shape>

【讨论】:

    猜你喜欢
    • 2014-03-03
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多