android之菜单及对话框

1 篇文章 0 订阅
1 篇文章 0 订阅
1 OptionsMenu(选项菜单)
1.1 OptionsMenu创建方式(菜单资源、Java程序)
重写onCreateOptionsMenu()方法
onCreateOptionsMenu()初始化菜单,其中menu参数就是即将要显示的Menu实例。返回true则显示该menu,false 则不显示;
1.2 两种方法添加menu
方法1:在xml中添加
在res/menu下新建my_menu.xml布局
在布局中添加item
android:showAsAction="never"一定要加上,不然只有平板可以显示,手机不一定
android:orderInCategory="100" 优先级 越小在越前面
使用:getMenuInflater().inflate(R.menu.my_menu, menu);

方法2:在onCreateOptionsMenu()方法中调用add方法添加
menu.add(1, 0, 0, "yi");
menu.add(1, 1, 1, "er");
menu.add(1, 2, 2, "san");
参数1--int groupId:分组id
参数2--int itemId:item的id
参数3--int order:排列顺序
参数4--CharSequence title:title(显示的文字)

1.3 菜单项点击事件(onMenuItemSelected()、onOptionsItemSelected())
注意:单击菜单项
如果是OptionsMenu就触发onOptionsItemSelected
如果是ContextMenu则触发onContextItemSelected


2 ContextMenu(上下文菜单)
定义:上下文菜单(长按控件时弹出)
上下文菜单与Options Menu最大的不同在于,Options Menu的拥有者是Activity,而上下文菜单的拥有者是Activity中的View。每个Activity有且只有一个Options Menu,它为整个Activity服务。而一个Activity往往有多个View,并不是每个View都有上下文菜单,这就需要我们显示地通过registerForContextMenu(View view)来指定。

2.1 上下文菜单绑定注册
TextView tv = (TextView) findViewById(R.id.tv);
/** 为textview 注册上下文菜单(当用户长按的时候 调用菜单显示) */
registerForContextMenu(tv);

2.2 ContextMenu创建方式
重写onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo)方法
注意:onCreateContextMenu会在用户每一次长按View时被调用,而且View必须已经注册了上下文菜单。
代码演示:
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.clear();
if (R.id.tv == v.getId()) {
/** 增加标题栏图标 */
menu.setHeaderIcon(R.drawable.ic_launcher);
/** 增加标题栏文字 */
menu.setHeaderTitle("上下文");
menu.add(1, 1, 1, "我是上下文菜单1111");
menu.add(1, 2, 2, "我是上下文菜单2222");
}
}

2.3上下文菜单项点击事件(onContextItemSelected)

@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), item.getTitle(), 0).show();
return super.onContextItemSelected(item);
}


3.PopupMenu(弹出式菜单)
定义:弹出式菜单,它会在指定组件上弹出PopupMenu,默认情况下,PopupMenu会显示在该组件的下方或上方。PopupMenu可增加多个菜单项,并可为菜单项增加子菜单。
PopupMenu的使用步骤:
1.PopupMenu的创建
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//1.new一个PopupMenu
PopupMenu popupMenu = new PopupMenu(MainActivity.this, btn);
}
});
2.将R.menu.popup_menu菜单资源加载到popup菜单中
getMenuInflater().inflate(R.menu.popup_menu,popup.getMenu());
3.显示弹出式菜单
popup.show();

代码演示:
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
//1.实例化PopupMenu
PopupMenu popupMenu = new PopupMenu(MainActivity.this, btn);
//2.把菜单的资源文件装进PopupMenu
getMenuInflater().inflate(R.menu.main, popupMenu.getMenu());
//3.调用show方法展示出来
popupMenu.show();
}
});
}

4 PopupWindow(弹出窗口)
PopupWindow实现自定义菜单
I.通过getLayoutInflater().inflate得到布局的view
View view = getLayoutInflater().inflate(R.layout.popupwindowlayout, null);
II.实例化一个PopupWindow,并给它宽高
PopupWindow popupWindow = new PopupWindow(view,
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
III.设置PopupWindow的属性
popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.axsa)); // 设置popupWindow背景
popupWindow.setOutsideTouchable(true);// 点击边上消失( 要先设置背景样式才有 效果
设置为true则点击pop外会消失,设置为false则不会消失。如果设置了popupWindow.setFocusable(true)则设置为false也会失效

popupwindow.getBackground().setAlpha(70);// 设置透明度
popupwindow.setFocusable(true);// 当前获取到焦点(里面的控件才能被点击)
popupwindow.setTouchable(true);// 可以被触摸
popupwindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);// 软键盘弹出
popupwindow.setAnimationStyle(android.R.style.DeviceDefault_ButtonBar_AlertDialog);// 设置动画

IV.设置PopupWindow显示的位置
方法1:popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
方法2:popupWindow.showAsDropDown(v, 10, 10);


代码实例:通过点击按钮实现弹出popuWindow并能点击其布局文件里的
控件的点击事件
//创建一个自定义的布局文件如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="宝宝最担心的是\n宝宝的宝宝是不是宝宝的宝宝"
/>
</LinearLayout>

//在主布局文件建一个button功能为实现弹出popupWindow
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button" />

</RelativeLayout>

//java代码实现
public class MainActivity extends Activity {
Button btn;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button) findViewById(R.id.btn);

btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//1.将布局文件转化为view对象
View view=LayoutInflater.from(MainActivity.this).inflate(R.layout.my_pop, null);
//2.创建popupWindow对象,参数为view对象和宽高
PopupWindow popupWindow=new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//3.设置属性
popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_launcher));
//4.显示
popupWindow.showAtLocation(btn,Gravity.CENTER , 0, 0);
//设置popupWindow里的控件点击事件
tv=(TextView) view.findViewById(R.id.tv);
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "宝宝心里苦~", Toast.LENGTH_SHORT).show();
}
});
}
});
}
}


---------------------------------------------------------------------------------


5.AlertDialog(提示对话框)
AlertDialog的使用
创建对话框
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle("标题") // 设置AlertDialog的title
.setIcon(R.drawable.ic_launcher)// 设置AlertDialog的title左边的图标
.setMessage("内容内容")// 设置AlertDialog的内容
.create();
显示对话框
alertDialog.show();
注意:AlertDialog的构造方法全部是Protected的,所以不能直接通过new一个AlertDialog来创建出一个AlertDialog。要创建一个AlertDialog,就要用到AlertDialog.Builder中的create()方法。

AlertDialog的点击事件

setPositiveButton :给对话框添加"Yes"按钮
setNegativeButton :对话框添加"No"按钮

!!!注意:在同一个activity中使用到Button的click 事件和dialog的click 事件,这两个事件导包是不一样的,先写哪一个,则可以导入哪个对应的包,后写的就没法再导入包,会一直报错。解决办法:dialog的click事件用 new DialogInterface.OnClickListener(){} 。


---------------------------------------------------------------------------------


6 ProgressDialog(进度对话框)
ProgressDialog的样式
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条的形式为圆形转动的进度条
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 设置水平进度条
ProgressDialog的创建(2种方式)
第一种是new Dialog
//1.实例化ProgressDialog
ProgressDialog progressDialog=new ProgressDialog(this);
//2.设置属性
progressDialog.setTitle("进度对话框");
progressDialog.setIcon(R.drawable.ic_launcher);
progressDialog.setMessage("显示的内容");
progressDialog.setButton(DialogInterface.BUTTON_POSITIVE,"确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//监听事件
}
});
progressDialog.setButton(DialogInterface.BUTTON_NEUTRAL,"中立",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//监听事件
}
});
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE,"取消",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//监听事件
}
});
//3.显示
progressDialog.show();


第二种种是调用Dialog的静态方法Dialog.show()
ProgressDialog dialog = ProgressDialog.show(this, "提示", "正在登陆中", true, true, cancelListener);
show(Context context, CharSequence title,
CharSequence message, boolean indeterminate,
boolean cancelable, OnCancelListener cancelListener)6个参数
//1.Context context 上下文对象
//2.CharSequence title 进度提示框显示的title
//3.CharSequence message 进度提示框显示的内容
//4.boolean indeterminate 是否是不明确的状态
//5.boolean cancelable 是否可以取消
//6.OnCancelListener cancelListener 设置取消的监听

注意:使用静态方式创建并显示,这种进度条只能是圆形条

--------------------------------------------------------------------------------------------------------

7 列表对话框
I.new一个Builder
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("列表框");
注意:
//不能设置其内容否则无法显示数据源里的内容
// aBuilder.setMessage("内容");

II.创建数据源
//列表框要展示的数据源
final String[] str = new String[] { "Item1", "Item2" };

III.设置items值及点击事件
//设置列表数据和item的点击事件
dialog.setItems(str, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, str[which], 0).show();
}
});

IV.展示列表对话框
dialog.show();

--------------------------------------------------------------------------------------------------------

8 自定义Dialog
1.写一个自定义的dialog的xml布局
2.获取自定义dialog布局文件的视图
// 获取dialog布局文件的视图
View view=LayoutInflater.from(this).inflate(R.layout.my_dialog, null);
//或者
View view = getLayoutInflater().inflate(R.layout.my_dialog, null);
3.new 一个Builder
Builder builder = new Builder(this);
4.把自定义的布局文件设置到buidler里面
builder.setView(view);
5.设置属性和监听事件等等
builder.setTitle("自定义dialog");
builder.setPositiveButton("升级", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

}
});
builder.setNegativeButton("暂不升级",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

}
});
6.展示出来
builder.show();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值