开发者

Android shift key listener

I need to know when the user hits the Shift key (Soft Keyboard) in my EditText. currently, I'm using an addTextChangedListener, but it isn't called when the Shift key is pressed (though it is when any other key is pressed)

What can I do to know when the user 开发者_开发技巧hits the Shift key?

Thanks,

Mat


You have to use View.OnKeyListener

edit1.setOnKeyListener(new View.OnKeyListener() {
 @Override
 public boolean onKey(View v, int keyCode, KeyEvent event) {
    if(event.isShiftPressed())
      {

       }
    return false;
  }
});


the example below can help you

//while your activity class should be implementing OnKeyListener like below
//public class MyActivity extends Activity implements OnKeyListener { ....}
myEditText.setOnKeyListener(this);

then by overriding the method of key listener you can sense the shift key

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    switch (v.getId()) 
    {
    case R.id.myEditTextId:
    if(keyCode==59)//59 is shift's keycode
    //do your stuff here against pressing shift key
    break;
    }
    }

you can get android key code list here http://qdevarena.blogspot.com/2009/04/android-keycodes-list.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜