OnContextItemSelected() 菜单应用

Posted liumin-txgt

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OnContextItemSelected() 菜单应用相关的知识,希望对你有一定的参考价值。

public class MainActivity extends Activity {
private boolean flag=true;
private Button btn01,btn02;
private String[] sizes=new String[]{"8","12","16","20","24","28"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btn01=(Button)findViewById(R.id.btn01);
btn02=(Button)findViewById(R.id.btn02);
registerForContextMenu(btn01);
registerForContextMenu(btn02);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuItem start=menu.findItem(R.id.start);
MenuItem stop=menu.findItem(R.id.stop);
start.setEnabled(flag);
stop.setEnabled(!flag);
flag=!flag;
return super.onPrepareOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub

switch (item.getItemId()) {
case R.id.exit:
finish();
break;
case R.id.start:
case R.id.stop:
invalidateOptionsMenu();
default:
Toast.makeText(MainActivity.this, item.getTitle()+"被选中了", Toast.LENGTH_LONG).show();
break;
}
return super.onOptionsItemSelected(item);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
switch (v.getId()) {
case R.id.btn01:
menu.setHeaderTitle("文件操作");
menu.add(0, Menu.FIRST+1, 0, "重命名");
SubMenu color=menu.addSubMenu(0, Menu.FIRST+2, 0, "设置文本颜色");
color.add(0,Menu.FIRST+21,0,"红色");
color.add(0,Menu.FIRST+22,0,"蓝色");
color.add(0,Menu.FIRST+23,0,"绿色");
color.add(0,Menu.FIRST+24,0,"紫色");
break;
case R.id.btn02:
menu.setHeaderTitle("文件操作");
menu.add(0, Menu.FIRST+3, 0, "设置文本字体大小");
SubMenu bgcolor=menu.addSubMenu(0, Menu.FIRST+4, 0, "设置文本背景");
bgcolor.add(0,Menu.FIRST+41,0,"红色");
bgcolor.add(0,Menu.FIRST+42,0,"蓝色");
bgcolor.add(0,Menu.FIRST+43,0,"绿色");
bgcolor.add(0,Menu.FIRST+44,0,"紫色");
break;
default:
break;
}
}

@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case Menu.FIRST+1:
Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("请输入新的名称");
builder.setIcon(R.drawable.a1);
final EditText edit=new EditText(MainActivity.this);
builder.setView(edit);
builder.setPositiveButton("确定 ", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
btn01.setText(edit.getText().toString());
}
});
builder.create().show();

break;
case Menu.FIRST+21:
btn01.setTextColor(Color.RED);
break;
case Menu.FIRST+22:
btn01.setTextColor(Color.GREEN);
break;
case Menu.FIRST+23:
btn01.setTextColor(Color.BLUE);
break;
case Menu.FIRST+24:
btn01.setTextColor(Color.YELLOW);
break;
case Menu.FIRST+41:
btn02.setBackgroundColor(Color.RED);
break;
case Menu.FIRST+42:
btn02.setBackgroundColor(Color.GREEN);
break;
case Menu.FIRST+43:
btn02.setBackgroundColor(Color.BLUE);
break;
case Menu.FIRST+44:
btn02.setBackgroundColor(Color.YELLOW);
break;

case Menu.FIRST+3:
Builder sizeBuilder=new AlertDialog.Builder(MainActivity.this);
sizeBuilder.setTitle("请选择字体大小");
sizeBuilder.setIcon(R.drawable.a2);
sizeBuilder.setSingleChoiceItems(sizes, 1, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
btn02.setTextSize(Integer.parseInt(sizes[which]));
}
});
sizeBuilder.setPositiveButton("确定 ", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//btn01.setText(edit.getText().toString());
}
});
sizeBuilder.create().show();

default:
break;
}
return super.onContextItemSelected(item);
}

}

以上是关于OnContextItemSelected() 菜单应用的主要内容,如果未能解决你的问题,请参考以下文章

如何在多片段活动中处理 onContextItemSelected?

从 onContextItemSelected 显示的 DialogFragment 无法在 onPause/onResume 中继续存在

鸟叔linux私房菜在哪里

形容菜非常好吃的四字词语

鸟哥的linux私房菜 哪些内容要看

求鸟哥的linux私房菜全部视频教程 百度网盘资源