android对话框的使用
来源:技术人生 责任编辑:栏目编辑 发表时间:2013-07-01 19:48 点击:次
package com.test;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.TextView;
import android.widget.Toast;
public class layoutMain extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem menuItem = menu.add(0, 0, 0, "菜单");
menuItem.setIcon(R.drawable.icon01);
menuItem.setAlphabeticShortcut('a');
menu.add(0, 1, 1, "退出").setIcon(R.drawable.icon02);
menu.add(0, 2, 2, "菜单3").setIcon(R.drawable.icon03);
menu.add(0, 4, 4, "菜单7").setIcon(R.drawable.icon04);
menu.add(0, 3, 3, "菜单8").setIcon(R.drawable.icon05);
return super.onCreateOptionsMenu(menu);
}
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.TextView;
import android.widget.Toast;
public class layoutMain extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem menuItem = menu.add(0, 0, 0, "菜单");
menuItem.setIcon(R.drawable.icon01);
menuItem.setAlphabeticShortcut('a');
menu.add(0, 1, 1, "退出").setIcon(R.drawable.icon02);
menu.add(0, 2, 2, "菜单3").setIcon(R.drawable.icon03);
menu.add(0, 4, 4, "菜单7").setIcon(R.drawable.icon04);
menu.add(0, 3, 3, "菜单8").setIcon(R.drawable.icon05);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Toast.makeText(layoutMain.this, "被单击的菜单项为:" + String.valueOf(item.getItemId()), Toast.LENGTH_SHORT).show();
switch(item.getItemId()){
case 1:
openOptionsDialog();
break;
}
return super.onOptionsItemSelected(item);
}
private void openOptionsDialog(){ //这种编码方式很好的
new AlertDialog.Builder(this)
.setTitle("标题")
.setMessage("真的要离开吗?")
.setNegativeButton("离开", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setPositiveButton("不离开", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
}
// Toast.makeText(layoutMain.this, "被单击的菜单项为:" + String.valueOf(item.getItemId()), Toast.LENGTH_SHORT).show();
switch(item.getItemId()){
case 1:
openOptionsDialog();
break;
}
return super.onOptionsItemSelected(item);
}
private void openOptionsDialog(){ //这种编码方式很好的
new AlertDialog.Builder(this)
.setTitle("标题")
.setMessage("真的要离开吗?")
.setNegativeButton("离开", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setPositiveButton("不离开", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
}
本文出自 “技术人生” 博客,请务必保留此出处http://zhaohaiyang.blog.51cto.com/2056753/487039
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>