标签:
package com.example.second;
import java.util.zip.Inflater;
import android.support.v7.app.ActionBarActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements OnClickListener {
private Button b1,b2,b3,b4,b5,b6,div;
private Button b7,b8,b9,b0;
@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);
b7=(Button) findViewById(R.id.sev);
b8=(Button) findViewById(R.id.eig);
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();
}
}
});
b5.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final String[] items={"郭嘉-智计百出","贾诩-完杀","沮授-凝眸知阵"};
AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this)
.setTitle("选择人物:")
.setItems(items,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), items[which], Toast.LENGTH_LONG).show();
}
});
dialog.setView(new EditText(getApplicationContext()));
dialog.show();
}
});
b7.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final String[] items={"智计百出-郭嘉","东吴大帝-孙权","凝眸知阵-沮授","偷渡阴平-邓艾"};
AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this)
.setTitle("选择你喜欢的英雄:")
.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), items[which], Toast.LENGTH_LONG).show();
dialog.cancel();
}
});
dialog.show();
}
});
b8.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
LayoutInflater flater=getLayoutInflater();
View layout=flater.inflate(R.layout.dialog, (ViewGroup)findViewById(R.id.dialog));
new AlertDialog.Builder(MainActivity.this).setTitle("自定义布局").setView(layout)
.setPositiveButton("yes", null)
.setNegativeButton("no", null)
.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;
}
case R.id.three:
{
AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this)
.setMessage("you create a Dialoge,congraturation!")
.setPositiveButton("YES", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "you click yes!", Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("no", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "you click no!", Toast.LENGTH_LONG).show();
dialog.dismiss();
}});
dialog.show();
}
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);
}
}
标签:
原文地址:http://www.cnblogs.com/linduan/p/5322623.html