Android开发笔记04-EditText

一、EditText主要属性

1. android:hint输入提示
2. android:textColorHint输入提示文字的颜色
3. android:inputType输入类型,限定你输入的数据的类型,一般不做限定
4. android:drawableXxxx在输入框的指定方位添加图片
5. android:drawablePadding设置图片与输入内容的间距
6. android:paddingXxxx设置内容与边框的间距
        android:padding="xxdp",EditText中的文本与控件内部四周保持的边距
        android:paddingLeft="xxdp",内容与控件内部的左侧边距
        android:paddingRight="xxdp",内容与控件内部的右侧边距
        android:paddingTop="xxdp",内容与控件内部的顶部边距
        android:paddingButtom="xxdp",内容与控件内部的底部边距
7. android:background背景色

 在activity_main.xml中:app:layout_constraintLeft_toLeftOf="parent"
 app:layout_constraintTop_toTopOf="parent" 
这两句话是让你的控件基于左上角的位置开始布局,如果不加xml会飘红
(Not Horizontally Constrained)

android:layout_marginLeft="104dp"
android:layout_marginTop="264dp"
这两句话是,控件距离左侧和顶端的边距是多少dp,决定了控件在画面中的位置。

    <EditText
        android:id="@+id/editText"

        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_marginLeft="104dp"
        android:layout_marginTop="264dp"
        android:hint="请输入用户名(手机号)"
        android:inputType="phone"
        android:textColor="#95a1aa"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
android:inputType="numberPassword"//只能输入0-9作为密码
android:inputType="textPassword"//支持文本作为密码,而且是不可见的。

二、输入用户名和密码控件的编写

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <EditText
        android:id="@+id/editText"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_marginLeft="104dp"
        android:layout_marginTop="264dp"
        android:hint="请输入用户名(手机号)"
        android:inputType="phone"
        android:textColor="#95a1aa"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <EditText
        android:id="@+id/editText2"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_marginLeft="104dp"
        android:layout_marginTop="372dp"
        android:hint="请输入密码"
        android:inputType="textPassword"
        android:textColor="#95a1aa"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

三、插入logo

选择一个用户样式的logo 

四、效果图 

 

五、MainActivity.java的编写

发现edt飘红

 

六、最终代码:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.334" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_marginLeft="104dp"
        android:layout_marginTop="264dp"
        android:hint="请输入用户名(手机号)"
        android:drawableLeft="@drawable/ic_account_circle_black_24dp"
        android:drawablePadding="10dp"
        android:padding="2dp"
        android:inputType="phone"
        android:textColor="#95a1aa"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <EditText
        android:id="@+id/editText2"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_marginLeft="104dp"
        android:layout_marginTop="372dp"
        android:hint="请输入密码"
        android:inputType="textPassword"
        android:textColor="#95a1aa"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="160dp"
        android:layout_marginTop="460dp"
        android:background="#C9F531A8"
        android:text="获取用户名"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

 MainActivity.java

public class MainActivity extends AppCompatActivity {

    private EditText edt;
    private String TAG="张三";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn =findViewById(R.id.btn);
        edt=findViewById(R.id.editText);
        btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                String userName= edt.getText().toString();
                Log.e(TAG, "用户名为:"+userName);
            }
        });

    }
}

七、效果预览

 

 

 

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值