How to make the program clamp a regular button in Android Studio? Not visually clamped, but that she did the same thing as a person when pressing a button, only herself.

  • The question is rather strange, but if we are talking about a mobile application, then it’s not so easy when creating a program to call a function that hangs on the button. If we are talking about Android Studio, then I can not understand what exactly you are asking)) - Andrew Goroshko
  • Write in more detail what you want to do, but rather show the code - danilshik
  • @AndrewGoroshko we are talking about Android Studio. I made a button. How to make it clamped automatically? - Alexander
  • 2
    I will now reformulate the question: are you talking about a button in the IDE or in a program for a mobile device that you made in this IDE? - Andrew Goroshko

1 answer 1

An ordinary change listener in the input field will be suitable for your task. The button is certainly cool, but I think that it is not necessary to use it. For EditText there is a listener who accepts a string and if it corresponds to some previously known word, then the program makes a transition to another activation. For example:

 //Инициализируем наши элементы: mEditTxt = (EditText) findViewById(R.id.edittext); 

Next we hang the listener text changes:

mEditTxt .addTextChangedListener(textWatcher);

Well, the listener himself:

 //Создаем экземпляр TextWatcher: private final TextWatcher textWatcher = new TextWatcher() { public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void onTextChanged(CharSequence s, int start, int before, int count) { } //Задаем действия для TextView после смены введенных в EditText символов: public void afterTextChanged(Editable s) { } }; 

The implementation of Text Watcher is quite simple, thanks to its 3 methods:

 -beforeTextChanged () - работает до изменений в тексте; -onTextChanged () - работает во время ввода текста; -afterTextChanged () - работает после ввода текста, уведомляет, что текст был изменен. 

accordingly, you need the method that will work after changing the text:

 public void afterTextChanged(Editable s) { if (s.length() > 0) { String word = s.getText.toString().trim(); switch (word){ case "машина": Intent int1 = new Intent(YourActivity.this, CarActivity.class) startActivity(int1) break; case "трактор": break; }} //Иначе: else{ // выводим сообщение что нужно что-то ввести }}} 

as you can see here there is a function после того как текст изменен . There you read characters from your input field, and continue to make a comparison. Of course, you can be confused with the button, but I think that you can solve the problem much faster and easier. Here is a link to the source.

Perhaps there are errors in the correct use of functions and variables, there is no possibility now to open a studio for testing, but the meaning should be clear.

If you still need to somehow make a program with a button, then write in comments, and we will think in the direction of the button.

  • Comments are not intended for extended discussion; conversation moved to chat . - Yuriy SPb
  • one
    Finally, I just didn’t see the button appear to move) thanks - Andrew Goroshko