单击“完成”时,Android OnEditorActionListener()actionId为0

时间:2011-10-05 12:06:53

标签: android android-emulator keyboard

我创建了一个键盘。当用户输入号码时,他们会将号码发送到特定EditText,但当用户点击“完成”键时,它不会转到setOnEditorActionListener(但会关闭键盘)。< / p>

这是我的代码:

 final EditText txtQty = new EditText(this);
    txtQty.setHeight(1);
    txtQty.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 42));
    txtQty.setInputType(InputType.TYPE_CLASS_PHONE);
    txtQty.setImeOptions(EditorInfo.IME_ACTION_DONE);
    txtQty.setSelectAllOnFocus(true);
    txtQty.setTextSize(9);
    txtQty.setVisibility(View.VISIBLE);
    txtQty.setHint("0.0");
    txtQty.setHighlightColor(R.color.green);
    tr.addView(txtQty);
    txtQty.setOnEditorActionListener( new OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            Log.i("KeyBoard" ,"Inside the Edit Text");
            if (actionId == EditorInfo.IME_ACTION_DONE ||actionId == EditorInfo.IME_ACTION_NEXT ) { ......}

此处提供actionId = 0EditorInfo.IME_ACTION_NEXT = 5

当我通过Android软键盘运行时,它工作正常。

  txtQty.setOnEditorActionListener( new OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            Log.i("KeyBoard" ,"Inside the Edit Text");
            Log.i("---EditorInfo.IME_ACTION_NEXT---" , EditorInfo.IME_ACTION_NEXT);
            Log.i("---actionId---" , actionId);
            Log.i("---event---" , event);
            Log.i("---EditorInfo.IME_ACTION_DONE---" , EditorInfo.IME_ACTION_DONE);

此处提供EditorInfo.IME_ACTION_NEXT = 5, actionId = 5EditorInfo.IME_ACTION_DONE = 6, actionId = 6

但是当我浏览软键盘时,它会显示EditorInfo.IME_ACTION_NEXT = 5, actionId = 0EditorInfo.IME_ACTION_DONE = 6, actionId = 0

为什么我的软键盘上没有actionId值?

1 个答案:

答案 0 :(得分:3)

如果你想通过这种方式获得actionid:

在我的项目中,我改变了edittext的属性,如

input type  -----  text
ime options -----  actionDone

并在java文件中:

  etSearch = (EditText) findViewById(R.id.etSearch);
  etSearch.setOnEditorActionListener(mEditorActionListener);

private OnEditorActionListener mEditorActionListener = new OnEditorActionListener() {

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        // TODO Auto-generated method stub
        if (actionId == EditorInfo.IME_ACTION_DONE) {
                       //do something
        }
        return false;
    }
};

以这种方式可以得到actionid = 6;