防止在按下Android后退按钮时关闭软键盘 [英] Prevent softkeyboard dismiss on the android back button press

查看:54
本文介绍了防止在按下Android后退按钮时关闭软键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有edittext的活动.活动到来时,我总是专注于那个Edittext.我通过给出

I have an activity with an edittext. when the activity comes, i always focusing that Edittext. i made the softkeyboard always visible by giving

android:windowSoftInputMode="stateAlwaysVisible" 

清单中的

.我正在按软键的完成按钮上的功能.实际上,我需要的是,当用户正在进行此活动时,应该始终显示软键盘.现在,我通过改写禁用了后退按钮,什么也不做.

in the manifest. i m doing the functionality on the done button press of the softkeyboard. Actually my need is the softkeyboard should always present when the user is on this activity. now i disabled the back button press, by overriding and do nothing.

@Override
public void onBackPressed() {
    // Do nothing
}

,但软键盘在按下后不显示.如何摆脱这一点.我已经尝试过防止软键盘被关闭,但仍然会按回去键盘被解雇.但是通过再次按后退"按钮回来,我需要的是后按功能不应该关闭软键盘.任何帮助将不胜感激.

but softkeyboard dismisses on the back press. how to get rid of this. I had tried this Prevent soft keyboard from being dismissed but still on the back press keyboard get dismisses. But it comes back by again pressing the back button, My need is backpress should not dismiss the softkeyboard. Any help will be appreciated.

推荐答案

创建一个自定义EditText,例如跟随一个接口:

create one custom EditText like follow with one interface:

public class CustomEditText extends EditText {


    public void setHandleDismissingKeyboard(
        handleDismissingKeyboard handleDismissingKeyboard) {
           this.handleDismissingKeyboard = handleDismissingKeyboard;
    }

    private handleDismissingKeyboard handleDismissingKeyboard;

    public interface handleDismissingKeyboard {
        public void dismissKeyboard();
    }

    @SuppressLint("NewApi")
    public CustomEditText(Context context, AttributeSet attrs,
                             int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        // TODO Auto-generated constructor stub
    }

    public CustomEditText(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // TODO Auto-generated constructor stub
    }


    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_UP) {
            handleDismissingKeyboard.dismissKeyboard();
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

然后在您的活动中创建初始化CustomEditText并使用:

then in your activity create initialize your CustomEditText and use:

customEditText.setHandleDismissingKeyboard(this);

然后实现类和重写方法,并将您的代码放入其中

then implement class and override method and put your code in that

有关我的答案的更多信息,您需要的所有东西都是 onKeyPreIme ,您可以在EditText类中覆盖它(正如我为您提供的那样)以处理所有的键,例如back Key或任何其他键其他键盘键,我在自定义类中放置了一个内部接口,以便从该类中回调到您想要的每个活动或类,您也可以使用静态方法来执行此操作,并且由于要防止关闭键盘,您可以只返回 true .

for more info about my answer all thing you need is onKeyPreIme, you can override that in your EditText class ( as i post for you ) to handle all key on that, like back Key or any other keyboard key, I put one inner interface in custom class to get call back from this class to each activity or class that you want, you can do that with static method too, and as you want prevent from dismissing keyboard you can just return true in that.

要使用 CustomEditText ,您可以使用xml或在Java中进行定义,并像其他小部件(Button,TextView,...)一样对其进行初始化,仅需要在XML中进行定义就像:

for using CustomEditText you can use xml or in java, define that and initialize that like other widget ( Button , TextView , ... ), and only different is you need define this in your xml like :

 <yourPackage.CustomEditText ..... />

这篇关于防止在按下Android后退按钮时关闭软键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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