【问题标题】:How to hide the display of Toast message, on long press of ToolBar item(icon) android?如何在长按工具栏项目(图标)android时隐藏Toast消息的显示?
【发布时间】:2015-10-04 06:34:17
【问题描述】:

长按工具栏项后有什么方法可以隐藏吐司吗?

android ToolBar 图标或长按图像按钮显示吐司。

【问题讨论】:

  • 你想说什么?显示吐司,当点击工具栏时应该消失还是什么?
  • 仔细阅读问题。问题很明显。谢谢

标签: android android-toolbar android-menu


【解决方案1】:

好的。我的问题解决了。

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

【讨论】:

  • 为什么要使用Handler来设置OnLongClickListener?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-28
  • 2014-12-19
  • 2023-03-26
  • 1970-01-01
  • 2011-05-17
相关资源
最近更新 更多