How to call one function inside another in Java for Android?

Asked

Viewed 1,892 times

4

Every letter the user types in EditText, invokes the function TextWatcher that leads to a Text-to-speech.
So if it writes 'c', the app returns a line saying 'c'. if it then enters 'a', returns 'a'.
The problem is that when the user deletes the last character entered, the app also returns the letter that is before the deleted one.
With the same example, the word formed would be 'ca'. If the user deletes 'a', the app returns 'c'.

I need to do a check of the key pressed to delete, because if it is pressed, it will not be able to trigger the text-to-speech.

   editTextPrincipal.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            keyCode = event.getKeyCode();
            if (keyCode == KeyEvent.KEYCODE_DEL){
                return true;
            }else{
                return false;
            }
        }
    });

    editTextPrincipal.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (onKey == false){
                editTextPrincipal.addTextChangedListener(textWatcher);
            }

        }
    });

I tried to make this suggested change, but it didn’t resolve:

    editTextPrincipal.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            keyCode = event.getKeyCode();
            if (keyCode != KeyEvent.KEYCODE_DEL){
                editTextPrincipal.addTextChangedListener(textWatcher);
                return true;
            }
            return false;
        }
    });

I know that code is wrong, but the logic I wanted was to call the function onKey within the setOnClickListener, in that condition of if.

My Textwatcher:

TextWatcher textWatcher = 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) {
        oqSeraFalado = editTextPrincipal.getText().toString();
        if (oqSeraFalado.length() > 1){
            oqSeraFalado = oqSeraFalado.substring(oqSeraFalado.length()-1);
            vamosFalar();
        }else{
            vamosFalar();
        }

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
};

public void vamosFalar(){
    String ToSpeak = oqSeraFalado;
    Toast.makeText(getApplicationContext(), ToSpeak, Toast.LENGTH_SHORT).show();
    tts.setPitch(1);
    tts.setSpeechRate(1);
    tts.speak(ToSpeak, TextToSpeech.QUEUE_FLUSH, null);
}

I found something funny... I made this change in the code:

    editTextPrincipal.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            keyCode = event.getKeyCode();
            Log.e("key",keyCode+"");
            if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_DOWN){
                usuarioDeletouCaracter = true;
                return usuarioDeletouCaracter;
            }
            return usuarioDeletouCaracter;

        }
    });

and in the log, it only takes the keyboard delete value only if there is nothing written in the Edittext in question. otherwise it does not take the code from any other key to the var keycode.

  • 1

    a little confusing xD

1 answer

0

Mikhael, because you need onClick in editText?

If every time the user types something your TTS should read it, I believe you don’t even need to control the textChanged System, you could call the text-to-Speech function straight from your keyListener, no?

Edit this function below /

public void vamosFalar(String toSpeak){

    Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
    tts.setPitch(1);
    tts.setSpeechRate(1);
    tts.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
}

and calls this function on your onKey()

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    keyCode = event.getKeyCode();
    if ( keyCode != KeyEvent.KEYCODE_DEL){
        vamosFalar( editTextPrincipal.getText().toString() ); //Se quiser pode validar se existe o texto no edit
    }
}
  • a classmate suggested something similar but it had no effect =\

  • Mikhael, the idea is not to add more textChangedListener as you did: editTextPrincipal.addTextChangedListener(textWatcher); I believe that within this "textWatcher" you are calling or executing a function that does the right text-to-Speech? The most correct thing in my view is for you to call this function or make this TTS treatment directly on onKey()

  • If possible, shows p/people the code of your textWatcher

  • Gabriel, I just edited the post including the textwatcher code !

  • Test with Edit of the question, so you won’t even need your textWatcher class, because onKey is already a Watcher of when something was typed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.