我有一个tabhost,每个标签我都有一个活动组。
当应用程序启动时,我按下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();
活动组中的后台代码:
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));
}
}
我使用以下代码在onClickListener
上设置editText
:
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并且我从childactivity返回的时间返回false。
当我点击另一个标签然后返回第一个标签页,然后我点击editText它再次返回true。
编辑:我得到了一个临时修复,我在editTextbox上设置了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回来时,首先必须在键盘显示之前用代码切换标签= /
有人得到了解释吗?
答案 0 :(得分:4)
在我使用Activity Group的应用程序中,我使用下面的代码来解决同样的问题
YOUR_EDIT_TEXT.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(searchName
.getApplicationWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
return false;
}
});
它工作正常。所以试试这段代码。
答案 1 :(得分:1)
您可以尝试将requestFocus />
元素添加到EditText
的定义中,即
<EditText android:id="@+id/edit_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text" >
<requestFocus />
</EditText>
如果没有帮助,请致电requestFocus()
back()
方法中重点关注