Popupwindowdemo

ShowPopupWindow:

package com.example.popupwindowdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
public class ShowPopupWindow extends Activity implements View.OnClickListener {
 View view;
 PopupWindow pop;
 Button bt1,bt2,bt3;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.popup_activity);
  bt1 = (Button) findViewById(R.id.btnShowAsDrawDown);
  bt1.setOnClickListener(this);
  bt2 = (Button) findViewById(R.id.btnShowAsDrawDown1);
  bt2.setOnClickListener(this);
  bt3 = (Button) findViewById(R.id.btnShowAt);
  bt3.setOnClickListener(this);
  
  initPopupWindow();
 }
 @Override
 public void onClick(View v) {
  // TODO Auto-generated method stub
  switch (v.getId()) {
  case R.id.btnShowAsDrawDown:
   if (pop.isShowing()) {
    pop.dismiss();
   } else {
    pop.showAsDropDown(v);
   }
   break;
  case R.id.btnShowAsDrawDown1:
   if (pop.isShowing()) {
    pop.dismiss();
   } else {
    pop.showAsDropDown(v, 0, -160);
   }
   break;
  default:
   if (pop.isShowing()) {
    pop.dismiss();
   } else {
    pop.showAtLocation(findViewById(R.id.main),
      Gravity.CENTER_HORIZONTAL, 0, 0);
   }
   break;
  }
 }
 private void initPopupWindow() {
  view = this.getLayoutInflater().inflate(R.layout.popup_window, null);
  pop = new PopupWindow(view, ViewGroup.LayoutParams.FILL_PARENT,
    ViewGroup.LayoutParams.WRAP_CONTENT);
  pop.setOutsideTouchable(true);
  view.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    pop.dismiss();
   }
  });
 }
}

popup_activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="pop 展示" />
    <Button
        android:id="@+id/btnShowAsDrawDown"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="第一个" />
    <Button
        android:id="@+id/btnShowAsDrawDown1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="第二个" />
    <Button
        android:id="@+id/btnShowAt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="第三个" />
</LinearLayout>

popup_window:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#d3d3d3"
    android:gravity="center_horizontal"
    android:orientation="vertical" >
    <Button
        android:id="@+id/btn_pop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:text="哈哈哈" />
</LinearLayout>
<?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="wrap_content"
    android:background="#FFBBFFBB"
    android:orientation="vertical" >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="Hello My Window"
        android:textSize="20sp" />
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="Button"
        android:textSize="20sp" />
</LinearLayout>
弹出框布局
package com.example.popopupwindowdemo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;
public class MainActivity extends Activity {
    private Context mContext = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showPopupWindow(view);
            }
        });
    }
    private void showPopupWindow(View view) {
        // 一个自定义的布局,作为显示的内容
        View contentView = LayoutInflater.from(mContext).inflate(
                R.layout.pop_window, null);
        // 设置按钮的点击事件
        Button button = (Button) contentView.findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "button is pressed",
                        Toast.LENGTH_SHORT).show();
            }
        });
        final PopupWindow popupWindow = new PopupWindow(contentView,
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        popupWindow.setTouchInterceptor(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.i("mengdd", "onTouch : ");
                return false;
                // 这里如果返回true的话,touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss
            }
        });
        // 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
        // 我觉得这里是API的一个bug
        popupWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.selectmenu_bg_downward));
        // 设置好参数之后再show
        popupWindow.showAsDropDown(view);
    }
}

转载于:https://my.oschina.net/u/1994482/blog/425384

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值