3

我的 Activity 的 xml 代码是

<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>

为此添加关键事件,我已经在这样的主要活动中编码

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();

}


}

当我从软键盘按 A 时...未调用 onKeyDown 或 onKeyUp 方法...有人告诉我当我在软键盘中按字母表时调用这些方法的方法..

4

1 回答 1

6

来自http://developer.android.com/reference/android/view/KeyEvent.html

*由于软输入法可以使用多种创新方式输入文本,因此无法保证软键盘上的任何按键都会产生按键事件:这由 IME 自行决定,实际上不鼓励发送此类事件。您永远不应该依赖接收 KeyEvents 来获取软输入法上的任何键。特别是,默认的软件键盘永远不会向任何针对 Jelly Bean 或更高版本的应用程序发送任何键事件,并且只会向针对 Ice Cream Sandwich 或更早版本的应用程序发送某些按下删除键和返回键的事件。请注意,其他软件输入法可能永远不会发送按键事件,无论版本如何。如果您需要与软件键盘进行特定交互,请考虑使用 IME_ACTION_DONE 等编辑器操作,

因此,大多数键不会生成任何KeyEvent。捕获按下哪个键的唯一方法是通过TextWatcher

于 2013-03-18T19:28:36.313 回答