Android 更改EditText的下划线的颜色

在styles中建一个style

<style name="MyEditText" parent="Theme.AppCompat.Light">
        <item name="colorControlNormal">@color/text_9</item>
        <item name="colorControlActivated">@color/text_9</item>
    </style>

color中的颜色

<color name="text_9">#999999</color>

在EditText中引用

<EditText
            android:id="@+id/et_latitude"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginRight="10dp"
            android:layout_weight="1"
            android:theme="@style/MyEditText"
            android:gravity="center"
            android:hint="维度"
            android:textSize="@dimen/font_size_13" />

这样就可以了 。。。

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要在Android Studio中为EditText添加下划线,可以使用以下方法: 1. 在布局文件中,将EditText的背景设置为下划线的Drawable资源文件。例如: ``` <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/edittext_underline" /> ``` 2. 创建一个Drawable资源文件,命名为edittext_underline.xml,内容如下: ``` <shape xmlns:android="http://schemas.android.com/apk/res/android"> <stroke android:width="1dp" android:color="@color/black" /> <solid android:color="@android:color/transparent" /> </shape> ``` 这个Drawable资源文件定义了一个带有1dp宽度和黑色颜色的边框,以及透明背景的形状。 3. 在代码中获取EditText实例,并设置其下划线颜色。例如: ``` EditText editText = findViewById(R.id.editText); editText.getBackground().setColorFilter(getResources().getColor(R.color.black), PorterDuff.Mode.SRC_IN); ``` 这个代码片段获取了EditText实例,并将其背景的颜色过滤器设置为黑色。这将使EditText下划线颜色与Drawable资源文件中定义的颜色相匹配。 希望这些方法能够帮助你在Android Studio中为EditText添加下划线。 ### 回答2: 在Android开发中,EditText是一种非常常见的控件,用于用户输入和编辑文本内容。如果我们想要在EditText中添加下划线,可以使用以下方法: 一、使用XML属性添加下划线 通过在布局文件中使用android:background属性来添加下划线,其中可以指定Drawable资源或颜色值。例如: ``` <EditText android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/edit_text_line" /> ``` 其中,@drawable/edit_text_line指定了一个Drawable资源文件,该文件定义了一条下划线。下面是一个示例: ``` <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line"> <stroke android:width="1dp" android:color="#000000" /> </shape> ``` 这个Drawable资源是一个简单的线条,可以使用shape元素和stroke元素来定义它的形状和样式。其中,android:width属性指定线条的宽度,android:color属性指定线条的颜色。在EditText的背景中使用这个Drawable资源,就可以实现下划线的效果。 二、使用SpannableString添加下划线 另一种方式是在代码中使用SpannableString对象来添加下划线。例如: ``` EditText editText = findViewById(R.id.edit_text); SpannableString content = new SpannableString("EditText with underlined text"); content.setSpan(new UnderlineSpan(), 10, 20, 0); editText.setText(content); ``` 这个代码段中,使用SpannableString对象来表示EditText中的文本内容。然后,使用content.setSpan()方法来在SpannableString中添加一个UnderlineSpan对象,该对象可以添加下划线效果。其中,10和20分别指定了下划线所在文本的起始和结束位置。最后,调用EditText的setText()方法将SpannableString对象设置为文本内容。 总之,在Android开发中,如果需要在EditText中添加下划线,可以使用以上两种方法之一。具体取决于你的需求和实现方案。 ### 回答3: Android Studio中的EditText下划线可以通过XML或Java代码进行更改。通常,EditText下划线是使用drawable实现的。我们可以使用以下步骤更改EditText下划线的样式。 步骤1:创建一个drawable XML文件 在res/drawable目录中创建一个新的XML文件,并对其进行命名。然后,添加以下代码: ``` <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line"> <stroke android:width="1dp" android:color="@color/colorAccent" /> </shape> ``` 这个代码块定义了一个具有1dp宽度和颜色colorAccent的线条。 步骤2:将drawable应用于EditText 打开XML布局文件,并在EditText中添加以下代码: ``` <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/your_drawable_name"/> ``` 这里,我们将drawable应用于EditText的background属性。 步骤3:Java代码中更改下划线 除了使用XML文件外,您还可以使用Java代码更改EditText下划线的样式。下面是一个示例代码块: ``` ShapeDrawable shape = new ShapeDrawable(new RectShape()); shape.getPaint().setColor(getColor(R.color.colorAccent)); shape.getPaint().setStrokeWidth(1f); EditText editText = findViewById(R.id.editText); editText.setBackground(shape); ``` 这个代码块定义了一个RectShape,并将其应用于EditText的background属性。此外,它还设置了线条的宽度和颜色。 总结:在Android Studio中更改EditText下划线的样式非常简单,并且可以使用XML或Java代码进行操作。通过更改drawable或ShapeDrawable来实现它,您可以根据自己的需要自定义下划线

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值