【问题标题】:Back button in Toolbar not working工具栏中的后退按钮不起作用
【发布时间】:2015-11-14 20:18:09
【问题描述】:

我有一个简单的 Activity,它是 ActionBarActivity 类的孩子。在我设置 OnCreate 的方法中支持工具栏。为此我覆盖了 OnOptionsItemSelected,所以当我按下后退按钮时执行了一些操作

代码如下:

    [Activity (Label = "SimplyActivity", Theme="@style/MyTheme")]           
        public class SimplyActivity : ActionBarActivity
        {
            private Toolbar toolbar;

            // ... OnCreate method
            this.toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
            SetSupportActionBar (this.toolbar);
            SupportActionBar.SetDisplayHomeAsUpEnabled (true);
            SupportActionBar.SetHomeButtonEnabled (true);

            public override bool OnOptionsItemSelected (IMenuItem item)
            {
                if (item.TitleFormatted == null) this.OnBackPressed ();
                return base.OnOptionsItemSelected (item);
            }

不幸的是,只要工具栏正确显示,这不再是按键时的任何反应。我会在其他活动(使用片段)中添加,一切正常。

请帮帮我

【问题讨论】:

  • "这不再是按键被按下时的任何反应。"键 - 硬件按钮“返回”还是什么?

标签: c# android xamarin toolbar


【解决方案1】:

它应该像这样工作

public override bool OnOptionsItemSelected(IMenuItem item)
{
    //Back button pressed -> toggle event
    if (item.ItemId == Android.Resource.Id.Home)
        this.OnBackPressed(); 

    return base.OnOptionsItemSelected(item);
}

【讨论】:

    【解决方案2】:

    试试这样的:

    只需在您的 OnCreate 方法中添加以下行:

      SupportActionBar.SetDisplayHomeAsUpEnabled(true);
    

    然后重写OnOptionsItemSelected 方法,如下所示。

    public override bool OnOptionsItemSelected(IMenuItem item)
    {
        if (item.ItemId != Android.Resource.Id.Home)
            return base.OnOptionsItemSelected(item);
        Finish();
        return true;
    }
    

    【讨论】:

      【解决方案3】:

      试试 this.toolbar.setNavigationOnClickListener 并根据你的需要让它处理 onBackPressed 或 popBackstack。

      【讨论】:

      • 我以前试过这个,但它不起作用。事件未执行。
      【解决方案4】:

      尝试这样做:

      [Activity (Label = "SimplyActivity", Theme="@style/MyTheme")]           
              public class SimplyActivity : ActionBarActivity
              {
                  private Toolbar toolbar;
      // ...
      // OnCreate method
      this.toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
      SetSupportActionBar (this.toolbar);
                  SupportActionBar.SetDisplayHomeAsUpEnabled (true);
                  SupportActionBar.SetHomeButtonEnabled (true);
      //dont forget this
                  this.toolbar.SyncState();
      this.toolbar += ClickedMenu;
      
      public override bool OnOptionsItemSelected (IMenuItem item)
              {
                  this.OnOptionsItemSelected(item);
                  return base.OnOptionsItemSelected (item);
              }
      
       public void ClickedMenu(object sender,SupportToolbar.MenuItemClickEventArgs e)
              {
                  switch (e.Item.ItemId)
                  {     //your TitleFormatted ID
                      case Resource.Id.action_edit:
                          //do stuff here
                      this.OnBackPressed ();
                          break;
                  }
              }
       protected override void OnPostCreate(Bundle savedInstanceState)
              {
                  base.OnPostCreate(savedInstanceState);
                  this.toolbar.SyncState();     
              }
      

      【讨论】:

        【解决方案5】:
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
          if(item.getItemId() == android.R.id.home) {
          // do something
          }
          return super.onOptionsItemSelected(item);
        }
        

        【讨论】:

          【解决方案6】:

          这个问题真的很奇怪。使用操作栏的布局具有RelativeLayout。改成LinearLayout属性android:gravity="vertical"后,一切正常。

          感谢大家的帮助

          【讨论】:

            【解决方案7】:

            我建议您使用此代码 sn-p 在工具栏中使用自定义后退按钮:

            第一步: 将图标返回按钮添加到可绘制文件夹中。

            第二步: 像这样将工具栏添加到您的 AppBarLayout 中:

            <android.support.design.widget.AppBarLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:theme="@style/AppTheme.AppBarOverlay">
            
                    <android.support.v7.widget.Toolbar
                        android:id="@+id/chart_toolbar"
                        android:layout_width="match_parent"
                        android:layout_height="?attr/actionBarSize"
                        android:background="?attr/colorPrimary"
                        app:popupTheme="@style/AppTheme.PopupOverlay" />
            
            </android.support.design.widget.AppBarLayout>
            

            第三步: 在你的 onCreate 中找到这样的视图:

            Toolbar toolbar = (Toolbar) findViewById(R.id.chart_toolbar);
            

            第四步: 将支持操作栏添加到您的工具栏:

            setSupportActionBar(toolbar);
            
            if (getSupportActionBar() != null) {
                    getSupportActionBar().setDisplayShowHomeEnabled(true);
                    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            }
            

            第五步: 将愿望图标添加到您的按钮:

            toolbar.setNavigationIcon(ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_chevron_left));
            

            第六步: 为您的后退按钮设置一个点击监听器:

            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            NavUtils.navigateUpFromSameTask(Chart.this);
                        }
            });
            

            最后覆盖 oncreateoptionsmenu 和 onoptionsitemselected 方法:

            @Override
                public boolean onCreateOptionsMenu(Menu menu) {
                    getMenuInflater().inflate(R.menu.my_menu, menu);
                    return true;
                }
            
                @Override
                public boolean onOptionsItemSelected(MenuItem item) {
            
                    return true;
                }
            

            【讨论】:

              猜你喜欢
              • 2013-03-28
              • 1970-01-01
              • 2014-07-24
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多