当前位置: 首页 > 编程笔记 >

Android EditText自定义样式的方法

潘驰
2023-03-14
本文向大家介绍Android EditText自定义样式的方法,包括了Android 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程序设计有所帮助。

 类似资料:
  • 本文向大家介绍thinkPHP3.2实现分页自定义样式的方法,包括了thinkPHP3.2实现分页自定义样式的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了thinkPHP3.2实现分页自定义样式的方法。分享给大家供大家参考,具体如下: 下面是一个Tp3.2的自定义分页,这个方法也是在看过一个网友的博客之后受到启发这么写的。经过了一些修改,大家在看到代码之后也可以进行修改自定义样式;

  • 本文向大家介绍Android编程自定义AlertDialog样式的方法详解,包括了Android编程自定义AlertDialog样式的方法详解的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Android编程自定义AlertDialog样式的方法。分享给大家供大家参考,具体如下: 开发的时候,通常我们要自定义AlertDialog来满足我们的功能需求: 比如弹出对话框中可以输入信息,或者要

  • 本文向大家介绍android自定义弹出框样式的实现方法,包括了android自定义弹出框样式的实现方法的使用技巧和注意事项,需要的朋友参考一下 前言: 做项目时,感觉android自带的弹出框样式比较丑,很多应用都是自己做的弹出框,这里也试着自己做了一个。 废话不说先上图片: 实现机制 1.先自定义一个弹出框的样式 2.自己实现CustomDialog类,继承自Dialog,实现里面方法,在里面加

  • 本文向大家介绍jquery自定义表格样式,包括了jquery自定义表格样式的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了jquery自定义表格样式实现代码。分享给大家供大家参考。具体如下: 运行效果截图如下: 上面这张图有3种状态,默认状态(灰白相间),鼠标悬浮状态(绿色),鼠标点击状态(黄色),是如何实现的呐? Html代码如下: 插件实现代码如下: 有些时候我们可能并不需要鼠标点击后

  • 出于性能考虑,HTML 中不允许使用内联 style,所有样式只能放到 <head> 的 <style mip-custom> 标签里。 正确方式: <head> <style mip-custom> p { color: #00f;} </style> </head> <body> <p>Hello World!</p> </body> 错误方式: <!-- 禁止使用 sty

  • 本文向大家介绍如何自定义radio按钮的样式相关面试题,主要包含被问及如何自定义radio按钮的样式时的应答技巧和注意事项,需要的朋友参考一下 选择器 input[type=“radio”] 现在几乎不用原生的radio,一是原生样式改成设计稿的样子太浪费时间,二是不同浏览器对于原生radio的展示还不一样。 基于状态驱动的思想,用自定义按钮或其他元素来替代radio,很容易实现,也能保证浏览器兼