Android中EditText如何去除边框添加下划线

Android EditText去除边框并添加下划线的方法
本文分享了在Android开发中如何修改EditText,使其去除边框并显示下划线的实现代码。通过设置背景颜色和文本颜色,可以自定义编辑框的视觉效果。

废话不多说了,直接给大家贴代码了。

<span style="font-family: Arial, Helvetica, sans-serif;"><?xml version="1.0" encoding="utf-8"?> 
</span> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<!--注意名称 --> 
<com.marine.study.LineEditText 
android:id="@+id/myEdit" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
style="?android:attr/textViewStyle" 
android:background="@null" 
android:textColor="@null" 
/> 
</LinearLayout> 

其中background,可以设置成其他颜色等

textColor不一定要是null,可以设置字体颜色

加下划线

public class LineEditText extends EditText { 
// 画笔 用来画下划线 
private Paint paint; 
public LineEditText(Context context, AttributeSet attrs) { 
super(context, attrs); 
paint = new Paint(); 
paint.setStyle(Paint.Style.STROKE); 
paint.setColor(Color.RED); 
// 开启抗锯齿 较耗内存 
paint.setAntiAlias(true); 
} 
@Override 
protected void onDraw(Canvas canvas) { 
super.onDraw(canvas); 
// 得到总行数 
int lineCount = getLineCount(); 
// 得到每行的高度 
int lineHeight = getLineHeight(); 
// 根据行数循环画线 
for (int i = 0; i < lineCount; i++) { 
int lineY = (i + 1) * lineHeight; 
canvas.drawLine(0, lineY, this.getWidth(), lineY, paint); 
} 
} 
}

以上内容给大家介绍了Android中EditText如何去除边框添加下划线的相关内容,希望对大家有所帮助!

### 回答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、付费专栏及课程。

余额充值