同时使用软键盘的onkeydown不调用 [英] onKeyDown is not invoked while using soft keypads

查看:198
本文介绍了同时使用软键盘的onkeydown不调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的XML code代表我的活动是

My xml code for my Activity is

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<EditText
    android:id="@+id/et1"
            android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

</RelativeLayout>

要添加关键事件为了这个,我有这样的主要活动codeD

TO add Key Event for this I have coded in main activity like this

public class MainActivity extends Activity
{
EditText e1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     e1=(EditText)findViewById(R.id.et1);
/*  e1.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            Log.i("keycode","Inside on key");
            //char a=event.getDisplayLabel();
            char a = (char)event.getUnicodeChar();
      Log.i("keycode",Character.toString(a));               
            if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                    (keyCode == KeyEvent.KEYCODE_ENTER)) {
                Log.i("keycode",Integer.toString(keyCode));
                Log.i("keycode",(e1.getText()).toString());
                return true;
            }
            else
            {
                return false;
            }


            // TODO Auto-generated method stub

        }
    });*/


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    Log.i("key","KeyPressed"+Integer.toString(keyCode));
    Log.i("keycode",(e1.getText()).toString());
    return super.onKeyDown(keyCode, event);
}



@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub

            switch(keyCode)
    {
    case KeyEvent.KEYCODE_A:
    {
        Log.i("key1","KeyPressed A");
        Log.i("keycode1",(e1.getText()).toString());

        return true;
    }
    }
    return super.onKeyUp(keyCode, event);
}
@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    new AlertDialog.Builder(this)
    .setIcon(android.R.drawable.ic_dialog_alert)
    .setTitle("Closing Activity")
    .setMessage("Are you sure you want to close this activity?")
    .setPositiveButton("Yes", new DialogInterface.OnClickListener()
  {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        finish();    
    }

})
.setNegativeButton("No", null)
.show();

}


}

当我从软键盘$ P $的PSS的......的方法OnKeyDown或的onkeyup不叫......有人说我来调用这些方法时,我preSS字母软键盘..顺便

when I press A from the soft Keypad...the method onKeyDown or onKeyUp is Not called...somebody say me the way to invoke these methods when i press alphabet in soft Keypad..

推荐答案

从<一个href=\"http://developer.android.com/reference/android/view/KeyEvent.html\">http://developer.android.com/reference/android/view/KeyEvent.html

*作为软输入的方法可以使用​​输入多个文本和创造性的方式,但不保证该软键盘上的任意键preSS将产生的键事件:这是留给IME的决定,而实际上发送此类事件气馁。你永远不应该依赖于接收的KeyEvent对软输入法的任意键。尤其是,默认的软件键盘绝不会任意键事件发送到任何应用程序目标定位果冻豆或更高版本,将只发送事件删除的一些presses和返回键到打靶冰淇淋三明治或应用程序更早。要知道,其他软件输入法可能永远不会发送关键事件无论版本。如果您需要特定的相互作用与软件键盘,因为它提供了更多的知名度,以用户为你的应用程序将如何关键presses反应考虑使用编辑器操作,如IME_ACTION_DONE。*

*As soft input methods can use multiple and inventive ways of inputting text, there is no guarantee that any key press on a soft keyboard will generate a key event: this is left to the IME's discretion, and in fact sending such events is discouraged. You should never rely on receiving KeyEvents for any key on a soft input method. In particular, the default software keyboard will never send any key event to any application targetting Jelly Bean or later, and will only send events for some presses of the delete and return keys to applications targetting Ice Cream Sandwich or earlier. Be aware that other software input methods may never send key events regardless of the version. Consider using editor actions like IME_ACTION_DONE if you need specific interaction with the software keyboard, as it gives more visibility to the user as to how your application will react to key presses.*

因此​​,大多数键不会产生任何的的KeyEvent
捕捉到了哪个键的唯一途径是pressed通过的 TextWatcher

Thus most keys do not generate any KeyEvent. The only way to capture which key is pressed is through TextWatcher.

这篇关于同时使用软键盘的onkeydown不调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆