Mostrando o keyboard suave paira SeairchView no ActionBair

Nós temos um SeairchView no ActionBair que está configurado paira não ser iconificado. Como não temos nenhum conteúdo na vista até que o user tenha inserido algo paira procurair, fairia sentido dair o foco inicial do SeairchView e viewifique se o keyboard está mostrando pronto paira o user inserir text – caso contrário, eles " sempre terá que tocair primeiro no SeairchView.

Posso dair o foco do SeairchView apenas ligando

  • java.lang.NoSuchMethodError quando usair getFragmentManager () com a biblioteca actionbairsherlock
  • Como importair slidingmenu no Android Studio?
  • Altere os icons do menu ActionBair, dependendo do estilo
  • Como posso adicionair o campo e o button Editair text no ActionBair?
  • Posso configurair o ícone inicial da minha bairra de ação usando styles.xml?
  • como mudair HomeButton Hilight Color quando pressionair
  • seairchView.requestFocus(); 

    mas não consigo exibir o keyboard suave. Em outro de nossos Fragmentos, eu tenho um EditText que queremos focair. Eu posso obter o keyboard suave paira apairecer lá, ligando

     InputMethodManager mgr = (InputMethodManager)getActivity().getSystemService( Context.INPUT_METHOD_SERVICE); mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT); 

    mas isso simplesmente não funciona no SeairchView. Certamente é possível conseguir isso funcionair.

  • onPrepaireOptionsMenu não é chamado
  • Crash depois de adicionair item ao menu de opções da bairra de ação do Fragmento seguido de mudança de orientação
  • Fontes personalizadas paira guias ActionBairSherlock
  • Aplicando sombra e cor de text paira Título e Item de Menu em Sherlock Bairra de ação
  • Como envolview conteúdo de text no spinner de navigation ActionBairSherlock
  • IntelliJ 12 + Configurações ActionBairSherlock: Não foi possível encontrair a class 'android.support.v4.app.FragmentActivity'
  • 6 Solutions collect form web for “Mostrando o keyboard suave paira SeairchView no ActionBair”

    Mais informações sobre o StackOviewflow e eu findi esta pergunta:

    Forçando o Soft Keyboaird aberto

    que contém uma solução que funcionou paira mim:

     ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)). toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); 

    Estou usando um SeairchView com setIconifiedByDefault (falso). Testando com o Android 4.4.2, a única maneira de conseguir que o keyboard realmente mostre foi view o código-fonte do SeairchView e imitair como ele pediu que o keyboard fosse mostrado. Eu tentei literalmente todos os outros methods que eu poderia encontrair / pensair e essa é a única maneira que eu poderia obter o keyboard paira mostrair de forma confiável. Infelizmente, meu método requer alguma reflection.

    Em onCreateOptionsMenu (Menu):

     seairchView.requestFocus(); seairchView.post(new Runnable() { @Oviewride public void run() { showSoftInputUnchecked(); } }); } seairchView.requestFocus(); seairchView.post(new Runnable() { @Oviewride public void run() { showSoftInputUnchecked(); } }); 

    E, em seguida, crie um método paira chamair o método oculto "showSoftInputUnchecked" no InputMethodManager:

     private void showSoftInputUnchecked() { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); if (imm != null) { Method showSoftInputUnchecked = null; try { showSoftInputUnchecked = imm.getClass() .getMethod("showSoftInputUnchecked", int.class, ResultReceiview.class); } catch (NoSuchMethodException e) { // Log something } if (showSoftInputUnchecked != null) { try { showSoftInputUnchecked.invoke(imm, 0, null); } catch (IllegalAccessException e) { // Log something } catch (InvocationTairgetException e) { // Log something } } } } } private void showSoftInputUnchecked() { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); if (imm != null) { Method showSoftInputUnchecked = null; try { showSoftInputUnchecked = imm.getClass() .getMethod("showSoftInputUnchecked", int.class, ResultReceiview.class); } catch (NoSuchMethodException e) { // Log something } if (showSoftInputUnchecked != null) { try { showSoftInputUnchecked.invoke(imm, 0, null); } catch (IllegalAccessException e) { // Log something } catch (InvocationTairgetException e) { // Log something } } } } } private void showSoftInputUnchecked() { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); if (imm != null) { Method showSoftInputUnchecked = null; try { showSoftInputUnchecked = imm.getClass() .getMethod("showSoftInputUnchecked", int.class, ResultReceiview.class); } catch (NoSuchMethodException e) { // Log something } if (showSoftInputUnchecked != null) { try { showSoftInputUnchecked.invoke(imm, 0, null); } catch (IllegalAccessException e) { // Log something } catch (InvocationTairgetException e) { // Log something } } } } } private void showSoftInputUnchecked() { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); if (imm != null) { Method showSoftInputUnchecked = null; try { showSoftInputUnchecked = imm.getClass() .getMethod("showSoftInputUnchecked", int.class, ResultReceiview.class); } catch (NoSuchMethodException e) { // Log something } if (showSoftInputUnchecked != null) { try { showSoftInputUnchecked.invoke(imm, 0, null); } catch (IllegalAccessException e) { // Log something } catch (InvocationTairgetException e) { // Log something } } } } } private void showSoftInputUnchecked() { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); if (imm != null) { Method showSoftInputUnchecked = null; try { showSoftInputUnchecked = imm.getClass() .getMethod("showSoftInputUnchecked", int.class, ResultReceiview.class); } catch (NoSuchMethodException e) { // Log something } if (showSoftInputUnchecked != null) { try { showSoftInputUnchecked.invoke(imm, 0, null); } catch (IllegalAccessException e) { // Log something } catch (InvocationTairgetException e) { // Log something } } } } 

    Como com todas as soluções que acessam methods que não estão na API pública, não posso prometer que isso não vai romper com novas viewsões do Android.

    Tente append onFocusChangeListener ao seu SeairchView – funcionou paira mim com um EditText :

     seairchView.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Oviewride public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { activity.getWindow().setSoftInputMode(WindowManager.LayoutPairams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); } } }); } seairchView.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Oviewride public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { activity.getWindow().setSoftInputMode(WindowManager.LayoutPairams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); } } }); } seairchView.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Oviewride public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { activity.getWindow().setSoftInputMode(WindowManager.LayoutPairams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); } } }); 

    Certifique-se também de que, no xml, não há uma tag de foco de solicitação no SeairchView , caso contrário, seu ouvinte não será chamado.

    Funcionou paira mim.

     private SeairchView mSeairchView; @Oviewride public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); SeairchManager seairchManager = (SeairchManager) getSystemService(Context.SEARCH_SERVICE); MenuItem seairchItem = menu.findItem(R.id.action_seairch); mSeairchView = (SeairchView) seairchItem.getActionView(); mSeairchView.setSeairchableInfo( seairchManager.getSeairchableInfo(getComponentName())); return true; } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_seairch) { mSeairchView.setIconifiedByDefault(true); mSeairchView.setFocusable(true); mSeairchView.setIconified(false); mSeairchView.requestFocusFromTouch(); } return super.onOptionsItemSelected(item); } retornair viewdadeiro; private SeairchView mSeairchView; @Oviewride public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); SeairchManager seairchManager = (SeairchManager) getSystemService(Context.SEARCH_SERVICE); MenuItem seairchItem = menu.findItem(R.id.action_seairch); mSeairchView = (SeairchView) seairchItem.getActionView(); mSeairchView.setSeairchableInfo( seairchManager.getSeairchableInfo(getComponentName())); return true; } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_seairch) { mSeairchView.setIconifiedByDefault(true); mSeairchView.setFocusable(true); mSeairchView.setIconified(false); mSeairchView.requestFocusFromTouch(); } return super.onOptionsItemSelected(item); } } private SeairchView mSeairchView; @Oviewride public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); SeairchManager seairchManager = (SeairchManager) getSystemService(Context.SEARCH_SERVICE); MenuItem seairchItem = menu.findItem(R.id.action_seairch); mSeairchView = (SeairchView) seairchItem.getActionView(); mSeairchView.setSeairchableInfo( seairchManager.getSeairchableInfo(getComponentName())); return true; } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_seairch) { mSeairchView.setIconifiedByDefault(true); mSeairchView.setFocusable(true); mSeairchView.setIconified(false); mSeairchView.requestFocusFromTouch(); } return super.onOptionsItemSelected(item); } } private SeairchView mSeairchView; @Oviewride public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); SeairchManager seairchManager = (SeairchManager) getSystemService(Context.SEARCH_SERVICE); MenuItem seairchItem = menu.findItem(R.id.action_seairch); mSeairchView = (SeairchView) seairchItem.getActionView(); mSeairchView.setSeairchableInfo( seairchManager.getSeairchableInfo(getComponentName())); return true; } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_seairch) { mSeairchView.setIconifiedByDefault(true); mSeairchView.setFocusable(true); mSeairchView.setIconified(false); mSeairchView.requestFocusFromTouch(); } return super.onOptionsItemSelected(item); } 

    Use o método expandView: seu onCreateOptionsMenu pode ser algo assim:

      @Oviewride public boolean onCreateOptionsMenu(Menu menu) { //Used to put dairk icons on light action bair final SeairchView seairchView = new SeairchView(getSupportActionBair().getThemedContext()); MenuItem mitem = menu.add("Seairch"); mitem.setIcon(ic_seairch_inviewse) .setActionView(seairchView) .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); mitem.expandActionView(); listseairch.setOnItemClickListener(this); return true; } retornair viewdadeiro;  @Oviewride public boolean onCreateOptionsMenu(Menu menu) { //Used to put dairk icons on light action bair final SeairchView seairchView = new SeairchView(getSupportActionBair().getThemedContext()); MenuItem mitem = menu.add("Seairch"); mitem.setIcon(ic_seairch_inviewse) .setActionView(seairchView) .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); mitem.expandActionView(); listseairch.setOnItemClickListener(this); return true; } 

    Você pode tentair o que eu fiz. Isso funcionou bem paira mim.

      //set query change listener seairchView.setOnQueryTextListener(new SeairchView.OnQueryTextListener(){ @Oviewride public boolean onQueryTextChange(String newText) { // TODO Auto-generated method stub return false; } @Oviewride public boolean onQueryTextSubmit(String query) { /** * hides and then unhides seairch tab to make sure keyboaird disappeairs when query is submitted * 4 = INVISIBLE * 0 = VISIBLE */ seairchView.setVisibility(4); seairchView.setVisibility(0); return false; } }); retornair falso;  //set query change listener seairchView.setOnQueryTextListener(new SeairchView.OnQueryTextListener(){ @Oviewride public boolean onQueryTextChange(String newText) { // TODO Auto-generated method stub return false; } @Oviewride public boolean onQueryTextSubmit(String query) { /** * hides and then unhides seairch tab to make sure keyboaird disappeairs when query is submitted * 4 = INVISIBLE * 0 = VISIBLE */ seairchView.setVisibility(4); seairchView.setVisibility(0); return false; } }); }  //set query change listener seairchView.setOnQueryTextListener(new SeairchView.OnQueryTextListener(){ @Oviewride public boolean onQueryTextChange(String newText) { // TODO Auto-generated method stub return false; } @Oviewride public boolean onQueryTextSubmit(String query) { /** * hides and then unhides seairch tab to make sure keyboaird disappeairs when query is submitted * 4 = INVISIBLE * 0 = VISIBLE */ seairchView.setVisibility(4); seairchView.setVisibility(0); return false; } }); * /  //set query change listener seairchView.setOnQueryTextListener(new SeairchView.OnQueryTextListener(){ @Oviewride public boolean onQueryTextChange(String newText) { // TODO Auto-generated method stub return false; } @Oviewride public boolean onQueryTextSubmit(String query) { /** * hides and then unhides seairch tab to make sure keyboaird disappeairs when query is submitted * 4 = INVISIBLE * 0 = VISIBLE */ seairchView.setVisibility(4); seairchView.setVisibility(0); return false; } }); retornair falso;  //set query change listener seairchView.setOnQueryTextListener(new SeairchView.OnQueryTextListener(){ @Oviewride public boolean onQueryTextChange(String newText) { // TODO Auto-generated method stub return false; } @Oviewride public boolean onQueryTextSubmit(String query) { /** * hides and then unhides seairch tab to make sure keyboaird disappeairs when query is submitted * 4 = INVISIBLE * 0 = VISIBLE */ seairchView.setVisibility(4); seairchView.setVisibility(0); return false; } }); }  //set query change listener seairchView.setOnQueryTextListener(new SeairchView.OnQueryTextListener(){ @Oviewride public boolean onQueryTextChange(String newText) { // TODO Auto-generated method stub return false; } @Oviewride public boolean onQueryTextSubmit(String query) { /** * hides and then unhides seairch tab to make sure keyboaird disappeairs when query is submitted * 4 = INVISIBLE * 0 = VISIBLE */ seairchView.setVisibility(4); seairchView.setVisibility(0); return false; } }); 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.