1. 定义一个接口:OnSelectItemListener。定义一个方法 void selectItem(String name, int type),作为点击弹窗的每个Item的回调接口。
2. 自定义弹窗类:MyPopupWindow,其布局文件为popup_window.xml。当在MainActivity调用其构造函数创建对象时,同时执行initPopupWindow()函数,给每个Item设置监听器,监听点击Item时,回调接口函数selectItem("Pop Window A", POP_WINDOW_ITEM_1),该函数在MainActivity中实现。
3. 主Activity: MainActivity。其布局文件为一个Button和一个TextView。监听Button,每当点击则弹出PopWindow,呈现三个Item。调用MyPopupWindow类中的方法setOnSelectItemListener(OnSelectItemListener listener),传入OnSelectItemListener 对象作为参数,同时实现回调接口OnSelectItemListener的方法void selectItem(String name, int type)。
主Activity: MainActivity.java
package com.lambdroid.callbacktest2;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
//联系接口的回调以及PopWindow弹窗的简单使用
public class MainActivity extends Activity {
private MyPopupWindow myPopupWindow;
private Button btn_pop_window;
private TextView tv_display;
protected Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
btn_pop_window = (Button) findViewById(R.id.btn_pop_window);
tv_display = (TextView) findViewById(R.id.tv_display);
//给Button设置事件监听:弹出弹窗
btn_pop_window.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myPopupWindow.show(btn_pop_window);
}
});
myPopupWindow = new MyPopupWindow(context);
//实现OnSelectItemListener接口的selectItem方法:对于弹窗三个Item的事件监听
myPopupWindow.setOnSelectItemListener(new OnSelectItemListener() {
@Override
public void selectItem(String name, int type) {
//点击电站列表,弹出弹框
if (myPopupWindow != null && myPopupWindow.isShowing()) {
myPopupWindow.dismiss();
}
tv_display.setText(name);
switch (type){
case MyPopupWindow.POP_WINDOW_ITEM_1:
Toast.makeText(context, "我是弹窗A, 我的英文名是" + name, Toast.LENGTH_SHORT).show();
break;
case MyPopupWindow.POP_WINDOW_ITEM_2:
Toast.makeText(context, "我是弹窗B, 我的英文名是" + name, Toast.LENGTH_SHORT).show();
break;
case MyPopupWindow.POP_WINDOW_ITEM_3:
Toast.makeText(context, "我是弹窗C, 我的英文名是" + name, Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
});
}
}
activity_main.xml
自定义弹窗类:MyPopupWindow.java
package com.lambdroid.callbacktest2;
import android.app.ActionBar;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
public class MyPopupWindow implements View.OnClickListener{
private PopupWindow mPopWindow;
private Context mContext;
private LinearLayout llPop1;
private LinearLayout llPop2;
private LinearLayout llPop3;
private int pw_height;
public static final int POP_WINDOW_ITEM_1 = 1;
public static final int POP_WINDOW_ITEM_2 = 2;
public static final int POP_WINDOW_ITEM_3 = 3;
private OnSelectItemListener listener;
public void setOnSelectItemListener(OnSelectItemListener listener){
this.listener = listener;
}
public MyPopupWindow(Context context){
mContext = context;
initPopupWindow(); //初始化弹窗
}
public void initPopupWindow(){
View view = LayoutInflater.from(mContext).inflate(R.layout.popup_window, null);
mPopWindow = new PopupWindow(view, ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT, true);
mPopWindow.setOutsideTouchable(true);
/** 为其设置背景,使得其内外焦点都可以获得 */
mPopWindow.setBackgroundDrawable(new ColorDrawable());
mPopWindow.setFocusable(true);
pw_height = view.getHeight();
llPop1 = (LinearLayout) view.findViewById(R.id.ll_pop_1);
llPop1.setOnClickListener(this);
llPop2 = (LinearLayout) view.findViewById(R.id.ll_pop_2);
llPop2.setOnClickListener(this);
llPop3 = (LinearLayout) view.findViewById(R.id.ll_pop_3);
llPop3.setOnClickListener(this);
}
//监听三个弹窗的点击事件
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.ll_pop_1:
if(listener != null) {
listener.selectItem("Pop Window A", POP_WINDOW_ITEM_1); //回调接口
}
break;
case R.id.ll_pop_2:
if(listener != null) {
listener.selectItem("Pop Window B", POP_WINDOW_ITEM_2);
}
break;
case R.id.ll_pop_3:
if(listener != null) {
listener.selectItem("Pop Window C", POP_WINDOW_ITEM_1);
}
break;
default:
break;
}
}
//显示弹窗,并设置弹窗基于标题栏的显示位置
public void show(View view) {
//popupwindow相对view位置x轴偏移量
View viewTemp = mPopWindow.getContentView();
viewTemp.measure(0, 0);
int width = viewTemp.getMeasuredWidth();
int xOffset = (view.getWidth() - width) / 2;
mPopWindow.showAsDropDown(view, xOffset, 0);
}
/**
* 退出popupwindow
*/
public void dismiss() {
if (mPopWindow != null && mPopWindow.isShowing()) {
mPopWindow.dismiss();
}
}
/**
* popupwindow是否正在显示
*/
public boolean isShowing() {
if(mPopWindow != null) {
return mPopWindow.isShowing();
}
return false;
}
}
popup_window.xml
package com.lambdroid.callbacktest2;
public interface OnSelectItemListener {
void selectItem(String name, int type);
}
点击第二个Item,通过回调函数,来实现TextView内容的修改,以及弹出Toast