Android的定制的EditText和后退按钮覆盖 [英] Android custom EditText and back button override

查看:245
本文介绍了Android的定制的EditText和后退按钮覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要重写后退按钮显示软键盘时。基本上当后退按钮被击中,我想键盘辞退了,我要追加一些文字上的任何用户在编辑文本字段中键入。所以基本上我需要当键盘被驳回知道。周围搜索后,我意识到有这种没有API,而要做到这一点的唯一真正的方法是使你的EditText类。

I want to override the back button when the soft keyboard is shown. Basically when the back button is hit, I want the keyboard to dismiss, and I want to append some text onto whatever the user has typed in that edit text field. So basically I need to know when the keyboard is dismissed. After searching around, I realized there is no API for this, and that the only real way to do this would be to make your EditText class.

所以,我创建了自己的EditText类和扩展的EditText这样

So I created my own EditText class and extended EditText like this

public class CustomEditText extends EditText
{

    public CustomEditText(Context context)
    {
        super(context);
        init();
    }

    public CustomEditText(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init();
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init();
    }

    private void init()
    {

    }

}

我也加入了这个方法。

I have also added this method

    @Override
        public boolean dispatchKeyEventPreIme(KeyEvent event)
        {
            if (KeyEvent.KEYCODE_BACK == event.getKeyCode())
            {
                Log.v("", "Back Pressed");

                            //Want to call this method which will append text
                            //init();
            }
            return super.dispatchKeyEventPreIme(event);
        }

现在这个方法不覆盖的后退按钮,它会关闭键盘,但我不知道我怎么会通过文本到的EditText字段中。有谁知道我会怎么做呢?

Now this method does override the back button, it closes the keyboard, but I dont know how I would pass text into the EditText field. Does anyone know how I would do this?

另一种快速的问题,没有人知道为什么这种方法被称为两次?正如你可以看到暂时的,我添加了一个快速的logcat消息来测试它的工作原理,但是当我打的返回按钮,它打印了两次,任何理由为什么它会是这样?

Also another quick question, does anyone know why this method is called twice? As you can see for the time being, I have added a quick logcat message to test it works, but when I hit the back button, it prints it twice, any reason why it would be doing this?

任何帮助将是非常美联社preciated !!

Any help would be much appreciated!!

推荐答案

这是由于d​​ispatchKeyEvent $ P $宗座外方传教会正在呼吁两国的 ACTION_DOWN 和的 ACTION_UP 。结果
你将不得不处理,只有当KEY下来就是pssed $ P $。因此,使用

This is due to the dispatchKeyEventPreIme being called on both ACTION_DOWN and ACTION_UP.
You will have to process only when KEY down is pressed. So use

如果(event.getAction()== KeyEvent.ACTION_DOWN)

编辑:
对于第一个问题你可以做

for the first question You could do

setText(getText().toString() + " whatever you want to append"); 

dispatchKeyEvent $ P $宗座外方传教会

这篇关于Android的定制的EditText和后退按钮覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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