zoukankan      html  css  js  c++  java
  • Android之弹出菜单框【注冊上下文菜单】

    注冊上下文菜单:(长按弹出一个菜单)


    第一种创建方法(与长按事件结合):


    public class MainActivity extends Activity {
    private TextView username,password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    username = (TextView) findViewById(R.id.username);
    password = (TextView) findViewById(R.id.password);


    username.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {


    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
    ContextMenuInfo menuInfo) {
    // TODO Auto-generated method stub
    menu.add("file");
    menu.add("edit");
    menu.add("source");


    }
    });




    }
    @Override
    public boolean onContextItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    //当菜单上下文元素被选中时运行
    Toast.makeText(this, item.getTitle()+"onContextItemSelected", 1).show();
    return super.onContextItemSelected(item);
    }
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
    // TODO Auto-generated method stub
    //菜单元素被选中时运行
    Toast.makeText(this, item.getTitle()+"onMenuItemSelected", 1).show();
    return super.onMenuItemSelected(featureId, item);
    }
    }






    另外一种方法:
    (注冊上下文菜单)




    public class MainActivity extends Activity {
    private TextView username,password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    username = (TextView) findViewById(R.id.username);
    password = (TextView) findViewById(R.id.password);


    this.registerForContextMenu(username);//注冊菜单
    }
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
    ContextMenuInfo menuInfo) {
    // TODO Auto-generated method stub
    menu.add("file");
    menu.add("edit");
    super.onCreateContextMenu(menu, v, menuInfo);
    }
    @Override
    public boolean onContextItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    //当菜单上下文元素被选中时运行
    Toast.makeText(this, item.getTitle()+"onContextItemSelected", 1).show();
    return super.onContextItemSelected(item);
    }
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
    // TODO Auto-generated method stub
    //菜单元素被选中时运行
    Toast.makeText(this, item.getTitle()+"onMenuItemSelected", 1).show();
    return super.onMenuItemSelected(featureId, item);
    }
    }
  • 相关阅读:
    新手Android开发:onclicklistener到底怎么用?
    Myeclipse中web project 与java project区别
    <jsp:directive.page import=""/>的用法和解释
    怎样在myeclipse下,打开已有的项目
    有史以来最简单的三层实例(C#)
    show()跟showdialog()的区别
    献给和我一样的Java初学者——用UltraEdit代替“笨重”的IDE,实现轻巧编程!
    数据库连接错误——请求失败或服务器未及时响应
    说说二级C++
    十一张图让你轻松学会用VS打包
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/7204665.html
Copyright © 2011-2022 走看看