如图是效果图
当点击 “点我”的按钮是 会弹出 如图的 弹窗
主代码如下 布局xml 就是只有一个 Button
1 package org.xml.popdemo; 2 3 import ogg.huanxin.huadong.R; 4 import android.app.Activity; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.view.View.OnClickListener; 8 import android.widget.Button; 9 10 public class MainPopWindow extends Activity implements OnClickListener { 11 private Button popButton; 12 private PopWindowForAttr popWindowForAttr; 13 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 // TODO Auto-generated method stub 17 super.onCreate(savedInstanceState); 18 super.setContentView(R.layout.main_popwindow); 19 popButton=(Button)super.findViewById(R.id.popbutton); 20 popButton.setOnClickListener(this); 21 popWindowForAttr = new PopWindowForAttr(this); 22 } 23 24 @Override 25 public void onClick(View v) { 26 // TODO Auto-generated method stub 27 switch (v.getId()) { 28 case R.id.popbutton: 29 popWindowForAttr.showAsDropDown(v); 30 break; 31 default: 32 break; 33 34 } 35 } 36 }
1 package org.xml.popdemo; 2 3 import ogg.huanxin.huadong.R; 4 import android.app.ActionBar.LayoutParams; 5 import android.content.Context; 6 import android.graphics.Color; 7 import android.graphics.drawable.ColorDrawable; 8 import android.view.Gravity; 9 import android.view.LayoutInflater; 10 import android.view.View; 11 import android.view.View.OnClickListener; 12 import android.widget.Button; 13 import android.widget.ImageButton; 14 import android.widget.PopupWindow; 15 import android.widget.TextView; 16 import android.widget.Toast; 17 import android.widget.PopupWindow.OnDismissListener; 18 19 public class PopWindowForAttr implements OnClickListener, OnDismissListener { 20 private Context context; 21 private PopupWindow popupWindow; 22 private Button addCart, doCart; 23 private final int AddReduce = 1; 24 private TextView Popreduce, Popnum, Popadd; 25 private ImageButton deleteButton; 26 27 public PopWindowForAttr(Context context) { 28 this.context = context; 29 // 自定义的一个控件作为显示内容 30 View contentView = LayoutInflater.from(context).inflate( 31 R.layout.popupwindowdemo, null); 32 // 33 addCart = (Button) contentView.findViewById(R.id.addCart); 34 doCart = (Button) contentView.findViewById(R.id.doCart); 35 Popreduce = (TextView) contentView.findViewById(R.id.pop_reduce); 36 Popnum = (TextView) contentView.findViewById(R.id.pop_num); 37 Popadd = (TextView) contentView.findViewById(R.id.pop_add); 38 deleteButton = (ImageButton) contentView.findViewById(R.id.delete); 39 // 各组件绑定事件 40 addCart.setOnClickListener(this); 41 doCart.setOnClickListener(this); 42 Popreduce.setOnClickListener(this); 43 Popadd.setOnClickListener(this); 44 deleteButton.setOnClickListener(this); 45 // 将加载的视图view载入PopubWindow,并且设置popupwindow这个组件的动画效果 46 popupWindow = new PopupWindow(contentView, LayoutParams.MATCH_PARENT, 47 LayoutParams.WRAP_CONTENT, true); 48 // 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框 49 popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 50 // 当popWindow消失时的监听 51 popupWindow.setOnDismissListener(this); 52 } 53 54 public void showAsDropDown(View v) { 55 // showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移 56 // showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移 57 // showAtLocation(View parent, int gravity, int x, int 58 // y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移 59 popupWindow.showAtLocation(v, Gravity.BOTTOM, 100, 100); 60 // 设置setFocusable(true),要不然点击弹窗其他地方以及返回键,弹窗都不会退出 61 // 也才能让popupWindow里面的布局控件获得点击的事件,否则就被它的父亲view给拦截了 62 popupWindow.setFocusable(true); 63 //这个方法时设置popupWindow以外的区域可以相应触摸事件,比如我们重写了触摸事件去做一些别的操作,但首先Focusable是true。 64 popupWindow.setOutsideTouchable(true); 65 popupWindow.update(); 66 67 } 68 69 // 销毁 70 @Override 71 public void onDismiss() { 72 // TODO Auto-generated method stub 73 popupWindow.dismiss(); 74 } 75 76 @Override 77 public void onClick(View arg0) { 78 // TODO Auto-generated method stub 79 switch (arg0.getId()) { 80 case R.id.addCart: 81 Toast.makeText(context, "添加到购物车", Toast.LENGTH_SHORT).show(); 82 break; 83 case R.id.doCart: 84 Toast.makeText(context, "立即购买", Toast.LENGTH_SHORT).show(); 85 break; 86 case R.id.delete: 87 onDismiss(); 88 break; 89 case R.id.pop_reduce: 90 if (!Popnum.getText().toString().equals("1")) { 91 String num_ReduceString = Integer.valueOf(Popnum.getText() 92 .toString()) - AddReduce + ""; 93 Popnum.setText(num_ReduceString); 94 } else { 95 Toast.makeText(context, "您买入的数量不能低于1", Toast.LENGTH_SHORT) 96 .show(); 97 } 98 break; 99 case R.id.pop_add: 100 if (!Popnum.getText().toString().equals("1000")) { 101 String numString = Integer.valueOf(Popnum.getText().toString()) 102 + AddReduce + ""; 103 Popnum.setText(numString); 104 } 105 break; 106 default: 107 break; 108 } 109 } 110 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="#FFFFFF" 6 android:orientation="vertical" 7 android:paddingLeft="15dp" 8 android:paddingRight="15dp" > 9 10 <LinearLayout 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" 13 android:layout_gravity="center_vertical" 14 android:layout_marginTop="10dp" > 15 16 <ImageView 17 android:layout_width="50dp" 18 android:layout_height="50dp" 19 android:scaleType="fitCenter" 20 android:src="@drawable/d" /> 21 22 <LinearLayout 23 android:layout_width="wrap_content" 24 android:layout_height="match_parent" 25 android:layout_marginLeft="10dp" 26 android:layout_weight="1" 27 android:orientation="vertical" > 28 29 <TextView 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:gravity="center_vertical" 33 android:text="现售价:¥49.00" 34 android:textSize="15sp" /> 35 36 <TextView 37 android:id="@+id/kucun" 38 android:layout_width="wrap_content" 39 android:layout_height="wrap_content" 40 android:gravity="center_vertical" 41 android:paddingTop="4dp" 42 android:text="库存为:987" 43 android:textSize="12sp" /> 44 </LinearLayout> 45 46 <ImageButton 47 android:layout_width="wrap_content" 48 android:layout_height="wrap_content" 49 android:id="@+id/delete" 50 android:layout_weight="1" 51 android:background="@null" 52 android:padding="4dp" 53 android:src="@drawable/delete" /> 54 </LinearLayout> 55 56 <LinearLayout 57 android:layout_width="wrap_content" 58 android:layout_height="wrap_content" 59 android:background="#eee" 60 android:orientation="horizontal" > 61 62 <TextView 63 android:layout_width="0dp" 64 android:layout_height="wrap_content" 65 android:layout_weight="1" 66 android:padding="1dp" 67 android:text="购买数量" /> 68 69 <TextView 70 android:id="@+id/pop_reduce" 71 android:layout_width="wrap_content" 72 android:layout_height="wrap_content" 73 android:paddingBottom="10dp" 74 android:paddingLeft="12dp" 75 android:paddingRight="12dp" 76 android:paddingTop="10dp" 77 android:text="-" 78 android:color="#000000" /> 79 80 <TextView 81 android:id="@+id/pop_num" 82 android:layout_width="60dp" 83 android:layout_height="wrap_content" 84 android:gravity="center" 85 android:padding="10dp" 86 android:text="1" 87 android:textColor="#000000" /> 88 89 <TextView 90 android:id="@+id/pop_add" 91 android:layout_width="wrap_content" 92 android:layout_height="wrap_content" 93 android:paddingBottom="10dp" 94 android:paddingLeft="12dp" 95 android:paddingRight="12dp" 96 android:paddingTop="10dp" 97 android:text="+" 98 android:textColor="#000000" /> 99 </LinearLayout> 100 101 <LinearLayout 102 android:layout_width="wrap_content" 103 android:layout_height="wrap_content" > 104 105 <Button 106 android:id="@+id/addCart" 107 android:layout_width="wrap_content" 108 android:layout_height="wrap_content" 109 android:layout_weight="1" 110 android:background="@drawable/png_3" 111 android:paddingBottom="10dp" 112 android:paddingLeft="20dp" 113 android:paddingRight="20dp" 114 android:paddingTop="10dp" 115 android:text="加入购物车" 116 android:textColor="@android:color/white" /> 117 118 <Button 119 android:id="@+id/doCart" 120 android:layout_width="wrap_content" 121 android:layout_height="wrap_content" 122 android:layout_weight="1" 123 android:background="@drawable/png_3" 124 android:paddingBottom="10dp" 125 android:paddingLeft="20dp" 126 android:paddingRight="20dp" 127 android:paddingTop="10dp" 128 android:text="立即购买" 129 android:textColor="@android:color/white" /> 130 </LinearLayout> 131 132 </LinearLayout>View Code