在我的应用程序中,我有一个包含多个片段的actionbar活动。在搜索活动时,结果显示在一个新片段中。我单击提交按钮后试图隐藏键盘,没有任何运气。这是我的代码
public class SymptomNavigator extends ActionBarActivity implements
MainBodyArea.Communicator, SearchResult.communicator, OnClickListener {
SearchView searchView;
FragmentManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.symptom_navigator);
Button generateButton = (Button) findViewById(R.id.btn_Generate);
generateButton.setOnClickListener(this);
Button symptomListButton = (Button) findViewById(R.id.btn_ViewList);
symptomListButton.setOnClickListener(this);
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null) {
return;
}
MainBodyArea firstFragment = new MainBodyArea();
firstFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
}
manager = getSupportFragmentManager();
mainFrag = (MainBodyArea) manager
.findFragmentById(R.id.mainBodyFragment);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
searchView = (SearchView) MenuItemCompat
.getActionView(searchItem);
final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
query.trim();
searchView.clearFocus();
if (FormValidator.isValidEntry(query)) {
respondToSearch(query);
}
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
return true;
}
@Override
public void respondToSearch(String value) {
SearchResult newFragment = new SearchResult();
Bundle args = new Bundle();
args.putString(SearchResult.ARG_POSITION, value);
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack("search");
getSupportFragmentManager().popBackStack("search", FragmentManager.POP_BACK_STACK_INCLUSIVE);
transaction.commit();
}
}
正如您在上面的代码中看到的,我在onqquery textlistener中的搜索视图上调用了clearFocus。它隐藏键盘并在新片段事务完成后再次显示它。我如何使它工作?
注意:我也尝试过以下方法而没有任何运气
try {
InputMethodManager input = (InputMethodManager) getActivity()
.getSystemService(Activity.INPUT_METHOD_SERVICE);
input.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
}catch(Exception e) {
e.printStackTrace();
}
答案 0 :(得分:2)
经过数小时的搜索后,我设法用一行代码修复它。正如我所说,问题是在生成结果后,操作栏正在集中注意力。
我所要做的只是把这个
searchView.setFocusable(false);
在调用结果片段之前。希望这有助于某人!
答案 1 :(得分:1)
尝试此操作以隐藏软键盘
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
要隐藏,请始终使用以下内容
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
使用此键盘时,仅在选择文本字段或类似内容时显示键盘