常用属性
<EditText
android:id="@+id/et_1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="16sp"
android:textColor="#ffad33"
android:hint="用户名"
android:background="@drawable/bg_username"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:drawablePadding="5dp"
android:maxLines="1"
android:drawableLeft="@drawable/username1"
android:layout_marginTop="50dp"/>
其中:
hint=“密码” 文本框中的提示信息
inputType=“” 输入类型
其常用属性值有:textPassword(密码/密文)、number(数字)、textUri(网址)、textEmailAddress(电子邮件地址)、textPostalAddress(地址)等
drawableLeft = “@drawable/icon_username” 提示信息左侧的图标
drawablePadding = “5dp” 图表与文字之间的距离
maxLines = “1” 一行显示
监听事件
//声明控件
private EditText mEtUserName;
//找到控件
mEtUserName = (EditText) findViewById(R.id_et_1);
//设置监听事件
mEtUserName.addTextChangedListener(new TextWatcherl){
//在文字改变前
@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("edittext", s.toString()); //实时打印文本框中输入的内容
}
//在文字改变后
@Override
public void afterTextChanged(Editable s){
}
}
注:
CharSequence 为当前文本框中输入的内容。
制作登录界面
话不多说,直接上代码吧!
activity_edit_text.xml文件
<?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="用户名"
android:background="@drawable/bg_username"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:drawablePadding="5dp"
android:maxLines="1"
android:drawableLeft="@drawable/username1"
android:layout_marginTop="50dp"/>
<EditText
android:id="@+id/et_2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="16sp"
android:textColor="#ffac33"
android:inputType="textPassword"
android:hint="密码"
android:layout_below="@+id/et_1"
android:layout_marginTop="15dp"
android:background="@drawable/bg_username"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:drawablePadding="5dp"
android:maxLines="1"
android:drawableLeft="@drawable/password1"/>
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@+id/et_2"
android:layout_marginTop="40dp"
android:background="@drawable/selector_orange"
android:text="登录"
android:textColor="#ffffff"
android:textSize="20sp"/>
</RelativeLayout>
package com.example.helloworld1;
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 mBtnLogin;
private EditText mEtUserName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_text);
//找到控件
mBtnLogin = (Button)findViewById(R.id.btn_login);
//设置点击事件
mBtnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(EditTextActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
}
});
//找到控件
mEtUserName = (EditText) findViewById(R.id.et_1);
mEtUserName.addTextChangedListener(new TextWatcher() {
/*
* 在文字改变之前
*
* */
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
/*
* 在文字改变之中
*CharSequence 当前输入框中的内容
* */
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d("edittext",s.toString());
}
/*
* 在文字改变之后
*
* */
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
效果图如下: