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了,效果图如下:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值