Android常用控件之TextView、EditText、Shape外形资源、Selector选择器

以上就是四种常用控件

接下来我用一个案例来具体讲一下怎么用:

这是案例截图,当文本输入框获得焦点时整个背景包括文本输入框变蓝色,失去焦点时为绿色

代码如下:

这里我用的是桢布局,先把文本框和背景铺在下面,然后再铺上文字

 

<?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>

然后就可实现上述效果了
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值