Android EditText设置边框

Android EditText设置边框

简介

Android应用程序中给EditText设置边框。

效果图:

在这里插入图片描述

快速开始

  1. 在res/drawable目录下新建样式文件 edit_background.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape
            android:shape="rectangle">
            <solid android:color="#efefef"/>
            <corners android:radius="5dp"/>
            <stroke
                android:width="1dp"
                android:color="#505050"/>
        </shape>
    </item>
  1. 布局文件中使用边框效果,/res/layout/activity_edit_text.xml。

android:background=“@drawable/edit_background”

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".EditTextActivity">

    <TextView
        android:id="@+id/name_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="账号:"
        android:textSize="18sp"
        android:textColor="#353535"
        android:layout_marginTop="60dp"
        android:layout_marginStart="60dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"/>

    <EditText
        android:id="@+id/edit_name"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:hint="请输入账号"
        android:gravity="center"
        android:inputType="number"
        android:background="@drawable/edit_background"
        android:layout_marginStart="10dp"
        app:layout_constraintTop_toTopOf="@id/name_label"
        app:layout_constraintBottom_toBottomOf="@id/name_label"
        app:layout_constraintLeft_toRightOf="@id/name_label"/>

    <TextView
        android:id="@+id/pwd_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="密码:"
        android:textSize="18sp"
        android:textColor="#353535"
        android:layout_marginTop="40dp"
        android:layout_marginStart="60dp"
        app:layout_constraintTop_toBottomOf="@id/name_label"
        app:layout_constraintLeft_toLeftOf="parent"/>

    <EditText
        android:id="@+id/edit_pwd"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:hint="请输入密码"
        android:gravity="center"
        android:inputType="textPassword"
        android:background="@drawable/edit_background"
        android:layout_marginStart="10dp"
        app:layout_constraintTop_toTopOf="@id/pwd_label"
        app:layout_constraintBottom_toBottomOf="@id/pwd_label"
        app:layout_constraintLeft_toRightOf="@id/pwd_label"/>

    <Button
        android:id="@+id/btn_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ok"
        android:layout_marginTop="30dp"
        android:layout_marginStart="110dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/pwd_label"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cancel"
        android:layout_marginStart="50dp"
        app:layout_constraintTop_toTopOf="@id/btn_login"
        app:layout_constraintLeft_toRightOf="@id/btn_login"/>

</android.support.constraint.ConstraintLayout>
  • 31
    点赞
  • 89
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值