gpt4 book ai didi

android - 如何在操作栏中显示 EditText 时聚焦并显示软键盘?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:16:38 26 4
gpt4 key购买 nike

我使用 ActionBarSherlock 来创建 ActionBar它有一个显示 AutoCompleteEditText 的搜索按钮(SHOW_AS_COLLAPSIBLE_ACTION_VIEW)单击“搜索”按钮时,将显示 EditText,我希望 EditText 获得焦点,从而在展开时显示软键盘这是代码

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.activity_main, menu);

boolean isLight = true;
// Add Search button
int menuItemId = menu
.add("Search")
.setIcon(
isLight ? R.drawable.ic_search_inverse
: R.drawable.ic_search)
.setActionView(R.layout.collapsible_edittext)
.setShowAsActionFlags(
MenuItem.SHOW_AS_ACTION_ALWAYS
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW)
.getItemId();
// Add Search Topic (Sub Menu)
SubMenu subMenu1 = menu.addSubMenu("Topic");
subMenu1.add(Menu.NONE, 10, 0, "All Topics");
subMenu1.add(Menu.NONE, 11, 1, "Adult");
subMenu1.add(Menu.NONE, 12, 2, "Pediatric");
subMenu1.add(Menu.NONE, 13, 13, "Drug");

MenuItem subMenu1Item = subMenu1.getItem();
subMenu1Item.setIcon(R.drawable.abs__ic_menu_moreoverflow_holo_light);
subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

searchBar = (AutoCompleteTextView) menu.findItem(menuItemId)
.getActionView().findViewById(R.id.etSearch);

setSearchSettings(searchMenuID);

// get instance of Search Button
searchWidgetItem = menu.findItem(menuItemId);

searchBar
.setOnEditorActionListener(new EditText.OnEditorActionListener() {


@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {

// Perform a Search
performSearch(v.getText().toString(),
(searchMenuID < 0) ? 3 : searchMenuID);

imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

// Collapse the ActionView (Search Bar)
searchWidgetItem.collapseActionView();

// Clear the TextEdit
v.setText("");

return true;
}
return false;
}
});
return true;
}

我尝试在 EditText 上设置一个 OnFocusChangeListener 并在它有焦点时显示 Soft Keboard,但没有成功:

searchBar.setOnFocusChangeListener(new OnFocusChangeListener() {

@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(v, InputMethodManager.SHOW_FORCED);
}
}
});

如何做到这一点?

最佳答案

找到解决方案:

searchWidgetItem.setOnActionExpandListener(new OnActionExpandListener() {

@Override
public boolean onMenuItemActionExpand(MenuItem item) {
searchBar.post(new Runnable() {
@Override
public void run() {
searchBar.requestFocus();
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(searchBar,
InputMethodManager.SHOW_IMPLICIT);
}
});
return true;
}

@Override
public boolean onMenuItemActionCollapse(MenuItem item) {

return true;
}
});

关于android - 如何在操作栏中显示 EditText 时聚焦并显示软键盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12875963/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com