不同按钮不同上下文菜单?

时间:2015-01-02 12:19:27

标签: android android-intent contextmenu

我有四个按钮,在那个四个按钮上我想调用不同的上下文菜单,这是第一个按钮,如何实现相同类型的代码到其他三个。我还尝试添加另一个按钮,并在同一覆盖方法中包含其他上下文菜单,也检查了网站,所以请告诉我该怎么做。

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.semtosub);
    sub1 = (Button) findViewById(R.id.subject1);
    registerForContextMenu(sub1);   

}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
    // TODO Auto-generated method stub
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflate = getMenuInflater();
    inflate.inflate(R.menu.contexts1,menu);

}
@Override
public boolean onContextItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId()){
    case R.id.chapter1:
        Intent c1 = new Intent(Subject.this,Sub1C1.class);
           startActivity(c1);
           break;

    case R.id.chapter2:
        Intent c2 = new Intent(Subject.this,Sub1C2.class);
           startActivity(c2);
           break;
    case R.id.chapter3:
        Intent c3 = new Intent(Subject.this,Sub1C3.class);
           startActivity(c3);
           break;   
    case R.id.chapter4:
        Intent c4 = new Intent(Subject.this,Sub1C4.class);
           startActivity(c4);
           break;       
    case R.id.chapter5:
        Intent c5 = new Intent(Subject.this,Sub1C5.class);
           startActivity(c5);
           break; 
    }

    return super.onContextItemSelected(item);

}

}

1 个答案:

答案 0 :(得分:2)

  

点击一个按钮运行两个或多个活动?

使用Context.startActivities在Array中获取多个意图并逐个启动Activity:

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent i_one = new Intent (Semester.this,Subject.class); 
        Intent j_two = new Intent (Semester.this,Subject2.class);
        startActivities(new Intent[]{i_one,j_two}); 

    }