如何在Android Studio的Activity中调用EditText的onSelectionChanged方法
Got it, let's figure out how to get the selection change events in your MainActivity—since you can't directly call the protected onSelectionChanged method from outside the custom EditText class, we'll use a callback interface to bridge the gap. Here's the step-by-step solution:
Step 1: Update your EditTextCursorWatcher class with a callback interface
We'll add a listener interface to let the custom EditText notify MainActivity when the selection changes:
public class EditTextCursorWatcher extends EditText { // Define a callback interface for selection changes public interface OnSelectionChangeListener { void onSelectionChanged(int selStart, int selEnd); } private OnSelectionChangeListener mSelectionListener; // Keep your existing constructors public EditTextCursorWatcher(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public EditTextCursorWatcher(Context context, AttributeSet attrs) { super(context, attrs); } public EditTextCursorWatcher(Context context) { super(context); } // Method to set the listener from outside the class public void setOnSelectionChangeListener(OnSelectionChangeListener listener) { mSelectionListener = listener; } @Override protected void onSelectionChanged(int selStart, int selEnd) { super.onSelectionChanged(selStart, selEnd); // Don't forget to call the parent method // Keep your original Toast logic Toast.makeText(getContext(), "selStart is " + selStart + ", selEnd is " + selEnd, Toast.LENGTH_LONG).show(); // Notify the listener (if set) about the selection change if (mSelectionListener != null) { mSelectionListener.onSelectionChanged(selStart, selEnd); } } }
Step 2: Listen for selection changes in MainActivity
Now you can register a listener on your custom EditText in MainActivity to handle the selection events directly:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Find your custom EditText EditTextCursorWatcher customEditText = findViewById(R.id.etEdit); // Set the selection change listener customEditText.setOnSelectionChangeListener(new EditTextCursorWatcher.OnSelectionChangeListener() { @Override public void onSelectionChanged(int selStart, int selEnd) { // This is where you handle the selection change in MainActivity Log.d("MainActivity", "Selection updated: start = " + selStart + ", end = " + selEnd); // Add your own logic here (e.g., update UI, process text, etc.) } }); } }
Note on your layout file
Your existing layout reference is correct—just make sure you use the full package name for your custom class:
<com.example.EditTextCursorWatcher android:id="@+id/etEdit" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="top" android:minLines="5" android:inputType="textMultiLine"/>
Why this works
The onSelectionChanged method is protected, meaning it can only be called within the class or its subclasses. By adding a callback interface, we create a way for the custom EditText to pass the selection data to MainActivity whenever the event occurs.
内容的提问来源于stack exchange,提问作者firozsaifi24

