无法更改焦点上“编辑文本”的边框颜色

时间:2017-08-31 09:59:44

标签: android android-xml

我正在尝试将我的EditText边框颜色设置为蓝色。我在Stack Overflow的许多答案中都尝试了所有可能的方法,但它不起作用。聚焦时只改变光标的颜色而不改变整个边框的颜色。以下是我试过的,如果我做错了,请告诉我

enter image description here

灰色边框为蓝色

styles.xml

<!--Edit Text-->
    <style name="EditTextTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorControlNormal">#FFFFFF</item>
        <item name="colorControlActivated">#FF1792E5</item>
        <item name="colorControlHighlight">#FF1792E5</item>
    </style>

edit_text_border.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#ffffff"/>
            <stroke
                android:width="1dp"
                android:color="#FF1792E5"
                />
        </shape>
    </item>
</selector>

XML

<EditText
                android:id="@+id/edtName"
                android:background="@drawable/edit_text_border"
                android:inputType="text|textCapWords"
                android:maxLength="40"
                android:textColor="@color/black"
                android:singleLine="true" />

清单:

<activity
            android:name=".ui.activity.MyActivity"
            android:theme="@style/EditTextTheme"
            android:screenOrientation="portrait" />

5 个答案:

答案 0 :(得分:0)

尝试在 res / drawable 文件夹中创建 edittext_focused_blue ,就像这样

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

   <solid android:color="#ffffff" />    
   <stroke android:width="1dp" android:color="#ff00" />
   <corners android:radius="5dp" />

 </shape>

这会在res / drawable文件夹中创建一个edittext_normal,如下所示

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


 </shape>

尝试在 res / drawable 文件夹中创建 edittext_bg ,如下所示

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

    <item 
        android:state_focused="true"
        android:drawable="@drawable/edittext_focused_blue" 
        /> <!-- focused -->    
    <item  android:state_focused="false"
        android:drawable="@android:drawable/edittext_normal" 
        /> <!-- default -->
      </selector>

使用 android:background =&#34; @ drawable / edittext_bg&#34; 这样适用于您的editext

 <EditText
     android:id="@+id/edtName"
     android:background="@drawable/edittext_bg"
     android:inputType="text|textCapWords"
     android:maxLength="40"
     android:textColor="@color/black"
     android:singleLine="true" />

答案 1 :(得分:0)

    <?xml version="1.0" encoding="utf-8"?>
<selector 
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <item 
        android:state_pressed="true"
        android:drawable="@android:drawable/edittext_pressed" 
        /> <!-- pressed -->    
    <item 
        android:state_focused="true"
        android:drawable="@drawable/edittext_focused_blue" 
        /> <!-- focused -->    
    <item 
        android:drawable="@android:drawable/edittext_normal" 
        /> <!-- default -->
</selector>

试试这个

答案 2 :(得分:0)

尝试在res / drawable中创建一个文件夹:

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

    <stroke android:color="@color/block" android:width="5dp"/>
<solid android:color="#ffffff"/>

</shape>

<强> XML:

android:background="@drawable/yourborder"

这是输出。

enter image description here

另一种方式:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_window_focused="false" android:state_enabled="true"
        android:drawable="@drawable/twitter_im_edittext_normal" />
  <item android:state_window_focused="false" android:state_enabled="false"
        android:drawable="@drawable/twitter_im_edittext_normal" />
  <item android:state_pressed="true" android:drawable="@drawable/twitter_im_edittext_normal" />
  <item android:state_enabled="true" android:state_focused="true" android:drawable="@drawable/twitter_im_edittext_focused" />
  <item android:state_enabled="true" android:drawable="@drawable/twitter_im_edittext_normal" />
  <item android:state_focused="true" android:drawable="@drawable/twitter_im_edittext_focused" />
  <item android:drawable="@drawable/twitter_im_edittext_normal" />
</selector>

我跳这可能对你有所帮助。

答案 3 :(得分:0)

我已经测试了以下代码并且它运行良好。

<EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@drawable/ediittext_selector"
      android:hint="Demo"
      />

ediittext_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true" android:drawable="@drawable/edit_text_border_selected" />
  <item android:state_focused="true" android:drawable="@drawable/edit_text_border_selected" />
  <item android:drawable="@drawable/edit_text_border" />
</selector>

edit_text_border_selected.xml

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

  <solid android:color="#737373" />
  <stroke
      android:color="#FF4081"
      android:width="1dp" />

  <padding
      android:bottom="10dp"
      android:left="10dp"
      android:right="10dp"
      android:top="10dp" />
</shape>

edit_text_border.xml

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

  <solid android:color="#737373" />
  <stroke
      android:color="#F78C1F"
      android:width="1dp" />

  <padding
      android:bottom="10dp"
      android:left="10dp"
      android:right="10dp"
      android:top="10dp" />
</shape>

答案 4 :(得分:0)

请尝试以下代码来实现您的设计:

  

可绘制代码:

et_border1.xml可绘制,用于未选中或未集中的编辑文本。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item >
        <shape android:shape="rectangle">
            <solid android:color="#ffffff"/>
            <stroke
                android:width="1dp"
                android:color="#ffffff" />
        </shape>
    </item>
</selector> 

et_border2.xml对于选定或重点突出的编辑文本可绘制。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item >
        <shape android:shape="rectangle">
            <solid android:color="#ffffff" />
            <stroke
                android:width="1dp"
                android:color="#FF1792E5" />
        </shape>
    </item>
</selector>

edit_text_border可绘制以显示选定或未选定的编辑文本

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/et_border2" />
    <item android:state_focused="true" android:drawable="@drawable/et_border2" />
    <item android:drawable="@drawable/et_border1" />
</selector>
  

在“布局文件”中

 <EditText
        android:id="@+id/edtName"
        android:inputType="text|textCapWords"
        android:maxLength="40"
        android:textColor="@android:color/black"
        android:singleLine="true"
        android:layout_height="40dp"
        android:background="@drawable/edit_text_border"
        android:layout_width="match_parent"/>

根据要求添加或删除颜色

我希望它对您有用。