2016-08-01 100 views
0

我使用的工具栏下方有一个搜索视图,并在搜索视图下方我放置了一个布局,其中包含一个片段和一个FloatingActionButton。无法清除搜索查看android的焦点,同时切换片段

enter image description here

搜索视图中有一个提示的城市名称的自动完成功能。

当我点击搜索按钮,它提交查询,失去焦点,隐藏的建议(这是它是如何工作的)。

但是,当我点击FloatingActionButton改变片段(得自ListView控件到MapView类),搜索视图再次抓住重点,并开始显示这些建议,即城市名称。

enter image description here

我使用下面的代码来改变碎片和清除焦点

fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if (flagFAB) { 
       fab.setImageResource(android.R.drawable.ic_menu_sort_by_size); 
       flagFAB = false; 
       if (fragmentManager.findFragmentByTag("testFrag2") != null) { 
        Log.wtf(TAG, "if the fragment exists, show it."); 
        fragmentManager.beginTransaction().show(fragmentManager.findFragmentByTag("testFrag2")).commit(); 
       } else { 
        Log.wtf(TAG, "if the fragment does not exist, add it to fragment manager."); 
        fragmentManager.beginTransaction().add(R.id.tabListBM, testFrag2, "testFrag2").commit(); 
       } 
       if (fragmentManager.findFragmentByTag("fragList") != null) { 
        Log.wtf(TAG, "if the other fragment is visible, hide it."); 
        fragmentManager.beginTransaction().hide(fragmentManager.findFragmentByTag("fragList")).commit(); 
       } 
      } else { 
       fab.setImageResource(android.R.drawable.ic_dialog_map); 
       flagFAB = true; 
       if (fragmentManager.findFragmentByTag("fragList") != null) { 
        //if the fragment exists, show it. 
        fragmentManager.beginTransaction().show(fragmentManager.findFragmentByTag("fragList")).commit(); 
       } else { 
        //if the fragment does not exist, add it to fragment manager. 
        fragmentManager.beginTransaction().add(R.id.tabListBM, listFrag, "fragList").commit(); 
       } 
       if (fragmentManager.findFragmentByTag("testFrag2") != null) { 
        //if the other fragment is visible, hide it. 
        fragmentManager.beginTransaction().hide(fragmentManager.findFragmentByTag("testFrag2")).commit(); 
       } 
      } 
      clearFocusSearchView(); 

     } 
    }); 

@Override 
public void clearFocusSearchView() { 

    if (title.contentEquals("city")) { 
     Log.wtf(TAG, "CLEAR FOCUS MAP"); 
     searchView.requestFocus(); 
     InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
     imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0); 
     getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
     searchView.clearFocus(); 
    } 
} 

我需要的是,搜索查看不应该有重点之前,除非我点击它再次。

我真的被困在这里,任何形式的帮助都会真的很感激。谢谢。

回答

2

要明确重点,同时切换片段,你应该clearFocus它在你的片段

覆盖的onCreate()

@Override 
public void onCreate(@Nullable Bundle savedInstanceState) {   
    super.onCreate(savedInstanceState); 
    setHasOptionsMenu(true); 
} 

和onCreateOptionsMenu

@Override 
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); 
    searchView.setIconified(false); 
    searchView.setIconifiedByDefault(false); 
    searchView.clearFocus(); 
    } 
} 

希望它能帮助!