标签:round led prot cte reg item ttext title button
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);
}
}
标签:round led prot cte reg item ttext title button
原文地址:https://www.cnblogs.com/liumin-txgt/p/12785715.html