Android customizes the Menu icon of Toolbar and pops up a dialog box after clicking it

We often encounter such a requirement: there is an icon in the upper right corner of the interface, and a dialog box will pop up after clicking it, such as reporting, selecting additional functions, etc., many apps have to implement it. An implementation is now summarized.

First add the menu, monitor the click event of the menu, and pop up a dialog when clicked:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_news,menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.id_menu_report:
            AlertDialog.Builder builder= new AlertDialog.Builder(NewsInfoActivity. this ) ;
             builder.setIcon(android.R.drawable. ic_dialog_info ) ;
             builder.setTitle( "Please select the reason for your report" ) ;
             final String []itemsId= new String []{ "false content" , "piracy infringement" , "vulgar content" , "dangerous speech" , "feudal superstition" } ;
             final boolean []checkedItems= new boolean []{ false,false,false,false,false } ; //The true here is the default number of people has been selected
            builder.setMultiChoiceItems(itemsId, checkedItems,new DialogInterface.OnMultiChoiceClickListener() {

                @Override
public void onClick(DialogInterface dialog, int which, boolean ischeck) {
                    checkedItems[which]=ischeck;
}                                
            }) ;
 //Set an OK button
 builder.setPositiveButton( "OK" , null ) ;
 builder.setNegativeButton( "Cancel" , new DialogInterface.OnClickListener() {
                 @Override
 public void onClick (DialogInterface dialog , int which) {                                                    
                    dialog.dismiss();
                }
            });
            builder.setCancelable(false);
            final AlertDialog dialog = builder.create();
            dialog.show();
            dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {

                @Override
public void onClick(View v) {                
                    Log.d("checkedItems", "checkedItems: "+checkedItems);
                    String text="";
                    boolean hasSelected=false;
                    for(int i=0;i<itemsId.length;i++)
                    {
                        text+=checkedItems[i]?itemsId[i]+",":"";
                        if (checkedItems[i]){
                            hasSelected = checkedItems[i];
                            break;
                        }
                    }
                    if (hasSelected) {
                        T. showToast ( "Successful report! Thanks for your feedback!" ) ;
 dialog .dismiss() ;
 }
                     else {                                            
                        T. showToast ( "No report reason has been selected!" ) ;
                         return;
                     }
                }
            });
            break;
    }
    return super.onOptionsItemSelected(item);
}

具体toolbar如何配置,如何加入flowOver menu,详见我的另一篇文章Android Toolbar使用技巧之添加toolbar和配置menu

然后需要特别配置menu的显示方式:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    tools:context="com.reality.realityapp.ui.activity.NewsInfoActivity">
    <item
        android:id="@+id/id_menu_report"
        android:orderInCategory="100"
        app:showAsAction="always"
        android:icon="@drawable/report"
        android:title="举报"
        />
</menu>

将showAsAction设为always,即菜单项item将直接代替menu的图标(默认是三个点)显示在toolbar右侧,然后item的图标便代替了menu的默认图标,这是一种自定义Menu图标的行之有效的方法。当然网上还有通过style更改menu图标的方式,不过经过尝试,本文使用的方法更加方便有效,不需考虑图片尺寸问题(因为实质上是修改item的图片,放到menu图标中去,而menu图标尺寸没有改变),而修改style加入图片是更改menu图标属性,如果图片实际尺寸不好则会出现显示问题。


这里还有一个tip:

如果按照传统方式给dialog设置按钮(比如上面的“取消”按钮),无论点击“确定”(PositiveButton)还是“取消”(NegativeButton),对话框都会消失,这其实是不合理的,因为当某些条件不满足时不能退出对话框。所以解决方式如下:

在创建AlertDialog时setPositiveButton方法的OnClickListener参数需传入null,然后让dialog show出来以后,再通过getButton(AlertDialog.BUTTON_POSITIVE)方法重新得到确定按钮,重设点击事件,这时如果不手动去调dialog.dismiss(),对话框就不会消失了。例子就是本文的“确定”按钮的监听设置。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325442613&siteId=291194637