This is the 22nd day of my participation in the August Wen Challenge.More challenges in August
background
EditText is one of the most commonly used basic controls in Android. As an input box, EditText performs most of the information entry functions in a project and has a wide range of requirements. This article takes a look at the basic functions of the EditText control
The basic function
Get focus
edit.requestFocus();
Copy the code
Get focus while the keyboard pops up
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);
Copy the code
Clear focus
edit.clearFocus();
Copy the code
Clear focus and hide keyboard
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
Copy the code
Force focus
With this combination, the soft keyboard can pop up directly on some models
edit.setFocusable(true);
edit.requestFocus();
edit.setFocusableInTouchMode(true);
Copy the code
Setting cursor Position
Dynamically set the cursor position
edit.setSelection(10);
Copy the code
Set the picture
edit.setCompoundDrawables(left,top, right,bottom);
Copy the code
Listen for text changes
edit.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 s) {}});Copy the code
BeforeTextChanged is a callback before text changes,onTextChanged is a callback during text changes, and afterTextChanged is a callback after text changes. Be very careful not to get stuck in a loop. That is: text changes -> Watcher receives notification ->setText-> Text changes -> Watcher receives notification ->..
Control gets the focus callback
edit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {}});Copy the code
A hasFocus value of true indicates that the control has gained focus. A hasFocus value of false indicates that the control has lost focus
Listen for the callback of a soft keyboard keypress
edit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
return false; }});Copy the code
ActionId == EditorInfo.IME_ACTION_DONE Indicates the Done key, send key, or search key
Disable the copy and paste function
Create a class that implements all the methods in the ActionMode.Callback interface
public class ActionModeCallbackInterceptor implements ActionMode.Callback {
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
public void onDestroyActionMode(ActionMode mode) {}}Copy the code
Call it where it is needed
edit.setCustomInsertionActionModeCallback( new ActionModeCallbackInterceptor());
Copy the code