2

菜单项未显示下面的对话框是我的代码,当用户单击菜单项时,它应该将他带到添加该代码的登录页面。

public boolean onOptionsItemSelected(MenuItem item, int id) {
    switch (item.getItemId()) {
        case R.id.Login:
             startActivity(new Intent(this, Login.class));
             return true;
        case R.id.About:
         startActivity(new Intent(this, About.class));
            return true;
        case R.id.Post_Ads:
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Are you sure you want to exit?")
                   .setCancelable(false);
            AlertDialog alert = builder.create();
                    alert.show();
                    startActivity(new Intent(this, Login.class));
             return true;


          }
    return super.onOptionsItemSelected(item);
}
4

2 回答 2

0

试试这个,它应该工作:

public boolean onOptionsItemSelected(MenuItem item) {
            // TODO Auto-generated method stub
            switch (item.getItemId()) {
            case R.id.Login:
                Toast.makeText(getApplicationContext(), "Fired 1", Toast.LENGTH_SHORT).show();
                 startActivity(new Intent(this, Login.class));
                 return true;

            case R.id.About:
    Toast.makeText(getApplicationContext(), "Fired 2", Toast.LENGTH_SHORT).show();
             startActivity(new Intent(this, About.class));

                return true;
                        case R.id.Post_Ads:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("Are you sure you want to exit?")
                       .setCancelable(false);
                AlertDialog alert = builder.create();
                        alert.show();
                        startActivity(new Intent(this, Login.class));

                 return true;


              }
        return super.onOptionsItemSelected(item);

        }

topublic boolean onOptionsItemSelected(MenuItem item, int id)中没有调用方法Activity classhandle option menu's.

处理此类事件的方法是 onOptionsItemSelected(MenuItem).

要显示菜单项,您也必须使用以下方法,检查您是否也实现了此方法,

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

和一个小的修正,

您已经实现了以下 Alertdialog 方法,

AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Are you sure you want to exit?")
                   .setCancelable(false);
            AlertDialog alert = builder.create();
                    alert.show();
                    startActivity(new Intent(this, Login.class));

您在这里没有提供任何确定或取消按钮,而且您还设置了 setCancellable(false)。所以用户不能在这里选择任何选项。

所以我建议你在这里给确定和取消按钮,并在 onClick() 方法中做你想做的事情。像下面的例子:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Are you sure you want to exit?")
                   .setCancelable(false);
            AlertDialog alert = builder.create();
            alert.setButton("Ok", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    // Do call some activity or close the app. Do what you wish to;

                }
            });

alert.setButton2("Cancel", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    // Do stay in the current activity or something else. Do what you wish to;

                }
            });
                    alert.show();

             return true;

希望,它可能会有所帮助。

于 2012-11-02T11:40:32.367 回答
0

试试这个

AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Navigate to login").setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                            //do things
                           startActivity(new Intent(this, Login.class));
                       }
                   });

            AlertDialog alert = builder.create();
            alert.show();
于 2012-11-02T11:49:01.940 回答