Monitor the content changes of EditText
Introduction to this section:
We have already learned about the EditText control before. In this section, we will talk about how to monitor the content changes of the input box! This is very practical in actual development. In addition, it also talks about how to realize the password visibility of EditText. And invisible! Okay, let’s start this section!
1. Monitor the content changes of EditText
As you can see from the title, it is an event processing mechanism based on monitoring. It seems that the previous click event is OnClickListener, and the text content The change listener is: TextWatcher, we can call EditText.addTextChangedListener(mTextWatcher); Set content change monitoring for EditText!
Let’s briefly talk about TextWatcher. To implement this class, you need to implement three methods:
public void onTextChanged(CharSequence s, int start, int before, int count);
public void afterTextChanged(Editable s);
will be triggered in the following situations:
- 1. Before the content changes
- 2. During the content change
- 3.After the content changes
We can rewrite related methods according to actual needs. Generally, the third method is rewritten more!
There are many occasions to monitor changes in EditText content: Limit the number of words entered, limit the input content, etc.~
Here is a simple custom EditText for everyone. After inputting the content, a crossed circle will be displayed on one side. After the user clicks You can clear the text box~, of course you can also add TextWatcher directly to EditText without customizing it and then set the delete button~
Achievement rendering:
Customized EditText: DelEditText.java
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.EditText;
/**
* Created by coder-pig on 2015/7/16 0016.
*/
public class DelEditText extends EditText {
private Drawable imgClear;
private Context mContext;
public DelEditText(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
init();
}
private void init() {
imgClear = mContext.getResources().getDrawable(R.drawable.delete_gray);
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) {
}
@Override
public void afterTextChanged(Editable editable) {
setDrawable();
}
});
}
//绘制删除图片
private void setDrawable(){
if (length() < 1)
setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
. When, the delete method is triggered, hiding the cross
@Override
public boolean onTouchEvent(MotionEvent event) {
if(imgClear != null && event.getAction() == MotionEvent.ACTION_UP)
{
int eventX = (int) event.getRawX();
int eventY = (int) event.getRawY();
Rect rect = new Rect();
getGlobalVis ibleRect(rect);
rect.left rect.right - 100;
t(event);
}
@Override
protected void finalize() throws Throwable {
super.finalize();
}
}
EditText’s background drawable:
bg_frame_search.xml
##<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:width="1px" android:color="@color/frame_search"/></shape>
颜色资源:color.xml
<resources>
<color name="reveal_color">#FFFFFF</color>
<color name="bottom_color">#3086E4</color>
<color name="bottom_bg">#40BAF8</color>
<color name="frame_search">#ADAEAD</color>
<color name="background_white">#FFFFFF</color>
<color name="back_red">#e75049</color>
</resources>
布局文件:activity_main.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="../style/images/back_red"
android:orientation="vertical"
tools:context=".MainActivity">
<demo.com.jay.buttondemo.DelEditText
android:id="@+id/edit_search"
android:layout_width="match_parent"
android:layout_height="32dp"
android:layout_margin="10dp"
android:background="../style/images/bg_frame_search"
android:hint="带删除按钮的EditText~"
android:maxLength="20"
android:padding="5dp"
android:singleLine="true" />
</LinearLayout>
PS: The code is very simple, so I won’t explain it~
2. Implementing the visible and invisible password of EditText
This is also a very practical requirement, that is After the user clicks the button, the password in the EditText can be made visible or invisible~
Implementation rendering:
Implementation code: activity_main.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:layout_margin="5dp"
android:orientation="horizontal">
<EditText
android:id="@+id/edit_pawd"
android:layout_width= "0dp"
android:layout_weight="2"
android:layout_height="48dp"
android:inputType="textPassword"
android:background="../style/images/editborder" /> :layout_height="48dp"
android:text="Password visible"/>
</LinearLayout>
MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText edit_pawd;
private Button btnChange;
private boolean flag = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit_pawd = (EditText) findViewById(R.id.edit_pawd);
btnChange = (Button) findViewById(R.id.btnChange);
edit_pawd.setHorizontallyScrolling(true); //设置EditText不换行
btnChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(flag == true){
edit_pawd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
flag = false;
btnChange.setText("密码不可见");
}else{
edit_pawd.setTransformationMethod(PasswordTransformationMethod.getInstance());
flag = true;
btnChange.setText("Password visible");
btnChange. xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Set a transparent background color -->
<solid android:color="#FFFFFF" />
<!-- Set a white border -->
<stroke
android:width="1px"
android:color="#FFFFFF" />
<!--Set the margins to make the space bigger --> ;
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
</shape>
Summary of this section:
That’s it for this section, thank you~