Android ActionBar options long click event

主宰稳场 提交于 2019-11-28 01:26:09

问题


Android Can anyone know about Actionbar item options long click , I want to show text on LongClick on actionbar menu option like a hint on long press of actionBar long press


回答1:


Do you want to capture long press on menu item on action bar? As for me, after finding 2,3 hour, I found this solution. This is perfectly work for me.

      @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;
}



回答2:


user1206890, you do not need listen long click event. If you want show action hint, will be sufficient set title in menu add. Checked on 2.3 and 4.0.




回答3:


For me, the following approach works fine for newer Android versions - I tested it with Android 4.2 and Android 5.0.1.

The idea is that I replace the action icon view by a custom view. Here, I have to handle the single click, and I can handle the long click.

If I want the appearance to be exactly like normal action bar icons, the following works.

First, create a layout containing just an ImageButton with your icon.

<?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" />

Then put this ImageButton into the action bar and attach listeners to it.

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
    }
});

Important remark: this will only work if the item appears in the action bar. So, whatever you want to do on long click will not be accessible in this way if the option appears in the menu dropdown.




回答4:


If you create your own action view via android:actionLayout, you are welcome to set up listeners on your own widgets for long-click events. You do not have access to widgets in the action bar that you do not create yourself.




回答5:


I think "findViewById" is the easiest way to find.
Just do

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;
                    }
                }
        );


来源:https://stackoverflow.com/questions/9261329/android-actionbar-options-long-click-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!