Android EditText自定义样式的方法

 更新时间:2016年02月20日 11:18:33   作者:一叶飘舟  
这篇文章主要介绍了Android EditText自定义样式的方法,结合实例形式分析了EditText属性的含义及样式定义的技巧,需要的朋友可以参考下

本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android通信方式总结》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结

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

相关文章

  • Flutter自定义Appbar搜索框效果

    Flutter自定义Appbar搜索框效果

    这篇文章主要为大家详细介绍了Flutter自定义Appbar搜索框,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-06-06
  • Android ListView详解

    Android ListView详解

    listview控件在项目开发过程中经常会用到,本文给大家分享android listview相关知识,感兴趣的朋友一起学习吧
    2015-12-12
  • Android NotificationListenerService 通知服务原理解析

    Android NotificationListenerService 通知服务原理解析

    这篇文章主要为大家介绍了Android NotificationListenerService 通知服务原理解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • Android编程中的四大基本组件与生命周期详解

    Android编程中的四大基本组件与生命周期详解

    这篇文章主要介绍了Android编程中的四大基本组件与生命周期,结合实例形式较为详细的分析了Android四大组件及生命周期的相关概念与使用技巧,需要的朋友可以参考下
    2015-12-12
  • Android 文件存储与SharedPreferences存储方式详解用法

    Android 文件存储与SharedPreferences存储方式详解用法

    SharedPreferences是安卓平台上一个轻量级的存储类,用来保存应用的一些常用配置,比如Activity状态,Activity暂停时,将此activity的状态保存到SharedPereferences中;当Activity重载,系统回调方法onSaveInstanceState时,再从SharedPreferences中将值取出
    2021-10-10
  • Android 自定义AlertDialog对话框样式

    Android 自定义AlertDialog对话框样式

    实际的项目开发当中,经常需要根据实际的需求来自定义AlertDialog。最近在开发一个WIFI连接的功能,点击WIFI需要弹出自定义密码输入框,具体代码大家参考下本文
    2017-09-09
  • Kotlin StateFlow单数据更新热流设计与使用介绍

    Kotlin StateFlow单数据更新热流设计与使用介绍

    StateFlow当值发生变化,就会将值发送出去,下流就可以接收到新值。在某些场景下,StateFlow比LiveData更适用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2022-09-09
  • Android 多图上传后将图片进行九宫格展示的实例代码

    Android 多图上传后将图片进行九宫格展示的实例代码

    这篇文章主要介绍了Android 多图上传后将图片进行九宫格展示,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-11-11
  • Android动画之小球拟合动画实例

    Android动画之小球拟合动画实例

    这篇文章主要介绍了Android动画之小球拟合动画实例的相关资料,需要的朋友可以参考下
    2017-07-07
  • 详解ASP.NET Core MVC四种枚举绑定方式

    详解ASP.NET Core MVC四种枚举绑定方式

    这篇文章主要介绍了详解ASP.NET Core MVC四种枚举绑定方式, 小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04

最新评论