【问题标题】:Android ActionBar options long click eventAndroid ActionBar 选项长按事件
【发布时间】:2012-03-04 21:39:46
【问题描述】:

Android 任何人都可以知道操作栏项目选项长按,我想在操作栏菜单选项上的 LongClick 上显示文本,就像长按操作栏长按的提示一样

【问题讨论】:

    标签: android click android-actionbar long-click


    【解决方案1】:

    您想捕捉长按操作栏上的菜单项吗?至于我,在找到 2,3 小时后,我找到了这个解决方案。这对我来说非常有用。

          @Override
         public boolean onCreateOptionsMenu(final Menu menu) {
    
    
    
        getMenuInflater().inflate(R.menu.menu, menu);
    
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                final View v = findViewById(R.id.action_settings);
    
                if (v != null) {
                    v.setOnLongClickListener(new CustomLongOnClickListener());
                }
            }
        });
    
        return true;
    }
    

    【讨论】:

    • 但第一次它工作正常。第二次当我们点击菜单项时它显示相同的空 toast 消息
    • @ YeeKhin 。非常感谢。
    • 但是findviewbyid总是返回null,为什么?
    【解决方案2】:

    对我来说,以下方法适用于较新的 Android 版本 - 我使用 Android 4.2 和 Android 5.0.1 对其进行了测试。

    我的想法是用自定义视图替换操作图标视图。在这里,我必须处理单击,我可以处理长单击。

    如果我希望外观与普通操作栏图标完全相同,则可以使用以下方法。

    首先,创建一个仅包含带有图标的 ImageButton 的布局。

    <?xml version="1.0" encoding="utf-8"?>
    <ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/myButton"
        style="?android:attr/actionButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@layout/text_view_initializing"
        android:src="@drawable/ic_action_plus" />
    

    然后把这个 ImageButton 放到 action bar 中,并给它附加监听器。

    MenuItem myItem = menu.findItem(R.id.my_action);
    myItem.setActionView(R.layout.my_image_button);
    myItem.getActionView().setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(final View v) {
            // here, I have to put the stuff that normally goes in onOptionItemSelected 
        }
    });
    myItem.getActionView().setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(final View v) {
            // here, I put the long click stuff
        }
    });
    

    重要提示:这仅在项目出现在操作栏中时才有效。因此,如果选项出现在菜单下拉菜单中,您将无法通过这种方式访问​​您想要在长按上执行的操作。

    【讨论】:

      【解决方案3】:

      user1206890,不需要监听长按事件。如果你想显示动作提示,在menu 中添加标题就足够了。检查 2.3 和 4.0。

      【讨论】:

        【解决方案4】:

        如果您通过android:actionLayout 创建自己的操作视图,欢迎您在自己的小部件上为长按事件设置侦听器。您无权访问不是您自己创建的操作栏中的小部件。

        【讨论】:

        【解决方案5】:

        我认为“findViewById”是最简单的查找方式。
        做吧

        View action_example = findViewById(R.id.action_example);
                if(action_example!=null)action_example.setOnLongClickListener(
                        new View.OnLongClickListener() {
                            @Override
                            public boolean onLongClick(View v) {
                                Toast.makeText(MainActivity.this, "action_example", Toast.LENGTH_SHORT).show();
                                return true;
                            }
                        }
                );
        

        【讨论】:

          【解决方案6】:

          感谢@YeeKhin,这是对我有用的函数代码 将“main”更改为您的菜单名称,将“action_refresh”更改为您的操作名称,将“Activity”更改为您的活动名称

          public boolean onCreateOptionsMenu(Menu menu) {
          
                  getMenuInflater().inflate(R.menu.main, menu);
          
                  new Handler().post(new Runnable() {
                      @Override
                      public void run() {
                          final View v = findViewById(R.id.action_refresh);
          
                          if (v != null) {
                              v.setOnLongClickListener(new View.OnLongClickListener() {
                                  @Override
                                  public boolean onLongClick(View v) {
                                      Toast.makeText(Activity.this,"Long Press!!",Toast.LENGTH_LONG).show();
                                      return false;
                                  }
                              });
                          }
                      }
                  });
          
                  return true;
              }
          

          【讨论】:

            猜你喜欢
            • 2011-04-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多