setOnKeyListener not responding

后端 未结 6 1029
执念已碎
执念已碎 2021-01-05 17:43

I am new to Android and working through a to do list example from a book. I have one Activity which is displaying an EditText and a ListView beneath it. There is an onKey ev

相关标签:
6条回答
  • 2021-01-05 18:10

    Instead of setOnKeyListener, try to implement setOnEditorActionListener. Hope this helps.

    0 讨论(0)
  • 2021-01-05 18:17

    Following code solved such problem: You just need to need to add addTextChangedListener like this.Rest is self- explanatory

        editabltText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
            }
    
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
            }
    
            @Override
            public void afterTextChanged(Editable editable)
            {
    
    
            }
        });
    
    0 讨论(0)
  • 2021-01-05 18:19

    <EditText android:id="@+id/search" android:singleLine="true" android:imeOptions="actionSearch">

    try to add singleLine and imeOption attributes.

    0 讨论(0)
  • 2021-01-05 18:21

    I was able to get that same example to work by adding the following attribute to the EditText element android:inputType="text". This changed the software keyboard that came up for the user and included a Send button.

    In the setOnKeyListener code, I changed it to the following:

        myEditText.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_UP)
                    if (keyCode == KeyEvent.KEYCODE_ENTER)
                    {
                        todoItems.add(0, myEditText.getText().toString());
                        aa.notifyDataSetChanged();
                        myEditText.setText("");
                        return true;
                    }
                return false;
            }
        });
    

    Don't use KEYCODE_DPAD_CENTER as that's a hardware button for devices that had up, down, left, right arrows.

    0 讨论(0)
  • 2021-01-05 18:23

    You could try using a OnEditorActionListener instead:

    http://developer.android.com/reference/android/widget/TextView.html#setOnEditorActionListener(android.widget.TextView.OnEditorActionListener)

    You could set on the edittext in XML:

    android:imeOptions="actionDone"

    and then in code:

     myEditText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
               // your action here
               return true;
            }
          return false;
        }
     )};
    
    0 讨论(0)
  • 2021-01-05 18:31

    Are you using the virtual keyboard? In that case the problem is that an OnKeyListener only gets called by hardware buttons. Take a look here: http://developer.android.com/reference/android/view/View.OnKeyListener.html

    The first line says: Interface definition for a callback to be invoked when a hardware key event is dispatched to this view.

    Perpaps something line this will solve your problem: Validating edittext in Android

    0 讨论(0)
提交回复
热议问题