MainActivity:
package com.kanxue.school_test_3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText ev_phone;
EditText ev_password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ev_phone=findViewById(R.id.tv_phone);
ev_password=findViewById(R.id.tv_password);
ev_phone.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Log.i("MainActivity","信息输入完成");
return false;
}
});
ev_password.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.i("MainActivity","键盘触发前文字"+s.toString());
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.i("MainActivity","键盘当前输入文字"+s.toString());
}
@Override
public void afterTextChanged(Editable s) {
Log.i("MainActivity","键盘输入完成");
}
});
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/tv_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@null"
android:inputType="text"
android:maxLength="11"
android:hint="请输入手机号"
android:drawablePadding="10dp"
android:padding="10dp"
android:drawableLeft="@mipmap/ic_launcher"
android:drawableBottom="@drawable/shape_et_bottom_line"
android:layout_marginTop="20dp"></EditText>
<EditText
android:id="@+id/tv_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@null"
android:inputType="textPassword"
android:maxLength="16"
android:hint="请输入密码"
android:drawablePadding="10dp"
android:padding="10dp"
android:drawableLeft="@mipmap/ic_launcher"
android:drawableBottom="@drawable/shape_et_bottom_line"
android:layout_marginTop="20dp"></EditText>
<EditText
android:id="@+id/tv_login"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="30dp"
android:text="登录"
android:textColor="#ffffffff"></EditText>
</LinearLayout>
output:
真机显示
注意,对于监听EditText有两种方式,即两个函数