• Back键和Menu键程序退出弹窗以及按钮退出程序弹窗的实现


    MainActivity.java

    package com.loaderman.backmenudemo;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.view.KeyEvent;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.LinearLayout;
    import android.widget.PopupWindow;
    import android.widget.RelativeLayout;
    
    public class MainActivity extends AppCompatActivity {
        private boolean menu_display = false;
        private PopupWindow    menuWindow;
        private LayoutInflater inflater;
        private View         layout;
        private LinearLayout mClose;
        private LinearLayout mCloseBtn;
        public static MainActivity instance = null;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            instance=this;
        }
         public void exit(View view ){
             Intent intent = new Intent();
             intent.setClass(MainActivity.this,ExitFromSettings.class);
             startActivity(intent);
         }
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {  //获取 back键
                System.out.println(2);
                if (menu_display) {
                    System.out.println(3);//如果 Menu已经打开 ,先关闭Menu
                    menuWindow.dismiss();
                    menu_display = false;
                } else {
                    System.out.println(4);
                    Intent intent = new Intent();
                    intent.setClass(this, Exit.class);
                    startActivity(intent);
                }
            } else if (keyCode == KeyEvent.KEYCODE_MENU) {   //获取 Menu键
                if(!menu_display){
                    //获取LayoutInflater实例
                    inflater =     (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
                    //这里的main布局是在inflate中加入的哦,以前都是直接this.setContentView()的吧?呵呵
                    //该方法返回的是一个View的对象,是布局中的根
                    layout = inflater.inflate(R.layout.main_menu, null);
    
                    //下面我们要考虑了,我怎样将我的layout加入到PopupWindow中呢???很简单
                    menuWindow = new PopupWindow(layout, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); //后两个参数是width和height
                    //menuWindow.showAsDropDown(layout); //设置弹出效果
                    //menuWindow.showAsDropDown(null, 0, layout.getHeight());
                    menuWindow.showAtLocation(this.findViewById(R.id.activity_main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
                    //如何获取我们main中的控件呢?也很简单
                    mClose = (LinearLayout)layout.findViewById(R.id.menu_close);
                    mCloseBtn = (LinearLayout)layout.findViewById(R.id.menu_close_btn);
    
    
                    //下面对每一个Layout进行单击事件的注册吧。。。
                    //比如单击某个MenuItem的时候,他的背景色改变
                    //事先准备好一些背景图片或者颜色
                    mCloseBtn.setOnClickListener (new View.OnClickListener() {
                        @Override
                        public void onClick(View arg0) {
                            //Toast.makeText(Main.this, "退出", Toast.LENGTH_LONG).show();
                            Intent intent = new Intent();
                            intent.setClass(MainActivity.this,Exit.class);
                            startActivity(intent);
                            menuWindow.dismiss(); //响应点击事件之后关闭Menu
                        }
                    });
                    menu_display = true;
                }else{
                    //如果当前已经为显示状态,则隐藏起来
                    menuWindow.dismiss();
                    menu_display = false;
                }
    
                return false;
            }
            return false;
        }
    }
    

     ExitFromSettings.java

    package com.loaderman.backmenudemo;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.LinearLayout;
    import android.widget.Toast;
    
    public class ExitFromSettings extends Activity {
        private LinearLayout layout;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_exit_from_settings);
            layout=(LinearLayout)findViewById(R.id.exit_layout2);
            layout.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), "提示:点击窗口外部关闭窗口!",
                            Toast.LENGTH_SHORT).show();
                }
            });
        }
        @Override
        public boolean onTouchEvent(MotionEvent event){
            finish();
            return true;
        }
    
        public void exitbutton1(View v) {
            this.finish();
        }
        public void exitbutton0(View v) {
            this.finish();
            MainActivity.instance.finish();//关闭Main 这个Activity
        }
    }
    

     Exit.java

    package com.loaderman.backmenudemo;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.LinearLayout;
    import android.widget.Toast;
    
    public class Exit extends Activity {
        private LinearLayout layout;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.exit_dialog);
            layout=(LinearLayout)findViewById(R.id.exit_layout);
            layout.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(), "提示:点击窗口外部关闭窗口!",
                            Toast.LENGTH_SHORT).show();
                }
            });
        }
        @Override
        public boolean onTouchEvent(MotionEvent event){
            finish();
            return true;
        }
    
        public void exitbutton1(View v) {
            this.finish();
        }
        public void exitbutton0(View v) {
            this.finish();
            MainActivity.instance.finish();//关闭Main 这个Activity
        }
    }
    

    在res/anim文件下创建滑入退出动画

    push_bottom_in.xml

    <?xml version="1.0" encoding="utf-8"?>
    <!-- 上下滑入式 -->
    <set xmlns:android="http://schemas.android.com/apk/res/android" >
        <translate
            android:duration="200"
            android:fromYDelta="0"
            android:toYDelta="50%p" />
    </set>
    

     push_bottom_out.xml

    <?xml version="1.0" encoding="utf-8"?>
    <!-- 上下滑入式 -->
    <set xmlns:android="http://schemas.android.com/apk/res/android" >
        <translate
            android:duration="200"
            android:fromYDelta="0"
            android:toYDelta="50%p" />
    </set>
    

     activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        tools:context="com.loaderman.backmenudemo.MainActivity">
    
        <Button
            android:onClick="exit"
            android:textSize="18sp"
            android:textColor="#fff"
            android:textStyle="bold"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/btn_style_alert_dialog_special"
            android:text="退出程序"/>
    </RelativeLayout>
    

     activity_exit_from_settings.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/activity_exit_from_settings"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       >
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center_horizontal"
            >
    
            <LinearLayout
                android:id="@+id/exit_layout2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:layout_gravity="bottom"
                android:orientation="vertical"
                android:background="@drawable/exit_dialog_bg" >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="4dp"
                    android:gravity="center"
                    android:textColor="#fff"
                    android:textSize="16sp"
                    android:text="退出后不会删除任何历史数据,下次
    登录依然可以使用本帐号" />
                <Button
                    android:id="@+id/exitBtn0"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="退出登录"
                    android:textSize="18sp"
                    android:textColor="#fff"
                    android:layout_marginTop="15dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:background="@drawable/btn_style_alert_dialog_special"
                    android:gravity="center"
                    android:onClick="exitbutton0"
                    />
                <Button
                    android:id="@+id/exitBtn1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_marginBottom="10dp"
                    android:text="取消"
                    android:textSize="18sp"
                    android:textColor="#fff"
                    android:background="@drawable/btn_style_alert_dialog_cancel"
                    android:gravity="center"
                    android:onClick="exitbutton1"
                    />
            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>
    

     exit_dialog.xml

    <?xml version="1.0" encoding="UTF-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/exit_layout"
            android:layout_width="280dp"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:orientation="vertical"
            android:background="@drawable/confirm_dialog_bg2" >
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="4dp"
                android:padding="5dp"
                android:textColor="#333"
                android:textSize="20sp"
                android:text="退出微信" />
            
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#333"
                android:layout_marginTop="1dp"
                android:padding="10dp"
                android:textSize="16sp"
                android:gravity="center_horizontal"
                android:text="退出后,你将收不到新的消
    息.确定要退出?" /> 
            
    
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"           
                android:layout_marginTop="33dp"
                android:layout_marginBottom="8dp"
                 >
                
                <Button
                  android:id="@+id/exitBtn0"
                  android:layout_width="110dp"
                  android:layout_height="wrap_content"
                  android:text="是"
                  android:textSize="16sp"
                  android:textColor="#fff"
                  android:background="@drawable/btn_style_green"
                  android:gravity="center"
               android:onClick="exitbutton0"
                  />
                
                <Button
                  android:id="@+id/exitBtn1"
                  android:layout_width="110dp"
                  android:layout_height="wrap_content"
                  android:layout_marginLeft="10dp"
                  android:text="否"
                  android:textSize="16sp"
                  android:textColor="#333"
                  android:background="@drawable/btn_style_white"
                  android:gravity="center"
               android:onClick="exitbutton1"
                  />
            </LinearLayout>
        </LinearLayout>
    

     main_menu.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        
        >
        
        <LinearLayout 
            android:id="@+id/menu_close" 
           android:layout_width="fill_parent" 
           android:layout_height="wrap_content" 
           android:orientation="vertical" 
           android:gravity="center" 
           android:background="@drawable/menu_bg"
             >
             
            <LinearLayout 
               android:id="@+id/menu_close_btn" 
              android:layout_width="fill_parent" 
              android:layout_height="wrap_content" 
              android:orientation="vertical" 
              android:gravity="center" 
              android:background="@drawable/menu_btn_bg"
              >
           
           <ImageView 
               android:layout_width="wrap_content" 
              android:layout_height="wrap_content" 
              android:src="@drawable/btn_close" />
                    
           <TextView 
               android:layout_width="wrap_content" 
              android:layout_height="wrap_content" 
              android:text="退出" 
              android:textColor="#eee"/>
          </LinearLayout>             
        </LinearLayout>
    </LinearLayout>
    

     给退出的Activity设置Style

    <style name="MyDialogStyle">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:backgroundDimEnabled">true</item>
    </style>
    <style name="MyDialogStyleBottom" parent="android:Theme.Dialog" >
        <item name="android:windowAnimationStyle">@style/AnimBottom</item>
        <item name="android:windowFrame">@null</item><!--边框-->
        <item name="android:windowIsFloating">true</item><!--是否浮现在activity之上-->
        <item name="android:windowIsTranslucent">true</item><!--半透明-->
        <item name="android:windowNoTitle">true</item><!--无标题-->
        <item name="android:windowBackground">@android:color/transparent</item><!--背景透明-->
        <item name="android:backgroundDimEnabled">true</item><!--模糊-->
    </style>
    <style name="AnimBottom" parent="@android:style/Animation">
        <item name="android:windowEnterAnimation">@anim/push_bottom_in</item>
        <item name="android:windowExitAnimation">@anim/push_bottom_out</item>
    </style>
    

     在清单文件中设置:

    <activity android:name=".Exit"
      android:theme="@style/MyDialogStyle"/>
    <activity android:name=".ExitFromSettings"
      android:theme="@style/MyDialogStyleBottom"/>
    

     效果图:

     

     

  • 相关阅读:
    dremio 21.1 分布式存储配置demo
    cube.js 对于headlessbi 核心组件的定义
    dremio 21.1 分布式存储单机测试的一种方法
    devsec devops+security 工具集
    gooddatacn 试用
    typescript njs 开发参考资料
    NSURLRequestUseProtocolCachePolicy decision tree for HTTP and HTTPS
    Web前端技术发展简史
    ArcGIS Pro SDK 分级专题
    Pro 模型中参数类型
  • 原文地址:https://www.cnblogs.com/loaderman/p/6485711.html
Copyright © 2020-2023  润新知