Android常用控件之TextView、EditText、Shape外形资源、Selector选择器
2017-06-03 21:11
483 查看
以上就是四种常用控件
接下来我用一个案例来具体讲一下怎么用:
这是案例截图,当文本输入框获得焦点时整个背景包括文本输入框变蓝色,失去焦点时为绿色
代码如下:
这里我用的是桢布局,先把文本框和背景铺在下面,然后再铺上文字
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名" android:inputType="text" android:background="@drawable/et_selector"//引用Selector选择器 /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableLeft="@mipmap/icon_user"//在文字的左边引用图标 android:layout_marginLeft="15dp"//设置距离屏幕左端的距离 android:layout_marginTop="5dp"//设置距离屏幕上端的距离 android:text="用户名:" android:textSize="20sp"//设置文字的大小 /> </FrameLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp" > <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" android:inputType="text" android:background="@drawable/et_selector" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableLeft="@mipmap/icon_user" android:layout_marginLeft="15dp" android:layout_marginTop="5dp" android:text=" 密码:" android:textSize="20sp" /> </FrameLayout> </FrameLayout>再引用选择器之前,我们需要创建一个选择器
在drawable文件夹下创建et_selector文件,内容如下
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:drawable="@drawable/et_shape"></item>//得到焦点时引用et_shape外形资源 <item android:state_focused="false" android:drawable="@drawable/et_shape2"></item>//失去焦点时引用et_shape2外形资源 </selector>
同样的,引用外形资源时,我们先在drawable文件夹下创建et_shape和et_shape2资源文件
et_shape:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <!--设置背景颜色--> <solid android:color="#00e6ff" > </solid> <!--弧度--> <corners android:radius="30dp" ></corners> <!--内边距--> <padding android:top="5dp" android:bottom="5dp" android:left="110dp" android:right="5dp" ></padding> <!--边框--> <stroke android:width="2dp" android:color="#000000" > </stroke> </shape>et_shape2:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <solid android:color="#1eff00" > </solid> <!--弧度--> <corners android:radius="30dp" ></corners> <!--内边距--> <padding android:top="5dp" android:bottom="5dp" android:left="110dp" android:right="5dp" ></padding> <!--边框--> <stroke android:width="2dp" android:color="#000000" > </stroke> </shape>然后就可实现上述效果了
相关文章推荐
- Android 常用控件(TextView、EditText)及Shape外形资源、Selector选择器
- Android常用控件(TextView-EditText-Shape-Selector)
- Android04-常用控件(TextView-EditText-Shape-Selector)
- 常用控件(TextView-EditText-Shape-Selector)
- TextView 、EditText 、Shape外形资源 、Selector选择器
- Android第四讲——常用的基本控件(一)TextView、EditText、Button、RadioButton、CheckButton、ImageView
- android内部培训视频_第三节 常用控件(Button,TextView,EditText,AutocompleteTextView)
- Android-常用UI控件(Spinner/AutoCompleteTextView)
- Android常用控件-AutoCompleteTextView
- Android用户界面基础之TextView控件,EditText控件,Button控件的学习
- android EditText与TextView几个常用的属性
- Android用户界面 UI组件--TextView及其子类(二) Button,selector选择器,sharp属性
- Android基本常用控件的介绍TextView EditText Button Menu
- android常用控件应用之文本框(TextView)特效
- Android控件属性 (TextView 、EditText )
- Android控件——TextView、EditText、ImageView、Button、ImageButton
- Android常用控件-TextView、EditView、Button、Menu
- Android基础控件之TextView与EditText
- Android开发之四(八):常用控件之自动提示(AutoCompleteTextView)
- ImageButton、Button、TextView、EditText等一些常用控件的属性(持续更新)