您的位置:首页 > 移动开发 > Android开发

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>
然后就可实现上述效果了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: