Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android OnOptions ItemSelected方法是否可以有3项?_Android - Fatal编程技术网

Android OnOptions ItemSelected方法是否可以有3项?

Android OnOptions ItemSelected方法是否可以有3项?,android,Android,我正在使用Android Studio,我想知道是否有可能在“onOptionsItemSelected”方法中包含多于2个项目?如果是,怎么做?提前谢谢 顺便说一句,我只是尝试添加另一个案例,这给了一个问题 case R.id.DropDB:{ AlertDialog.Builder builder4 = new AlertDialog.Builder(MainActivity.this); LayoutInflater inflater = LayoutInf

我正在使用Android Studio,我想知道是否有可能在
“onOptionsItemSelected”
方法中包含多于2个项目?如果是,怎么做?提前谢谢

顺便说一句,我只是尝试添加另一个案例,这给了一个问题

case R.id.DropDB:{
        AlertDialog.Builder builder4 = new AlertDialog.Builder(MainActivity.this);
        LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
        builder4.setTitle("DeleteMassage")
        builder4.setPositiveButton("set", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int id) {
                    msgsDB.DeleteTable();

                }
                });
            return true;

是的,你想放多少就放多少。 你必须做几件事。首先,您必须创建一个xml,将其放入文件夹:res/menu,例如,您可以将其命名为myMenu.xml

在这个xml中,您可以创建包含所有项目的菜单

myMenu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/action_1" android:title="@string/action_1" android:icon="@drawable/action_1" android:showAsAction="ifRoom" />
    <item android:id="@+id/action_2" android:title="@string/action_2" android:orderInCategory="100" android:showAsAction="never" />
    <item android:id="@+id/action_3" android:title="@string/action_3" android:orderInCategory="100" android:showAsAction="never" />
    <item android:id="@+id/action_4" android:title="@string/action_4" android:orderInCategory="100" android:showAsAction="never" />
</menu> 
然后,您可以实施所有需要的案例,以便采取行动:

@Override
public boolean onOptionsItemSelected (MenuItem item) {

    switch(item.getItemId())
    {
        case R.id.action_1:
            //You code for the action1
            return true;

        case R.id.action_2:
            //You code for the action2
            return true;

        case R.id.action_3:
            //You code for the action3
            return true;

        case R.id.action_4:
            //You code for the action4
            return true;
    }

    return super.onOptionsItemSelected(item);
}
@Override
public boolean onOptionsItemSelected (MenuItem item) {

    switch(item.getItemId())
    {
        case R.id.action_1:
            //You code for the action1
            return true;

        case R.id.action_2:
            //You code for the action2
            return true;

        case R.id.action_3:
            //You code for the action3
            return true;

        case R.id.action_4:
            //You code for the action4
            return true;
    }

    return super.onOptionsItemSelected(item);
}