onBackPressed方法无法正常工作

时间:2021-11-27 15:21:48

In my app there is one EditText. I have called keypad show on my activity for this EditText. keypad showing & working fine.

在我的应用程序中有一个EditText。我在此活动中为此EditText调用了键盘显示。键盘显示和工作正常。

Now in currently, I have to press back button two time, one for hiding keypad & another for performing some task(like data saving to DB).I don't want to press back button two times.

现在,在目前,我必须按两次按钮,一个用于隐藏键盘,另一个用于执行某些任务(如数据保存到DB)。我不想按两次后退按钮。

please guide me, or suggestion

请指导我,或建议

here some from onBackPressed()

这里有一些来自onBackPressed()

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();

    hideKeypad();

    //saving EditText data to db.
}

code form hideKeypad()

代码表hideKeypad()

private void hideKeypad() {
       InputMethodManager imm = (InputMethodManager) 
        getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(edittext1.getWindowToken(), 0);
}

3 个解决方案

#1


1  

Use this method it is similar what you already done but I little modified it . Register listener of your main layout and pass its object as param so that when you click out side of edit text i,e. main layout keypad will gone.

使用此方法它与您已经完成的相似,但我对它进行了一些修改。注册主布局的监听器并将其对象作为参数传递,以便当您单击编辑文本的一侧时,e。主布局键盘将消失。

/** Close Keypad on touch.
 * @param view on which click happens. */

 public void closeKeyPad(View view)
    {
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);    
    }

#2


0  

The problem is that the keyboard consumes the first BackPressed event. which you can't do anything about. to solve this just listen to the keyboard hide event and finish your activity.

问题是键盘消耗了第一个BackPressed事件。你无能为力。解决这个问题只需听一下键盘隐藏事件并完成你的活动。

code to do that:

代码来做到这一点:

    edit.setOnEditorActionListener(
     new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH ||
        actionId == EditorInfo.IME_ACTION_DONE ||
        event.getAction() == KeyEvent.ACTION_DOWN &&
        event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
    if (!event.isShiftPressed()) {
       // the user is done typing.  finish the activity
       finish();
       return true; // consume.
    }                
}
return false; // pass on to other listeners. 
}
});

#3


0  

Pressing back to close the keyboard is a standard function of the UI. Why would you want to change this?

按下以关闭键盘是UI的标准功能。你为什么要改变这个?

Users who are used to using a soft-keyboard on Android devices will be used to pressing back once to close the keyboard, then once more to move back in the application.

习惯在Android设备上使用软键盘的用户将用于按下一次以关闭键盘,然后再次回到应用程序中。

If you make the back button move back in the application while the keyboard is displayed, it makes navigation different to standard Android navigation, and many users may get frustrated with it.

如果您在显示键盘时使后退按钮在应用程序中向后移动,则会使导航与标准Android导航不同,并且许多用户可能会对此感到沮丧。

Besides that, your onBackPressed is not being called because when the keyboard is displayed, the onBackPressed for the keyboard is run - which, as we've all seen - is what hides the keyboard - and not the onBackPressed for your application.

除此之外,你的onBackPressed没有被调用,因为当显示键盘时,键盘的onBackPressed正在运行 - 正如我们所见,它是隐藏键盘的 - 而不是你应用程序的onBackPressed。

Mr.Me's answer is the way to do it if you do want to go that way, but I wouldn't recommend it.

如果你想要这样做,我的回答就是这样做的方法,但我不推荐它。

#1


1  

Use this method it is similar what you already done but I little modified it . Register listener of your main layout and pass its object as param so that when you click out side of edit text i,e. main layout keypad will gone.

使用此方法它与您已经完成的相似,但我对它进行了一些修改。注册主布局的监听器并将其对象作为参数传递,以便当您单击编辑文本的一侧时,e。主布局键盘将消失。

/** Close Keypad on touch.
 * @param view on which click happens. */

 public void closeKeyPad(View view)
    {
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);    
    }

#2


0  

The problem is that the keyboard consumes the first BackPressed event. which you can't do anything about. to solve this just listen to the keyboard hide event and finish your activity.

问题是键盘消耗了第一个BackPressed事件。你无能为力。解决这个问题只需听一下键盘隐藏事件并完成你的活动。

code to do that:

代码来做到这一点:

    edit.setOnEditorActionListener(
     new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH ||
        actionId == EditorInfo.IME_ACTION_DONE ||
        event.getAction() == KeyEvent.ACTION_DOWN &&
        event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
    if (!event.isShiftPressed()) {
       // the user is done typing.  finish the activity
       finish();
       return true; // consume.
    }                
}
return false; // pass on to other listeners. 
}
});

#3


0  

Pressing back to close the keyboard is a standard function of the UI. Why would you want to change this?

按下以关闭键盘是UI的标准功能。你为什么要改变这个?

Users who are used to using a soft-keyboard on Android devices will be used to pressing back once to close the keyboard, then once more to move back in the application.

习惯在Android设备上使用软键盘的用户将用于按下一次以关闭键盘,然后再次回到应用程序中。

If you make the back button move back in the application while the keyboard is displayed, it makes navigation different to standard Android navigation, and many users may get frustrated with it.

如果您在显示键盘时使后退按钮在应用程序中向后移动,则会使导航与标准Android导航不同,并且许多用户可能会对此感到沮丧。

Besides that, your onBackPressed is not being called because when the keyboard is displayed, the onBackPressed for the keyboard is run - which, as we've all seen - is what hides the keyboard - and not the onBackPressed for your application.

除此之外,你的onBackPressed没有被调用,因为当显示键盘时,键盘的onBackPressed正在运行 - 正如我们所见,它是隐藏键盘的 - 而不是你应用程序的onBackPressed。

Mr.Me's answer is the way to do it if you do want to go that way, but I wouldn't recommend it.

如果你想要这样做,我的回答就是这样做的方法,但我不推荐它。