Android 中 EditText 的基本使用

Android 中 EditText 的基本使用

1. 常见属性

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:padding="15dp"
    tools:context=".EditTextActivity">

    <EditText
        android:id="@+id/et_1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginBottom="15dp"
        android:autofillHints=""
        android:hint="@string/username"
        android:inputType="text"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:textColor="@color/red"
        android:textSize="16sp" />

</RelativeLayout>

2. 自定义样式

在这里插入图片描述

照片放在指定路径下, 可以按照原有的招嫖改动比例大小.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:padding="15dp"
    tools:context=".EditTextActivity">

    <EditText
        android:id="@+id/et_1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginBottom="15dp"
        android:autofillHints=""
        android:background="@drawable/bg_username"
        android:drawableStart="@mipmap/username"
        android:drawablePadding="10dp"
        android:hint="@string/username"
        android:inputType="text"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:textColor="@color/red"
        android:textSize="16sp" />

    <EditText
        android:id="@+id/et_2"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@id/et_1"
        android:layout_marginBottom="15dp"
        android:autofillHints=""
        android:background="@drawable/bg_username"
        android:drawableStart="@mipmap/password"
        android:drawablePadding="10dp"
        android:hint="@string/password"
        android:inputType="textPassword"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:textColor="@color/red"
        android:textSize="16sp" />

    <EditText
        android:id="@+id/et_3"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@id/et_2"
        android:layout_marginBottom="15dp"
        android:autofillHints=""
        android:background="@drawable/bg_username"
        android:drawableStart="@mipmap/code"
        android:drawablePadding="10dp"
        android:hint="@string/code"
        android:inputType="number"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:textColor="@color/red"
        android:textSize="16sp" />

</RelativeLayout>

样式:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <stroke
        android:width="1dp"
        android:color="@color/white_up" />

    <corners android:radius="5dp" />

</shape>

3. 监听事件

在这里插入图片描述

    <Button
        android:id="@+id/et_btn_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/et_3"
        android:text="@string/login"
        android:textColor="@color/white"
        android:textSize="23sp"
        android:background="@drawable/bg_btn4"/>

在这里插入图片描述

EditTextActivity 内容.

package com.example.hello;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EditTextActivity extends AppCompatActivity {
    
    

    // 声明
    private Button etBtn1;
    private EditText et_username;
    private EditText et_password;
    private EditText et_code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_text);
        // 得到按钮
        etBtn1 = findViewById(R.id.et_btn_1);
        // 点击事件
        etBtn1.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View v) {
    
    
                Toast.makeText(EditTextActivity.this, "登录成功!", Toast.LENGTH_SHORT).show();
            }
        });

        // 得到 username
        et_username = findViewById(R.id.et_1);
        // 添加内容的监听事件
        et_username.addTextChangedListener(new TextWatcher() {
    
    
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
    

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
    
    
                Log.d("username: ", s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {
    
    

            }
        });

        // 得到 password
        et_password = findViewById(R.id.et_2);
        // 添加内容的监听事件
        et_password.addTextChangedListener(new TextWatcher() {
    
    
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
    

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
    
    
                Log.d("password: ", s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {
    
    

            }
        });

        // 得到 code
        et_code = findViewById(R.id.et_3);
        // 添加内容的监听事件
        et_code.addTextChangedListener(new TextWatcher() {
    
    
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
    

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
    
    
                Log.d("code: ", s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {
    
    

            }
        });
    }

}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/YKenan/article/details/112650869
今日推荐