0

I am using Accessibility service and want to setText to a special EditText.

First, I get all events in a page recursively:

if(event.getClassName().equals(EditText.class.getName())){
        //event.getClassName() gets all events on a screen recursively,
        //and if it was an EditText, I want these happen:

        if(event.getSource().getText().equals("Hello")){ //special text
            event.getSource().setText("Bla Bla Bla"); //change the text
        }

}

This seems OK, but I got IllegalStateException exception, and according to this, I thought that I have to create a new Object of the current event.getClass() object. I did this:

event.getClass() editText = new event.getClass();
if(editText.getText().equals("Hello")){ //a special text
    editText.setText("Bla Bla Bla");
}

Fine, I know it's absolutely wrong, but I mean something exactly like that. Then I tried this:

EditText editText = (EditText) event.getClassName();

Again, seems OK, but I got this:

java.lang.ClassCastException: java.lang.String cannot be cast to android.widget.EditText

How can I get event's class and create on object of? I mean is there anyway to create a new EditText object of current event object? (In first if block, I'm making sure that "this specific event" is an EditText).

Mohammad Kholghi
  • 533
  • 2
  • 7
  • 21

1 Answers1

0

getClassName() return a String not an Object, you cannot cast it to a EditText.

You cannot use AccessibilityNodeInfo's setText like you're trying to do, you need something like this answer.

Kotlin equivalent:

event.source?.let {
    if (it.className == "android.widget.EditText") {
        it.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, Bundle().apply {
            putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, "android")
        })
    }
}
Paulo
  • 1