继续获得无法解析符号' logout'错误

发布于 2025-01-25 09:01:27 字数 1534 浏览 2 评论 0原文

在开关语句中关键字登录时,请继续出现错误,因为无法解决符号注销。 想要将图标放在动作栏上,但不知道如何设置动作栏。请帮助我设置Action Bar也可以解决此问题。 谢谢

@Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        //handle presses on the action bar items
        switch (item.getItemId())
        {
            case R.id.logout:
            {
                AlertDialog.Builder builder=new AlertDialog.Builder(this);
                builder.setMessage("Do you want to logout?").setCancelable(false)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener()
                        {
                            public void onClick(DialogInterface dialog, int id) {
                                startActivity(new Intent(Attendence.this,ChoosePanel.class));
                            } //public void onClick(DialogInterface dialog, int id)
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                //action for 'NO' button
                                dialog.cancel();
                            } //public void onClick(DialogInterface dialog, int id)
                        });
                //creatin dialog box
                AlertDialog alert=builder.create();
                //setting the title manually
                alert.setTitle("LOGOUT");
                alert.show();
                return true;
            }       

Keep getting error with keyword logout in switch statement as as cannot resolve symbol logout.
want to put icon on action bar but don't know how to setup action bar. help me setup action bar resolve this issue as well please.
thanks

@Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        //handle presses on the action bar items
        switch (item.getItemId())
        {
            case R.id.logout:
            {
                AlertDialog.Builder builder=new AlertDialog.Builder(this);
                builder.setMessage("Do you want to logout?").setCancelable(false)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener()
                        {
                            public void onClick(DialogInterface dialog, int id) {
                                startActivity(new Intent(Attendence.this,ChoosePanel.class));
                            } //public void onClick(DialogInterface dialog, int id)
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                //action for 'NO' button
                                dialog.cancel();
                            } //public void onClick(DialogInterface dialog, int id)
                        });
                //creatin dialog box
                AlertDialog alert=builder.create();
                //setting the title manually
                alert.setTitle("LOGOUT");
                alert.show();
                return true;
            }       

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

臻嫒无言 2025-02-01 09:01:27

要创建一个操作栏,您必须创建资源类型“菜单”的资源文件。

在该文件中,您可以根据需要拥有尽可能多的选项,并且可以将其定义在栏上或OU上“三个获得”菜单。这是一个示例:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/logout"
    android:icon="@drawable/ic_logout"
    android:title="@string/sign_out"
    app:showAsAction="ifRoom"/>
<item
    android:id="@+id/settings"
    android:icon="@drawable/abc_vector_test"
    android:title="@string/settings" />
</menu>

注意android:id定义中的 @+id。还请注意,如果您添加App:showasaction =“ ifroom”该选项将显示在栏上而不是下拉菜单上。

然后,在您的活动中,添加onCreateOptionsMenu(菜单菜单)方法。类似:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.game_menu, menu);

    return super.onCreateOptionsMenu(menu);
}

然后,您在问题中显示的功能将捕获菜单点击事件。

To create an action bar you have to create a resource file of resource type "Menu".

In that file you can have as much options as you want and can define them to be shown on the bar or ou the "three got" menu. Here is an example:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/logout"
    android:icon="@drawable/ic_logout"
    android:title="@string/sign_out"
    app:showAsAction="ifRoom"/>
<item
    android:id="@+id/settings"
    android:icon="@drawable/abc_vector_test"
    android:title="@string/settings" />
</menu>

Note the @+id in the android:id definition. Note as well that if you add app:showAsAction="ifRoom" the option will be shown on the bar instead of the dropdown menu.

Then, in your activity, add the onCreateOptionsMenu(Menu menu) method. Something like:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.game_menu, menu);

    return super.onCreateOptionsMenu(menu);
}

Then the function that you showed in your question will catch the menu click events.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文