单击android菜单项后打开弹出窗口

时间:2014-05-23 18:18:52

标签: android menu dialog popup

点击菜单项后,我想打开弹出窗口或对话框。我编写了代码,但对话框没有打开。菜单有两个项目" about"和"帮助"。有没有其他方式显示弹出消息。?

这是我的代码:

package com.easy.convert;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class AndroidMenusActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

// Initiating Menu XML file (menu.xml)
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.menu, menu);
    return true;
}

/**
 * Event Handling for Individual menu item selected
 * Identify single menu item by it's id
 * */
@Override
public boolean onOptionsItemSelected(MenuItem item)
 {

switch (item.getItemId())
{
case R.id.menu_help:
    Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.aboutus);
    dialog.setTitle("Help");
    dialog.setCancelable(true);
    dialog.show();
    return true;

case R.id.menu_about:
    Dialog dialog1 = new Dialog(this);
    dialog1.setContentView(R.layout.aboutus);
    dialog1.setTitle("About");
    dialog1.setCancelable(true);
    dialog1.show();
    return true;

default:
    return super.onOptionsItemSelected(item);
}
} 
}

0 个答案:

没有答案