如何隐藏Toast消息的显示,长按ToolBar项目(图标)android?

时间:2015-10-04 06:34:17

标签: android android-toolbar android-menu

长按ToolBar项目后有没有办法隐藏吐司?

长按的android ToolBar图标或图像按钮显示祝酒。

1 个答案:

答案 0 :(得分:5)

确定。我的问题解决了。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    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 View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        return false;
                    }
                });
            }
        }
    });

    new Handler().post(new Runnable() {
        @Override
        public void run() {
            final View v = findViewById(R.id.action_search);

            if (v != null) {
                v.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        return false;
                    }
                });
            }
        }
    });

    return true;
}

您可以点击Toolbar

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    //noinspection SimplifiableIfStatement
    if (id == R.id.action_search) {
        Toast.makeText(context,"action_search",Toast.LENGTH_SHORT).show();
        return true;
    }else if (id == R.id.action_settings) {
        Toast.makeText(context,"action_settings",Toast.LENGTH_SHORT).show();
        return true;
    }

    return super.onOptionsItemSelected(item);
}