EditText

今天学习了EditText,小小的制作了一个登陆界面。

一、EditText的一些常用属性

第一部分

这里是界面的代码粘贴出来,里面有注释来加以说明

**<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp">

    <EditText
        android:id="@+id/et_1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:textSize="16sp"
        android:textColor="#FFAD33"
        android:hint=" 用户名"//hint的效果就是用户名的对话框还没输入时,会显示“用户名”来提醒用户这里填的是什么
        android:background="@drawable/bg_btn5"//由于单纯只使用EditText,只有下划线,所以在drawable文件下写了一个图形,来充当EditText的背景
        android:drawableLeft="@drawable/icon_user"//这是在hint提示语的左侧会有一个小图标,也就是用户名的图标
        android:paddingLeft="5dp"
        android:maxLines="1"//由于用户名正常来说只能是一行,所以要限制最大行数
        android:layout_marginTop="50dp"/>
    <EditText
        android:id="@+id/et_2"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:textSize="16sp"
        android:maxLines="1"
        android:textColor="#FFAD33"
        android:hint=" 密码"
        android:layout_below="@id/et_1"
        android:layout_marginTop="15dp"
        android:inputType="textPassword"//这个作用是当我们在输入密码是,显示的是……而不是显示出自己输入了什么,如果已经确定了密码是数字的话,你可以换成numberpassword
        android:background="@drawable/bg_btn5"
        android:drawableLeft="@drawable/icon_password"
        android:paddingLeft="5dp"/>
    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/et_2"
        android:layout_marginTop="40dp"
        android:text="登录"
        android:textSize="26sp"
        android:background="@drawable/bg_btn4"/>//这里我设置的是当你点击这个按钮时会出现按钮颜色的变化
</RelativeLayout>**

效果图如下:
在这里插入图片描述

第二部分

//@drawable/bg_btn4,这是在登陆按钮下的background,颜色大家随意设计,我的不太有美感
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">//pressed = true 字面上的意思,当点击按钮的时候
        <shape>
            <solid android:color="#FF99FF"/>
            <corners android:radius="5dp"/>
        </shape>
    </item>
    <item android:state_pressed="false">
        <shape>
            <solid android:color="#FF9900"/>
            <corners android:radius="5dp"/>
        </shape>
    </item>
</selector>

呃,这个效果图我就不上了,想象一下,就点击的时候由黄色变成了紫色。

//@drawable/bg_btn5  这是用户名,密码的框框
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid//这个相当于框框的底色,所以设成白色
        android:width="1dp"
        android:color="#FFFFFF" />
    <corners//这个是能把长方形的四个角,设置成圆角,更有美感,通常是5dp就可以了
        android:radius="2dp"/>
    <stroke//这个是框框的外边界,我设置成黑子,宽度为1dp就够了,太宽可太难看了
        android:width="1dp"
        android:color="#000000"/>
</shape>

当然了,这里除了solid,corners,stroke还有其他属性,我也在逐渐摸索,这几个是常用的

第三部分

监听事件,这个就要在java文件上弄了

public class EdittextActivity extends AppCompatActivity {
    private Button mBtnButton;//要使用xml里的东西,一定要先声明
    private EditText mEtUserName;
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edittext);
        mBtnButton = findViewById(R.id.btn_login);
        mBtnButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Toast.makeText(EdittextActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
            }//Toast就是一个小的点击触发
        });
        mEtUserName = findViewById(R.id.et_1);
        //以下就是监听事件
        mEtUserName.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                Log.d("edittext",charSequence.toString());
                //在输入中进行监听,并把监听内容输出
            }
            public void afterTextChanged(Editable editable) {
            }
        });
    }
}

Toast效果就是模拟器下方的登陆成功,监听就是左下角的监听用户输入的helloworld
在这里插入图片描述
PS:这两个图标都是在我上一篇文章提到的那个icon网址上download的,然后去画图软件上,设置像素大小为100X100,就能像我上面的图一样,完美嵌入

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值