码迷,mamicode.com
首页 > 其他好文 > 详细

menu&&Toas简单使用

时间:2016-03-26 12:11:00      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

package com.example.second;


import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements OnClickListener {
private Button b1,b2,b3,b4,b5,b6,div;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button) findViewById(R.id.one);
b2=(Button) findViewById(R.id.two);
b3=(Button) findViewById(R.id.three);
b4=(Button) findViewById(R.id.four);
b5=(Button) findViewById(R.id.five);
b6=(Button) findViewById(R.id.six);
div=(Button) findViewById(R.id.div);
registerForContextMenu(div);
registerForContextMenu(b6);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.four)
{
Toast.makeText(MainActivity.this, "you click four!", Toast.LENGTH_LONG).show();
}
}
});
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.one:
{
Toast toast = Toast.makeText(getApplicationContext(), "自定义位置的Toast", Toast.LENGTH_LONG);
//toast.setGravity参数:位置,该位置水平偏移,相对该位置垂直偏移
toast.setGravity(Gravity.CENTER,500,50);
toast.show();
break;
}
case R.id.two:
{
Toast toast=Toast.makeText(getApplicationContext(), "自定义位置的Toast", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, -500, -50);
toast.show();
break;
}
default:
break;
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
//menu.add参数:分组,id,位置(默认为0),显示内容;
menu.add(0, Menu.FIRST+1, 0, "0");
menu.add(0,Menu.FIRST+2,1,"1");
menu.add(0, Menu.FIRST+3, 2, "2");
menu.add(1, Menu.FIRST+4, 3, "03");
menu.add(1, Menu.FIRST+5, 4, "04");
menu.add(1, Menu.FIRST+6, 5, "05");
menu.add(2, Menu.FIRST+7, 6, "06");
menu.add(2, Menu.FIRST+8, 7, "07");
menu.add(2, Menu.FIRST+9, 8, "08");
return true;
}

public void onCreateContextMenu(android.view.ContextMenu menu, View v, android.view.ContextMenu.ContextMenuInfo menuInfo)
{
menu.add(0, Menu.FIRST+1, 0, "0");
SubMenu subMenu=menu.addSubMenu(Menu.NONE, Menu.FIRST+10, 1, "上下文菜单子菜单1");
subMenu.addSubMenu(Menu.NONE, Menu.FIRST+101, 2, "201");
subMenu.addSubMenu(Menu.NONE, Menu.FIRST+102, 3, "202");
subMenu.addSubMenu(Menu.NONE,Menu.FIRST+103,1,"203");
menu.add(0, Menu.FIRST+2, 2, "1");
menu.addSubMenu(Menu.NONE,Menu.FIRST+20,3,"上下文菜单2");
menu.add(0, Menu.FIRST+3, 4, "2");

};

public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()){
case Menu.FIRST+1:{
Toast.makeText(this, "you click div0", Toast.LENGTH_LONG).show();
break;
}
case Menu.FIRST+2:{
Toast.makeText(this, "you click div1", Toast.LENGTH_LONG).show();
break;
}
case Menu.FIRST+3:{
Toast.makeText(this, "you click div 4", Toast.LENGTH_LONG).show();
}
case Menu.FIRST+10:{
Toast.makeText(this, "you click 10", Toast.LENGTH_LONG).show();
break;
}
case Menu.FIRST+101:{
Toast.makeText(this, "you click 101", Toast.LENGTH_LONG).show();
break;
}
case Menu.FIRST+102:{
Toast.makeText(this, "you click 102", Toast.LENGTH_LONG).show();
break;
}
case Menu.FIRST+103:{
Toast.makeText(this, "you click 103", Toast.LENGTH_LONG).show();
break;
}
}
return true;};

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case Menu.FIRST+1:
//Toast.makeText参数:上下文控件,内容,显示时间(_LONG比_SHORT显示时间长)
Toast.makeText(this,"1 was click!",Toast.LENGTH_LONG).show();
break;
case Menu.FIRST+2:
Toast.makeText(this,"2 was click!",Toast.LENGTH_LONG).show();
break;
case Menu.FIRST+3:
Toast.makeText(this,"3 was click!",Toast.LENGTH_LONG).show();
break;
}
return super.onOptionsItemSelected(item);
}
}

 

menu&&Toas简单使用

标签:

原文地址:http://www.cnblogs.com/linduan/p/5322575.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!