EditText属性:
xml代码:
<?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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码" />
<EditText
android:id="@+id/te"
android:layout_toRightOf="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请输入密码"/>
</RelativeLayout>
监听文本输入代码:
EditText te = findViewById(R.id.te);
te.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.d("-->test","content before change "+s+" start"+start+" count"+count+" after"+after);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d("-->test","content changed "+s+" start:"+start+" before:"+before+" count:"+count);
}
@Override
public void afterTextChanged(Editable s) {
Log.d("-->test","content after changed "+s);
}
});
收入12时的log
2020-04-28 22:52:38.423 17464-17464/com.gentle.edittext D/-->test: content before change start0 count0 after1
2020-04-28 22:52:38.425 17464-17464/com.gentle.edittext D/-->test: content changed 1 start:0 before:0 count:1
2020-04-28 22:52:38.428 17464-17464/com.gentle.edittext D/-->test: content after changed 1
2020-04-28 22:52:40.091 17464-17464/com.gentle.edittext D/-->test: content before change 1 start1 count0 after1
2020-04-28 22:52:40.093 17464-17464/com.gentle.edittext D/-->test: content changed 12 start:1 before:0 count:1
2020-04-28 22:52:40.097 17464-17464/com.gentle.edittext D/-->test: content after changed 12