如何在工具栏中的每个菜单上实现onclick事件我使用了TapTargetview库

时间:2018-01-17 11:16:39

标签: android material-design

 final TapTargetSequence sequence=new TapTargetSequence(this)
                .targets(
                        TapTarget.forToolbarMenuItem(toolbar,R.id.action_search,"Search","Search here!!!")
                                .outerCircleColor(R.color.red)      // Specify a color for the outer circle
                                .outerCircleAlpha(0.96f)            // Specify the alpha amount for the outer circle
                                .targetCircleColor(R.color.white)   // Specify a color for the target circle
                                .titleTextSize(24)                  // Specify the size (in sp) of the title text
                                .titleTextColor(R.color.white)      // Specify the color of the title text
                                .descriptionTextSize(28)            // Specify the size (in sp) of the description text
                                .descriptionTextColor(R.color.white)  // Specify the color of the description text
                                .textColor(R.color.white)            // Specify a color for both the title and description text
                                .textTypeface(Typeface.SANS_SERIF)  // Specify a typeface for the text
                                .dimColor(R.color.black)            // If set, will dim behind the view with 30% opacity of the given color
                                .drawShadow(true)                   // Whether to draw a drop shadow or not
                                .cancelable(false)                  // Whether tapping outside the outer circle dismisses the view
                                .tintTarget(true)                   // Whether to tint the target view's color
                                .transparentTarget(false)           // Specify whether the target is transparent (displays the content underneath)
                                .targetRadius(40)
                                .id(1),

                        TapTarget.forToolbarMenuItem(toolbar,R.id.action_cart,"Cart","Cart here!!!")
                                .id(2),
                        TapTarget.forToolbarNavigationIcon(toolbar,"Navigation","Navigation here!!!")
                                .id(3)                      
                );

        sequence.start();

1 个答案:

答案 0 :(得分:0)

为什么要使用库?它在Android框架中很简单。只需在xml中创建一个菜单并在代码中充气即可。 下面是my_toolbar_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/add_menu"
          android:icon="@drawable/ic_add_icon"
          android:title="@string/add_text"
          android:showAsAction="ifRoom"/>
    <item android:id="@+id/help"
          android:icon="@drawable/ic_help"
          android:title="@string/help" />
</menu>

现在膨胀菜单:

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.my_toolbar_menu, menu);
        return true;
    }

现在处理点击事件,如下所示:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.add_menu:
        doSomething();
        return true;
    case R.id.help:
        getHelp();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}