我有一个TabHost,每个选项卡都有一个ActivityGroup。
当应用程序启动并且我点击EditText时,键盘会弹出。但是,当我启动子活动,然后返回主活动时,键盘不再弹出。
我启动子活动的代码如下:
Intent i = new Intent(this, ShowAddFoodToSelection.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
View view = ActivityGroupMeal.group.getLocalActivityManager().startActivity(DataParser.activityIDMeal, i).getDecorView();
ActivityGroupMeal.group.setContentView(view);
我的代码用于返回到主活动
ActivityGroupMeal.group.back();
在ActivityGroup中的后台代码:
public void back() {
try {
// if we set history.size() > 0 and we press back key on home
// activity
// and then on another activity we wont get back!
if (history.size() > 1) {
history.remove(history.size() - 1);
// call the super.setContent view! so set the real view
super.setContentView(history.get(history.size() - 1));
} else {
}
} catch (Exception e) {
if (history.size() >= 0)
super.setContentView(history.get(0));
}
}
我在
editText
上设置了一个onClickListener
,代码如下:private void keyboardShow() {
InputMethodManager inputManager = (InputMethodManager) ActivityGroupMeal.group.getSystemService(Context.INPUT_METHOD_SERVICE);
boolean test = inputManager.showSoftInput(editTextSearch, InputMethodManager.SHOW_IMPLICIT);
Toast.makeText(this, "show keyboard " + test, Toast.LENGTH_SHORT).show();
}
第一次它返回 true,当我从子活动回来时它返回 false。
当我点击另一个选项卡,然后回到第一个选项卡,然后我再次点击 editText 时,它再次返回 true。
编辑:我有一个临时解决方法,我在 editText 上设置了一个 onClicklistener,然后在那里使用代码显示键盘。
InputMethodManager inputManager = (InputMethodManager) ActivityGroupMeal.group
.getSystemService(Context.INPUT_METHOD_SERVICE);
// show keyboard , when it fails first switch tab and then try again
if (!inputManager.showSoftInput(null, InputMethodManager.SHOW_FORCED)) {
// switch from tab and back
// the keyboard wont show if we dont do this
ShowHomeTab parentActivity;
parentActivity = (ShowHomeTab) this.getParent().getParent();
parentActivity.goToTab(DataParser.activityIDTracking);
parentActivity.goToTab(DataParser.activityIDShowFoodList);
inputManager.showSoftInput(null, InputMethodManager.SHOW_FORCED);
}
当我从子活动(childactivity)返回时,我必须先使用代码切换选项卡,然后键盘才会显示出来 =/。有人能解释一下吗?