android EditText 的聚焦和失焦,输入框的监听

 

1. EditText通过代码聚焦和失焦

//
 _editText.setFocusable(true);//设置输入框可聚集
 _editText.setFocusableInTouchMode(true);//设置触摸聚焦

 
_editText.requestFocus(); //聚焦
_editText.clearFocus(); //失焦

 

2. EditText输入框改变监听

//输入框文本改变的回调
 _editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        Toast.makeText(EditTextActivity.this, "text change before", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        Toast.makeText(EditTextActivity.this, "text changing", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void afterTextChanged(Editable s) {
        // 每次改变,会回调此方法
        Toast.makeText(EditTextActivity.this, "text change after:" + s, Toast.LENGTH_SHORT).show();
    }
});

注意:EditText每次改变,会调用 afterTextChanged 方法

 

3. 完整代码

android EditText 的聚焦和失焦,输入框的监听
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:gravity="left"
    android:orientation="vertical"
    android:padding="20dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Text模式:" />

    <EditText
        android:id="@+id/edit_text_text"
        android:layout_width="300dp"
        android:layout_height="40dp"                        
      android:background="@drawable/edit_background"
        android:hint="text"
        android:inputType="text"
        android:padding="5dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button_edit_focus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="聚焦" />

        <Button
            android:id="@+id/button_edit_blur"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:padding="10dp"
            android:text="失焦" />
    </LinearLayout>   

</LinearLayout> 
activity_edit_text.xml

 

android EditText 的聚焦和失焦,输入框的监听
package com.example.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.myapplication.R;

public class EditTextActivity extends AppCompatActivity {

    private EditText _editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_text);

        _editText = findViewById(R.id.edit_text_text);

        //获取焦点和失去焦点
        _editText.setFocusable(true);
        _editText.setFocusableInTouchMode(true);

        Button button_focus = findViewById(R.id.button_edit_focus);
        button_focus.setOnClickListener(new EditTextListener());
        Button button_blur = findViewById(R.id.button_edit_blur);
        button_blur.setOnClickListener(new EditTextListener());

        //聚焦和失焦的回调
        _editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus) {
                    Toast.makeText(EditTextActivity.this, "text focus", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(EditTextActivity.this, "text blur", Toast.LENGTH_SHORT).show();
                }
            }
        });
        //输入框文本改变的回调
        _editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Toast.makeText(EditTextActivity.this, "text change before", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Toast.makeText(EditTextActivity.this, "text changing", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void afterTextChanged(Editable s) {
                // 每次改变,会回调此方法
                Toast.makeText(EditTextActivity.this, "text change after:" + s, Toast.LENGTH_SHORT).show();
            }
        });
    }

    class EditTextListener implements View.OnClickListener{
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.button_edit_focus :
                    _editText.requestFocus(); //聚焦
                    break;
                case R.id.button_edit_blur:
                    _editText.clearFocus(); //失焦
                    break;
            }
        }
    }
}
EditTextActivity.java

android EditText 的聚焦和失焦,输入框的监听

上一篇:Android app校正系统时钟


下一篇:Android binder介绍(下)