第二天 Menu菜单及PopupWindow窗口

一、菜单

1、系统菜单OptionsMenu

注意:一个Activity只有一个系统菜单

流程

1.首先需要在res下面创建一个menu文件夹,并新建一个xml文件作为OptionsMenu的布局文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
    //想要app属性需要引入xmlns:app="http://schemas.android.com/apk/res-auto"
    <item android:id="@+id/much" android:title="更多" app:showAsAction="always"/>
    <item android:id="@+id/all" android:title="全选"/>
    <item android:id="@+id/px" android:title="排序"/>
    <item android:id="@+id/yinc" android:title="隐藏"/>
</menu>

2.Activity重写onCreateOptionsMenu加载资源文件

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	//将自己写的菜单布局放在系统菜单下
        getMenuInflater().inflate(R.menu.menu1,menu);
        return super.onCreateOptionsMenu(menu);
    }

3.Activity重写onOptionsItemSelected加设置事件监听

@Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId()){
            case R.id.all:
                Toast.makeText(this, ""+item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.px:
                Toast.makeText(this, ""+item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.yinc:
                Toast.makeText(this, ""+item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.much:
                Toast.makeText(this, ""+item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
        }

        return super.onOptionsItemSelected(item);
    }

2、上下文菜单ContextMenu

流程

1.在res下面创建一个menu文件夹,并新建一个xml文件作为ContextMenu的布局文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/all" android:title="全选"/>
    <item android:id="@+id/px" android:title="排序"/>
    <item android:id="@+id/yinc" android:title="隐藏"/>
</menu>

2.Activity重写onCreateConextMenu加载资源文件

@Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        //将自己写的菜单布局放在系统菜单下
        getMenuInflater().inflate(R.menu.menu2,menu);
    }

3.Activity重写onConextItemSelected设置事件监听

@Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.all:
                Toast.makeText(this, ""+item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.px:
                Toast.makeText(this, ""+item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.yinc:
                Toast.makeText(this, ""+item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onContextItemSelected(item);
    }

4.为控件添加长按属性并将菜单绑定到这个控件上:registerForContextMenu(控件)

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
		//为控件设置长按并绑定菜单
        registerForContextMenu(contentmenu);

    }

3、弹出菜单

流程

1.在res下面创建一个menu文件夹,并新建一个xml文件作为PoupMenu的布局文件。

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/red" android:title="红色" />
    <item android:id="@+id/green" android:title="绿色" />
    <item android:id="@+id/yellow" android:title="黄色" />

</menu>

2.把PopupMenu相关逻辑封装到showPopupMenu()方法中,包含PopupMenu的实例化、布局设置、显示、添加MenuItem 的点击监听及响应等

private void showPopupMenu() {
        ppmenu = (Button) findViewById(R.id.ppmenu);
		//为控件设置点击监听
        ppmenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            	//将自己写菜单布局加入弹出菜单对象
                PopupMenu menu = new PopupMenu(MainActivity.this,ppmenu);
                //加载布局
                menu.inflate(R.menu.menu3);
                //为控件设置监听
                menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) {
                        switch (menuItem.getItemId()){
                            case R.id.red:
                                ppmenu.setText(menuItem.getTitle());
                            break;

                            case R.id.green:
                                ppmenu.setText(menuItem.getTitle());
                                break;

                            case R.id.yellow:
                                ppmenu.setText(menuItem.getTitle());
                                break;
                        }
                        return false;
                    }
                });
                	//展示
					menu.show();
            }
        });
    }

3.直接调用showPopupMenu()方法

二、窗口

1、简单弹出窗口PopupWindow

流程

1.编写自己的窗口布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="match_parent"
   android:layout_height="match_parent">

   <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/one"
       android:text="第一"
       />
   <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/two"
       android:text="第二"
       />

</LinearLayout>

2.初始化控件,为控件添加点击监听
3.创建PopupWindow对象,为PopupWindow对象添加自己的窗口布局
4.添加布局,设置高,设置宽,缺一不可
5.设置外部点击取消,设置窗口弹出位置

 private void popwindow() {
        //初始化控件
        ppwindow = (Button) findViewById(R.id.ppwindow);
        //添加点击监听
        ppwindow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //创建PopupWindow对象
                PopupWindow popupWindow = new PopupWindow(MainActivity.this);
                //寻找自己的菜单布局
                View view1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.window01, null);
                //以下3步缺一不可
                //加入PopupWindow对象
                popupWindow.setContentView(view1);
                //为PopupWindow设置高,可以匹配父容器,可以包含
                popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
                //为PopupWindow设置宽
                popupWindow.setWidth(300);
                //设置外部点击取消
                popupWindow.setOutsideTouchable(true);
                //设置窗口显示位置,参数1:位于那个控件下,参数2:X轴偏移量,参数3:Y轴偏移量
                popupWindow.showAsDropDown(ppwindow,0,0);
            }
        });
    }

三、补充自定义对话框

流程

1.编写对话框XML布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="#11ffffff">
   <LinearLayout
       android:layout_width="300dp"
       android:layout_height="wrap_content"
       android:layout_centerInParent="true"
       android:orientation="vertical">
       <TextView
           android:id="@+id/title"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_gravity="center"
           android:layout_marginTop="10dp"
           android:text="警告"

           android:textColor="#38ADFF"
           android:textSize="16sp"/>
       <TextView
           android:id="@+id/message"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_margin="20dp"
           android:layout_gravity="center"
           android:text="保护视力,少玩手机"/>
       <View
           android:layout_width="match_parent"
           android:layout_height="1px"
           android:layout_marginTop="15dp"
           android:background="#E4E4E4"/>
       <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="40dp"
           android:orientation="horizontal">
           <Button
               android:id="@+id/no"
               android:layout_width="0dp"
               android:layout_height="match_parent"
               android:layout_weight="1"
               android:layout_marginLeft="10dp"
               android:background="@null"
               android:gravity="center"
               android:lines="1"
               android:text="取消"
               android:textColor="#7D7D7D"
               android:textSize="16sp"/>
           <View
               android:layout_width="1px"
               android:layout_height="match_parent"
               android:background="#E4E4E4"/>
           <Button
               android:id="@+id/yes"
               android:layout_width="0dp"
               android:layout_height="match_parent"
               android:layout_weight="1"
               android:layout_marginRight="10dp"
               android:background="@null"
               android:gravity="center"
               android:lines="1"
               android:text="确定"
               android:textColor="#38ADFF"
               android:textSize="16sp"/>
       </LinearLayout>
   </LinearLayout>
</RelativeLayout>

2.自定义类继承Dialog

package com.example.day0221;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MyDiglog1 extends Dialog {

   private TextView title;//标题
   private TextView message;//内容
   private Button no;//No按钮
   private Button yes;//Yes按钮

   private String titleStr;//标题值
   private String messageStr;//内容值
   private String noStr;//No按钮值
   private String yesStr;//Yes按钮值

   //接口对象
   private onYesOnclickListener onYesOnclickListener;
   private onNoOnclickListener onNoOnclickListener;

   //构造
   public MyDiglog1(Context context) {
       super(context);
   }

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.diglog1);
       //初始化控件
       initview();
       //初始化数据
       initdata();
       //设置监听
       inite();


   }

   private void inite() {
       //Yes按钮设置监听
       yes.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               //实现接口方法
               onYesOnclickListener.onYesOnclick();
           }
       });
       //设置取消按钮被点击后,向外界提供监听
       no.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               onNoOnclickListener.onNoClick();
           }
       });
   }

   private void initdata() {
       title.setText(titleStr);
       message.setText(messageStr);
       no.setText(noStr);
       yes.setText(yesStr);
   }

   private void initview() {
       title = (TextView) findViewById(R.id.title);
       message = (TextView) findViewById(R.id.message);
       no = (Button) findViewById(R.id.no);
       yes = (Button) findViewById(R.id.yes);
   }


   //设置标题文字
   public void settitleStr(String titleStr){
       this.titleStr = titleStr;
   }
   //设置内容文字
   public void setMessageStr(String messageStr){
       this.messageStr = messageStr;
   }
   //设置No按钮值
   public void setNoStr(String noStr){
       this.noStr = noStr;
   }
   //设置Yes按钮值
   public void setYesStr(String yesStr){
       this.yesStr = yesStr;
   }

   public interface onNoOnclickListener {
       public void onNoClick();
   }

   public interface onYesOnclickListener {
       public void onYesOnclick();
   }
   
   public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {
       this.onNoOnclickListener = onNoOnclickListener;
   }

   public void setYesOnclickListener(String str, onYesOnclickListener yesOnclickListener) {
       this.onYesOnclickListener = yesOnclickListener;
   }

}

发布了2 篇原创文章 · 获赞 1 · 访问量 64

猜你喜欢

转载自blog.csdn.net/qq_45272980/article/details/104435332