1. EditText codes by focusing and defocusing
// _editText.setFocusable ( to true ); // Set the input box aggregate _editText.setFocusableInTouchMode ( to true ); // a touch focus _editText.requestFocus (); // focusing _editText.clearFocus (); // defocus
2. EditText monitor changes in the input box
// input box to change the text callback _editText.addTextChangedListener ( new 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 (the Editable S) { // each change, will call back method Toast.makeText (EditTextActivity. the this , "Change After text:" + S, Toast.LENGTH_SHORT) the .Show (); } });
Note: EditText each change, calls the method afterTextChanged
3. The complete code
<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>
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); // get the focus and lose focus _editText.setFocusable ( to 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()); // focus and defocus correction _editText.setOnFocusChangeListener ( new 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(); } } }); // input box to change the text callback _editText.addTextChangedListener ( new 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 (the Editable S) { // each change, will call back method Toast.makeText (EditTextActivity. the this , "Change After text:" + S, Toast.LENGTH_SHORT) the .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; } } } }