How to show space key in softkeyboard and perform some action when user clicks on space key

你说的曾经没有我的故事 提交于 2019-12-21 20:39:11

问题


I have an edittext. In that edittext user enters some text. when user hits on space key, i need to perform some action. how can i do it.

Thanks in advance.


回答1:


To do something when the user presses the space key you use the following -

if((event.getAction()==KeyEvent.ACTION_DOWN)&&(key == KeyEvent.KEYCODE_SPACE)){
//put your code for whatever you what to do when the user presses space here
   }
}

Problems intercepting space key in EditText

If you have multiple edittexts, in the above code put

if (edittext.hasFocus()){
    //do something for particular edit text
}

I think I know why your edittext is not working. When the edittext is focused(selected), the key clicks are bypassing your activity and go in straight to the edittext. To solve this you need to move the onKeyListener into an onKeyListener for your edittext.

Try the below code instead

edittext.setOnKeyListener(new OnKeyListener() {           
        @Override
        public boolean onKey(View v, int key, KeyEvent event) {
            if (event.getAction()==KeyEvent.ACTION_DOWN && key == KeyEvent.KEYCODE_ENTER) {
                //operation that you want on key press
                return true;
            }
            return false;
        }
    });

Hope this one helps




回答2:


I think its better to use TextWatcher for EditText. and you can implement in this way

 EditText et = (EditText)findViewById(R.id.ed1);

    et.addTextChangedListener(new TextWatcher()
    {
        public void afterTextChanged(Editable s)
        {
              // Abstract Method of TextWatcher Interface.
        }
        public void beforeTextChanged(CharSequence s,
                int start, int count, int after)
        {
            // Abstract Method of TextWatcher Interface.
        }
        public void onTextChanged(CharSequence s,
                int start, int before, int count)
        {
            System.out.println(s);
            if(s.toString().equals(" "))
            {
                Toast.makeText(getApplicationContext(), "Space is clicked", Toast.LENGTH_LONG).show();
            }
        }
    });



回答3:


Try this surely useful to you when user click space button define your own action...

youredittext.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                // If the event is a key-down event on the "enter" button
                if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                    (keyCode == KeyEvent.KEYCODE_SPACE)) {
                  // Perform action on key press

                  Toast.makeText(getApplicationContext(), youredittext.getText(), Toast.LENGTH_SHORT).show();
                  return true;
                }
                return false;
            }
        });


来源:https://stackoverflow.com/questions/13393121/how-to-show-space-key-in-softkeyboard-and-perform-some-action-when-user-clicks-o

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