如何使用后返回preSS软键盘关闭的活动? [英] How to close Activity with software keyboard after BACK press?
问题描述
我有一个活动,在这里我想软键盘是alaways打开。如何关闭后返回preSS的活动,当键盘打开?现在,我必须单击后退两次,第一次以关闭键盘,然后完成活动。
I have an Activity, where I want the software keyboard to be alaways opened. How to close the Activity after BACK press, when keyboard is opened? Now I have to click BACK twice, first to close the keyboard and then to finish the Activity.
推荐答案
如前所述,onKey $ P $宗座外方传教会,可以用来捕捉返回按钮,但是这需要在文本视图被覆盖,而不是在活动。
As mentioned, onKeyPreIme can be used to catch the back button, but this needs to be overridden on the text view, not in the activity.
下面是一个完整的解决方案:
Here's a complete solution:
首先,从EditText上派生新类,覆盖onKey $ P $宗座外方传教会,并调用回调接口:
First a new class that derived from EditText, overrides onKeyPreIme and calls a callback interface:
// EditTextWithBackButton class
public class EditTextWithBackButton extends EditText
{
public interface IOnBackButtonListener
{
boolean OnEditTextBackButton();
}
public EditTextWithBackButton(Context context)
{
super(context);
}
public EditTextWithBackButton(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public void setOnBackButtonListener(IOnBackButtonListener l)
{
_listener = l;
}
IOnBackButtonListener _listener;
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event)
{
if (event.getAction()==KeyEvent.ACTION_UP && keyCode==KeyEvent.KEYCODE_BACK)
{
if (_listener!=null && _listener.OnEditTextBackButton())
return false;
}
return super.onKeyPreIme(keyCode, event); //To change body of overridden methods use File | Settings | File Templates.
}
}
其次,更新您的布局:
Next, update your layout:
<com.yournamespace.whatever.EditTextWithBackButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textField"
/>
接下来,更新活动,里面的OnCreate,之后的setContentView:
Next, update your activity, inside OnCreate, after setContentView:
((EditTextWithBackButton) findViewById(R.id.textField)).setOnBackButtonListener(new EditTextWithBackButton.IOnBackButtonListener()
{
@Override
public boolean OnEditTextBackButton()
{
finish();
return true;
}
});
这篇关于如何使用后返回preSS软键盘关闭的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!