How to determine if someone is typing on editText [duplicate]

巧了我就是萌 提交于 2019-12-13 22:35:26

问题


I am designing a chat application using android studio and i am using fire-base as cloud service provider and i got stock on how to make a listener like if the user is typing on the edit-text field The value of the user on fire-base will changed into true and when the user is not typing the value will become false.? I am looking for this solution for about a week, and didn't find any answer regarding on my researches.

How to put TextWatcher?

EDIT

       final Firebase firebase = new Firebase("https://my-first-sample-is.firebaseio.com/");


            editText = (EditText) findViewById(R.id.editText);

            listView = (ListView) findViewById(R.id.listView);

            listView.setAdapter(new MessageAdapter(this, Message.class, R.layout.fragment_message, firebase.child("chat")));
            editText.setAdapter(new RoomTypeAdapter(this, RoomType.class, R.layout.fragment_message1, firebase.child("Room-Typing")));

            button = (Button) findViewById(R.id.button);



            button.setOnClickListener(new View.OnClickListener() {


                @Override
                public void onClick(View view) {
  Message message = new Message();

                message.setMessage(editText.getText().toString());
                editText.setText("");
                message.setAuthor("Name");
                message.setNumMessages(numMessages);


                firebase.child("chat").push().setValue(message);


            }
        });
        editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {

                    return true;
                }
                return false;
            }
        });
    }

回答1:


Implement the TextWatcher on your edittext like this

    EditText myTextBox = (EditText) findViewById(R.id.myTextBox);
  myTextBox.addTextChangedListener(new TextWatcher() {

   public void afterTextChanged(Editable s) {
   }

   public void beforeTextChanged(CharSequence s, int start, 
     int count, int after) {
   }

   public void onTextChanged(CharSequence s, int start, 
     int before, int count) {
   // Here you can trigger your another code/function about which you are asking
   }
  });

Hope it will help!




回答2:


you can use OnFocusChangeListener, if the textview has focus then the user is typing. for more information you can check here http://developer.android.com/reference/android/view/View.OnFocusChangeListener.html



来源:https://stackoverflow.com/questions/33797431/how-to-determine-if-someone-is-typing-on-edittext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!