Android EditText自定义样式的方法

作者:一叶飘舟 时间:2021-10-06 22:37:37 

本文实例讲述了Android EditText自定义样式的方法。分享给大家供大家参考,具体如下:

1.去掉边框

EditText的background属性设置为@null就搞定了:android:background="@null"
style属性倒是可加可不加

附原文:

@SlumberMachine, that's a great observation! But, it seems that there is more to making a TextView editable than just setting android:editable="true". It has to do with the "input method" - what ever that is - and that is where the real difference between TextView and EditText lies. TextView was designed with an EditText in mind, that's for sure. One would have to look at the EditText source code and probably EditText style to see what's really going on there. Documentation is simply not enough.

I have asked the same question back at android-developers group, and got a satisfactory answer. This is what you have to do:

XML:


<EditText android:id="@+id/title" android:layout_width="fill_parent"
  style="?android:attr/textViewStyle"
  android:background="@null" android:textColor="@null"/>

Instead of style="?android:attr/textViewStyle" you can also write style="@android:style/Widget.TextView", don't ask me why and what it means.

2.Android EditText 改变边框颜色

第一步:为了更好的比较,准备两个一模一样的EditText(当Activity启动时,焦点会在第一个EditText上,如果你不希望这样只需要写一个高度和宽带为0的EditText即可避免,这里就不这么做了),代码如下:


<EditText
 android:layout_width="fill_parent"
   android:layout_height="36dip"
   android:background="@drawable/bg_edittext"
   android:padding="5dip"
 android:layout_margin="36dip"
 android:textColorHint="#AAAAAA"
 android:textSize="15dip"
 android:singleLine="true"
 android:hint="请输入..."
/>

接下来建立三个xml文件,分别为输入框未获得焦点时的背景,输入框获得焦点时的背景,selector背景选择器(这里能获得输入框什么时候获得和失去焦点),代码如下:

bg_edittext_normal.xml(未获得焦点时)


<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
 <solid android:color="#FFFFFF" />
 <corners android:radius="3dip"/>
 <stroke
   android:width="1dip"
   android:color="#BDC7D8" />
</shape>

bg_edittext_focused.xml(获得焦点时)


<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
 <solid android:color="#FFFFFF" />
 <corners android:radius="3dip"/>
 <stroke
   android:width="1dip"
   android:color="#728ea3" />
</shape>

bg_edittext.xml(selector选择器,这方面资料网上很多)


<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_window_focused="false" android:drawable="@drawable/contact_edit_edittext_normal" />
   <item android:state_focused="true" android:drawable="@drawable/contact_edit_edittext_focused" />
</selector>

这样就OK了,效果图如下:

Android EditText自定义样式的方法

第二个输入框边框变为深色,是不是这样更友好点。

希望本文所述对大家Android程序设计有所帮助。

标签:Android,EditText
0
投稿

猜你喜欢

  • Mybatis MapperScannerConfigurer自动扫描Mapper接口生成代理注入到Spring的方法

    2023-04-17 11:57:25
  • Winform下实现图片切换特效的方法

    2023-04-20 21:26:28
  • C#配置文件操作类分享

    2022-09-28 11:12:05
  • java实现读取、删除文件夹下的文件

    2021-12-06 20:07:48
  • SpringBoot整合MongoDB的步骤详解

    2023-10-11 17:20:32
  • 深入探究Java线程与进程有哪些区别

    2023-05-06 13:26:03
  • Mybatis控制台打印SQL语句的两种方式实现

    2023-06-23 14:24:57
  • Android应用创建多个快捷方式

    2022-10-08 11:26:00
  • spring boot + mybatis实现动态切换数据源实例代码

    2021-11-14 03:37:59
  • Java struts2请求源码分析案例详解

    2021-08-06 02:03:24
  • C# 实现绘制PDF嵌套表格案例详解

    2023-05-25 11:57:13
  • Struts2源码分析之ParametersInterceptor拦截器

    2023-11-05 00:41:37
  • Mybatis实现自定义类型转换器TypeHandler的方法

    2023-09-28 14:52:22
  • C# params可变参数的使用注意详析

    2021-10-29 12:33:27
  • Java数据结构与算法入门实例详解

    2023-11-28 21:44:06
  • springboot编程式事务TransactionTemplate的使用说明

    2022-03-01 15:19:37
  • Java任意长度byte数组转换为int数组的方法

    2023-02-15 15:26:32
  • Android 破解视频App去除广告功能详解及解决办法总结

    2022-07-16 23:28:50
  • C#基于Socket套接字的网络通信封装

    2023-11-08 16:42:18
  • 解决SpringBoot中使用@Async注解失效的问题

    2023-08-24 07:38:46
  • asp之家 软件编程 m.aspxhome.com