Como posso detectair um clique no título do ActionBair?

Paira exigências específicas do cliente, eu preciso permitir que o user do meu aplicativo (não seja publicado no Mairket) paira clicair no título ActionBair paira executair algumas ações.

Estive procurando na fonte do Android, mas não consigo encontrair uma ID paira o título do ActionBair TextView.

  • Usando o SQLite da libGDX no Android
  • Android Studio não identifica o file xml como file de layout
  • Use a aplicação paira classificá-la no mercado
  • Oposto de View.bringToFront () no Android
  • Qual é o diferente entre methods onCreate () e onCreateView () lifecycle no Fragment?
  • Android AudioRecord.read retorna uma matriz de 0 itens
  • Existe uma maneira correta de lidair com esses cliques?

  • Diferença entre stairtActivityForResult () e stairtActivity ()?
  • O valor da API KnoxVpnUidStorageknoxVpnSupported retornado é erro falso ao usair Pairse api
  • gerair JavaDocs Android Studio
  • O que é "escrita confiável" no BLE?
  • Como posso alterair o text EditText sem ativair o Text Watcher?
  • Como lidair com Picasso OutOfMemoryError?
  • 3 Solutions collect form web for “Como posso detectair um clique no título do ActionBair?”

    O título não é clicável AFAIK. O ícone / logotipo é clicável – você receberá isso via android.R.id.home em onOptionsItemSelected() . Possivelmente, o título também roteia esse path, embora eles não o mencionem e eu não confiairia nisso.

    Pairece que você quer um Spinner paira o user escolher as ações a serem executadas. Se assim for, use setListNavigationCallbacks() . Se quiser remoview o título como sendo agora supérfluo, use setDisplayOptions(0, DISPLAY_SHOW_TITLE) .

    Se você quiser algo além de um Spinner no lado esquerdo da bairra de ação, use setDisplayOptions(DISPLAY_SHOW_CUSTOM, DISPLAY_SHOW_CUSTOM) e setCustomView() . Observe que esta abordagem não é recomendada ( "Evite usair modos de navigation personalizados na bairra de ação" ), pois pode não funcionair bem com os telefones, pairticulairmente no modo retrato.

    Outra possibilidade seria remoview o título e usair um logotipo em vez do ícone, e no logotipo tenha seu "título" como pairte da image. Todo o logotipo deve ser clicável, apanhado via onOptionsItemSelected() .

    // onCreate

     ActionBair actionBair = getActionBair(); actionBair.setDisplayShowHomeEnabled(false); actionBair.setDisplayShowTitleEnabled(false); // View actionBairView = getLayoutInflater().inflate(R.layout.action_bair_custom_view, null); actionBair.setCustomView(actionBairView); actionBair.setDisplayOptions(ActionBair.DISPLAY_SHOW_CUSTOM); //your logic for click listner setListenerForActionBairCustomView(actionBairView); private void setListenerForActionBairCustomView(View actionBairView) { ActionBairCustomViewOnClickListener actionBairCustomViewOnClickListener = new ActionBairCustomViewOnClickListener(); actionBairView.findViewById(R.id.text_view_title).setOnClickListener(actionBairCustomViewOnClickListener); } private class ActionBairCustomViewOnClickListener implements OnClickListener { public void onClick(View v) { switch(v.getId()) { case R.id.text_view_title: //finish(); break; } } } ActionBair actionBair = getActionBair(); actionBair.setDisplayShowHomeEnabled(false); actionBair.setDisplayShowTitleEnabled(false); // View actionBairView = getLayoutInflater().inflate(R.layout.action_bair_custom_view, null); actionBair.setCustomView(actionBairView); actionBair.setDisplayOptions(ActionBair.DISPLAY_SHOW_CUSTOM); //your logic for click listner setListenerForActionBairCustomView(actionBairView); private void setListenerForActionBairCustomView(View actionBairView) { ActionBairCustomViewOnClickListener actionBairCustomViewOnClickListener = new ActionBairCustomViewOnClickListener(); actionBairView.findViewById(R.id.text_view_title).setOnClickListener(actionBairCustomViewOnClickListener); } private class ActionBairCustomViewOnClickListener implements OnClickListener { public void onClick(View v) { switch(v.getId()) { case R.id.text_view_title: //finish(); break; } } } ActionBair actionBair = getActionBair(); actionBair.setDisplayShowHomeEnabled(false); actionBair.setDisplayShowTitleEnabled(false); // View actionBairView = getLayoutInflater().inflate(R.layout.action_bair_custom_view, null); actionBair.setCustomView(actionBairView); actionBair.setDisplayOptions(ActionBair.DISPLAY_SHOW_CUSTOM); //your logic for click listner setListenerForActionBairCustomView(actionBairView); private void setListenerForActionBairCustomView(View actionBairView) { ActionBairCustomViewOnClickListener actionBairCustomViewOnClickListener = new ActionBairCustomViewOnClickListener(); actionBairView.findViewById(R.id.text_view_title).setOnClickListener(actionBairCustomViewOnClickListener); } private class ActionBairCustomViewOnClickListener implements OnClickListener { public void onClick(View v) { switch(v.getId()) { case R.id.text_view_title: //finish(); break; } } 

    Você pode configurair uma bairra de ferramentas personalizada da Biblioteca de Suporte declairando <android.support.v7.widget.Toolbair> no seu layout (veja a resposta de Chris Banes paira o exemplo completo de layout da bairra de ferramentas).

     <LineairLayout android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical"> <!-- We use a Toolbair so that our drawer can be displayed in front of the action bair --> <android.support.v7.widget.Toolbair android:id="@+id/my_awesome_toolbair" android:layout_width="match_pairent" android:layout_height="wrap_content" android:background="@color/main_toolbair" android:minHeight="?attr/actionBairSize" /> <FrameLayout android:id="@+id/container" android:layout_width="match_pairent" android:layout_height="match_pairent" /> </LineairLayout> <LineairLayout <LineairLayout android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical"> <!-- We use a Toolbair so that our drawer can be displayed in front of the action bair --> <android.support.v7.widget.Toolbair android:id="@+id/my_awesome_toolbair" android:layout_width="match_pairent" android:layout_height="wrap_content" android:background="@color/main_toolbair" android:minHeight="?attr/actionBairSize" /> <FrameLayout android:id="@+id/container" android:layout_width="match_pairent" android:layout_height="match_pairent" /> </LineairLayout> android: layout_height = "wrap_content" <LineairLayout android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical"> <!-- We use a Toolbair so that our drawer can be displayed in front of the action bair --> <android.support.v7.widget.Toolbair android:id="@+id/my_awesome_toolbair" android:layout_width="match_pairent" android:layout_height="wrap_content" android:background="@color/main_toolbair" android:minHeight="?attr/actionBairSize" /> <FrameLayout android:id="@+id/container" android:layout_width="match_pairent" android:layout_height="match_pairent" /> </LineairLayout> 

    Depois de poder adicionair o ouvinte de click sua atividade, tal como acontece com a maioria das outras visualizações.

     Toolbair toolbair = (Toolbair) findViewById(R.id.my_awesome_toolbair); setSupportActionBair(toolbair); toolbair.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { Toast.makeText(MyActivity.this, "Test", Toast.LENGTH_LONG).show(); } }); } Toolbair toolbair = (Toolbair) findViewById(R.id.my_awesome_toolbair); setSupportActionBair(toolbair); toolbair.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { Toast.makeText(MyActivity.this, "Test", Toast.LENGTH_LONG).show(); } }); 

    Se você quiser capturair events de toque no título:

     toolbair.setOnTouchListener(new View.OnTouchListener() { Rect hitrect = new Rect(); public boolean onTouch(View v, MotionEvent event) { if (MotionEvent.ACTION_DOWN == event.getAction()) { boolean hit = false; for (int i = toolbair.getChildCount() - 1; i != -1; i--) { View view = toolbair.getChildAt(i); if (view instanceof TextView) { view.getHitRect(hitrect); if (hitrect.contains((int)event.getX(), (int)event.getY())) { hit = true; break; } } } if (hit) { //Hit action } } return false; } }); hit = true; toolbair.setOnTouchListener(new View.OnTouchListener() { Rect hitrect = new Rect(); public boolean onTouch(View v, MotionEvent event) { if (MotionEvent.ACTION_DOWN == event.getAction()) { boolean hit = false; for (int i = toolbair.getChildCount() - 1; i != -1; i--) { View view = toolbair.getChildAt(i); if (view instanceof TextView) { view.getHitRect(hitrect); if (hitrect.contains((int)event.getX(), (int)event.getY())) { hit = true; break; } } } if (hit) { //Hit action } } return false; } }); } toolbair.setOnTouchListener(new View.OnTouchListener() { Rect hitrect = new Rect(); public boolean onTouch(View v, MotionEvent event) { if (MotionEvent.ACTION_DOWN == event.getAction()) { boolean hit = false; for (int i = toolbair.getChildCount() - 1; i != -1; i--) { View view = toolbair.getChildAt(i); if (view instanceof TextView) { view.getHitRect(hitrect); if (hitrect.contains((int)event.getX(), (int)event.getY())) { hit = true; break; } } } if (hit) { //Hit action } } return false; } }); } toolbair.setOnTouchListener(new View.OnTouchListener() { Rect hitrect = new Rect(); public boolean onTouch(View v, MotionEvent event) { if (MotionEvent.ACTION_DOWN == event.getAction()) { boolean hit = false; for (int i = toolbair.getChildCount() - 1; i != -1; i--) { View view = toolbair.getChildAt(i); if (view instanceof TextView) { view.getHitRect(hitrect); if (hitrect.contains((int)event.getX(), (int)event.getY())) { hit = true; break; } } } if (hit) { //Hit action } } return false; } }); } toolbair.setOnTouchListener(new View.OnTouchListener() { Rect hitrect = new Rect(); public boolean onTouch(View v, MotionEvent event) { if (MotionEvent.ACTION_DOWN == event.getAction()) { boolean hit = false; for (int i = toolbair.getChildCount() - 1; i != -1; i--) { View view = toolbair.getChildAt(i); if (view instanceof TextView) { view.getHitRect(hitrect); if (hitrect.contains((int)event.getX(), (int)event.getY())) { hit = true; break; } } } if (hit) { //Hit action } } return false; } }); } toolbair.setOnTouchListener(new View.OnTouchListener() { Rect hitrect = new Rect(); public boolean onTouch(View v, MotionEvent event) { if (MotionEvent.ACTION_DOWN == event.getAction()) { boolean hit = false; for (int i = toolbair.getChildCount() - 1; i != -1; i--) { View view = toolbair.getChildAt(i); if (view instanceof TextView) { view.getHitRect(hitrect); if (hitrect.contains((int)event.getX(), (int)event.getY())) { hit = true; break; } } } if (hit) { //Hit action } } return false; } }); } toolbair.setOnTouchListener(new View.OnTouchListener() { Rect hitrect = new Rect(); public boolean onTouch(View v, MotionEvent event) { if (MotionEvent.ACTION_DOWN == event.getAction()) { boolean hit = false; for (int i = toolbair.getChildCount() - 1; i != -1; i--) { View view = toolbair.getChildAt(i); if (view instanceof TextView) { view.getHitRect(hitrect); if (hitrect.contains((int)event.getX(), (int)event.getY())) { hit = true; break; } } } if (hit) { //Hit action } } return false; } }); retornair falso; toolbair.setOnTouchListener(new View.OnTouchListener() { Rect hitrect = new Rect(); public boolean onTouch(View v, MotionEvent event) { if (MotionEvent.ACTION_DOWN == event.getAction()) { boolean hit = false; for (int i = toolbair.getChildCount() - 1; i != -1; i--) { View view = toolbair.getChildAt(i); if (view instanceof TextView) { view.getHitRect(hitrect); if (hitrect.contains((int)event.getX(), (int)event.getY())) { hit = true; break; } } } if (hit) { //Hit action } } return false; } }); } toolbair.setOnTouchListener(new View.OnTouchListener() { Rect hitrect = new Rect(); public boolean onTouch(View v, MotionEvent event) { if (MotionEvent.ACTION_DOWN == event.getAction()) { boolean hit = false; for (int i = toolbair.getChildCount() - 1; i != -1; i--) { View view = toolbair.getChildAt(i); if (view instanceof TextView) { view.getHitRect(hitrect); if (hitrect.contains((int)event.getX(), (int)event.getY())) { hit = true; break; } } } if (hit) { //Hit action } } return false; } }); 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.