package com.example.test1;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

////////////////////////////
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
//import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
//import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;

import android.widget.Toast;
////////////////////////////

public class PopupWindow2 extends ActionBarActivity {
   
    ////////////////////////////
     private Button button;
     private Button button1;
     private Button button2;
     private Button button3;
     private Button button4;
     private View popupWindowView;
     private PopupWindow popupWindow;
     private LayoutInflater inflater;
    ////////////////////////////
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_popup_window2);
  
        ////////////////////////////
  button=(Button) findViewById(R.id.button_m);    
  button.setOnClickListener(new ButtonOnClickListener());
        ////////////////////////////
 }
 
 
 
 ////////////////////////////
  private class ButtonOnClickListener implements OnClickListener{
         public void onClick(View v) {
             inflater=(LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
             popupWindowView=inflater.inflate(R.layout.activity_popup_window_sub, null);
          
             popupWindow=new PopupWindow(popupWindowView, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,true);
             //必须要有这句否则弹出popupWindow后监听不到Back键
             popupWindow.setBackgroundDrawable(new BitmapDrawable());
             popupWindow.showAtLocation(findViewById(R.id.button_m), Gravity.NO_GRAVITY, 0, 0);
             //让popupWindow获得焦点
             popupWindow.setFocusable(true);
             //设置动画
             //popupWindow.setAnimationStyle(R.style.popupWindowAnimation);
             popupWindow.update();
                          
             //popupWindow中按钮的处理
             button1=(Button) popupWindowView.findViewById(R.id.button1);
             button2=(Button) popupWindowView.findViewById(R.id.button2);
             button3=(Button) popupWindowView.findViewById(R.id.button3);
             button4=(Button) popupWindowView.findViewById(R.id.button4);
             button1.setOnClickListener(new ButtonsOnClickListener());
             button2.setOnClickListener(new ButtonsOnClickListener());
             button3.setOnClickListener(new ButtonsOnClickListener());
             button4.setOnClickListener(new ButtonsOnClickListener());
              
         }
          
     }
      
     private class ButtonsOnClickListener implements OnClickListener {
         public void onClick(View v) {
             switch (v.getId()) {
             case R.id.button1:
                 //System.out.println("点击了按钮1");
              Toast.makeText(getApplicationContext(), "提示:点击了按钮1,点击窗口外部关闭窗口!",
                 Toast.LENGTH_SHORT).show();
                 break;
             case R.id.button2:
                  System.out.println("点击了按钮2");
                 break;
             case R.id.button3:
                  System.out.println("点击了按钮3");
                 break;
             case R.id.button4:
                  //System.out.println("点击了按钮4");
              popupWindow.dismiss(); // 关闭窗口
                 break;
  
             default:
                 break;
             }
  
         }
  
     }
      
     //监听Back事件
     @Override
     public boolean onKeyDown(int keyCode, KeyEvent event) {
         if (keyCode==KeyEvent.KEYCODE_BACK) {
             if (popupWindow!=null&&popupWindow.isShowing()) {
                 popupWindow.dismiss();      
             } else {
                 //MainActivity.this.finish();
              PopupWindow2.this.finish();
             }
         }
         return super.onKeyDown(keyCode, event);
     }

////////////////////////////

@Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.popup_window2, menu);
  return true;
 }

@Override
 public boolean onOptionsItemSelected(MenuItem item) {
  // Handle action bar item clicks here. The action bar will
  // automatically handle clicks on the Home/Up button, so long
  // as you specify a parent activity in AndroidManifest.xml.
  int id = item.getItemId();
  if (id == R.id.action_settings) {
   return true;
  }
  return super.onOptionsItemSelected(item);
 }
}

//////////////////////////////////////////////////////////////

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.test1.PopupWindow2" >

<Button
            android:id="@+id/button_m"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="30dp"
            android:text="test" />

</RelativeLayout>

//////////////////////////////////////////////////////////////

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.test1.PopupWindow_sub" >
     
    <LinearLayout
        android:id="@+id/linerLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical"
    >
         
        <Button 
            android:id="@+id/button1"
            android:layout_width="200dip"
            android:layout_height="wrap_content"
            android:text="Button1"
            android:textSize="20sp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="20dip"
        />
         <Button 
            android:id="@+id/button2"
            android:layout_width="200dip"
            android:layout_height="wrap_content"
            android:text="Button2"
            android:textSize="20sp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="20dip"
        />
          <Button 
            android:id="@+id/button3"
            android:layout_width="200dip"
            android:layout_height="wrap_content"
            android:text="Button3"
            android:textSize="20sp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="20dip"
        />
           <Button 
            android:id="@+id/button4"
            android:layout_width="200dip"
            android:layout_height="wrap_content"
            android:text="Button4"
            android:textSize="20sp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="20dip"
        />
         
         
    </LinearLayout>
     
 
</RelativeLayout>

//////////////////////////////////////////////////////////////

20150604_Andriod 窗体PopupWindow的更多相关文章

  1. 20150604_Andriod 窗体PopupWindow动画

    参考地址: http://www.open-open.com/lib/view/open1378720752084.html http://www.jcodecraeer.com/a/anzhuoka ...

  2. Android popupwindow 失去焦点或者点击空白区域时消失的解决方法

    先来看下Android API 的这个Methods: public void setOutsideTouchable (boolean touchable) Controls whether the ...

  3. popUpWindow 动画无法超出窗体的解决方案

    popupWindow 做动画时,当要求有一个放大动画时,动画无法超出窗体,给人的感觉是只有内容在放大,窗体不动. 这是由于窗口大小固定的原因,解决方案是加大popUpwindow的 大小. 一个比较 ...

  4. PopupWindow 防微信弹出右 侧窗体(继承PopupWindow )

    1, pop自定义 public class SelectPicPopupWindow extends PopupWindow { private Button btn_take_photo, btn ...

  5. PopupWindow 从底部弹出窗体

    第一步  : 初始化PopupWindow private void initPop() { if (view == null) { // 照片 view = View.inflate(Registe ...

  6. PopupWindow --- 弹出底部窗体

    第一步 : 布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...

  7. 仿QQ空间根据位置弹出PopupWindow显示更多操作效果

    我们打开QQ空间的时候有个箭头按钮点击之后弹出PopupWindow会根据位置的变化显示在箭头的上方还是下方,比普通的PopupWindow弹在屏幕中间显示好看的多. 先看QQ空间效果图:       ...

  8. popupwindow的基本使用以及基本动画效果

    1.创建一个popupwindow view的布局文件自己写一个就好了,这里就不说了 View view= LayoutInflater.from(context).inflate(R.layout. ...

  9. webview加载h5,关闭activity时,窗体泄露问题

    问题描述: webview加载一个含有input控件的html页面,当点击input控件是回调app的closepage方法[closepage中只有一个finish操作],出现窗体泄露问题. 分析: ...

随机推荐

  1. (Abstract Factory)抽象工厂

    定义: 抽象工厂同工厂方法有相似处:都提供了对子类创建的封装,都是有工厂方法的接口实现类的中决定了子类被创建为什么对象. 不同于工厂方法之处:工厂方法创建的对象只是一个类型的子类,而抽象工厂创建的对象 ...

  2. ubuntu14.04安装eclipse

    1.安装jdk7.x,参考上篇<Linux14.04安装JDK> 2下载eclipse.tar 3.sudo tar -zxvf eclipse-x-x-.tar.gz mv eclips ...

  3. PostgreSQL Errors and Messages

    使用RAISE语句来报告消息并抛出错误 RAISE [ level ] ’format’ [, expression [, ... ]] [ USING option = expression [, ...

  4. several生命周期

    several生命周期 1. 实例化: 容器调用servlet创建servlet对象 2. 初始化: <init-param> <param-name> company < ...

  5. 使用git做服务器端代码的部署

    传统部署方案     windows 远程桌面     FTP/SFTP     登录服务器pull github代码     Phing(PHP专业部署工具) git 自动部署流程图   服务器端准 ...

  6. datagridview 用法

        标签:c# datagridview 用法 拖一个datagridview控件 初始的名字叫dataGridView1 准备工作: 点击控件右上角的三角,选择“添加列”添加表头的项 基本操作: ...

  7. 深度学习 vs 机器学习 vs 模式识别

    http://www.csdn.net/article/2015-03-24/2824301 [编者按]本文来自CMU的博士,MIT的博士后,vision.ai的联合创始人Tomasz Malisie ...

  8. [tp3.2.1]让默认页面: 加载Home模块的Index控制器;而让admin.php默认去加载Admin模块的Adminc控制器.

    QQ:让index.php默认加载Home模块的Index控制器;而让admin.php默认去加载Admin模块的Adminc控制器.AA:复制index.php命名为admin.php复制(新建)A ...

  9. yii2开启session

    1.在写入session的页面. use yii\web\Session;$session = new Session;$session->open(); 2.在获取session的页面 use ...

  10. 【Ah20160703】咏叹 By C_SUNSHINE

    咏叹 By C_SUNSHINE [试题描述] Salroey拿到了一个1~n的排列A,她想对这个排列进行冒泡排序: counter=0 While A不是升序的 counter=counter+1 ...