开发者

Android - text dialog popup from an option menu

A simple question. I want a static dialog message with ONLY text to popup when pressed a button in an options menu. This is my menu code:

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
       switch (item.getItemId()) {
           case R.id.icon:
                Intent intent = new Intent(this, Main.class);
                startActivity(intent);
           case R.id.help:
    开发者_Go百科           //popup window code here
       }
       return true;
   }
}

How do I do it the easiest way?


@Override
public boolean onOptionsItemSelected(MenuItem item) {
   switch (item.getItemId()) {
       case R.id.icon:
            Intent intent = new Intent(this, Main.class);
            startActivity(intent);
       case R.id.help:
           //popup window code here
Toast.makeText(this, "This is the Toast message", Toast.LENGTH_LONG).show();

   }
   return true;
}
}

or u can i use the dialog boxes

@Override
public boolean onOptionsItemSelected(MenuItem item) {
   switch (item.getItemId()) {
       case R.id.icon:
            Intent intent = new Intent(this, Main.class);
            startActivity(intent);
       case R.id.help:
           //popup window code here
 AlertDialog.Builder alertbox = new AlertDialog.Builder(this);

        // set the message to display
        alertbox.setMessage("This is the alertbox!");

        // add a neutral button to the alert box and assign a click listener
        alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {

            // click listener on the alert box
            public void onClick(DialogInterface arg0, int arg1) {
                // the button was clicked

            }
        });

        // show it
        alertbox.show();

   }
   return true;
}

}


You can create simple dialog

static final int DIALOG_MESSAGE_ID= 0;

protected Dialog onCreateDialog(int id) {
  switch(id) {
  case DIALOG_MESSAGE_ID:
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Your message ")//your message
       });
    return builder.create();
    break;
  }
  return null;
}


//in your code
case R.id.help:
  showDialog(DIALOG_MESSAGE_ID);

more at http://developer.android.com/guide/topics/ui/dialogs.html


AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setMessage("Blah Blah...");
dialog.show();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜