Android接口回调总结,以及运用到弹窗PopWindow的Demo实现

背景:

最近项目中接触到接口回调,以及Android弹窗PopWindow组件的使用,现在利用学到的知识自己写了一个简单的Demo,练习下在Android下如何运用接口回调,来实现弹窗PopWindow的功能。

代码结构:

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

[java]view plaincopy

packagecom.lambdroid.callbacktest2;

importandroid.app.Activity;

importandroid.content.Context;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.widget.Button;

importandroid.widget.TextView;

importandroid.widget.Toast;

//联系接口的回调以及PopWindow弹窗的简单使用

publicclassMainActivityextendsActivity {

privateMyPopupWindow myPopupWindow;

privateButton btn_pop_window;

privateTextView tv_display;

protectedContext context;

@Override

protectedvoidonCreate(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(newView.OnClickListener() {

@Override

publicvoidonClick(View v) {

myPopupWindow.show(btn_pop_window);

}

});

myPopupWindow =newMyPopupWindow(context);

//实现OnSelectItemListener接口的selectItem方法:对于弹窗三个Item的事件监听

myPopupWindow.setOnSelectItemListener(newOnSelectItemListener() {

@Override

publicvoidselectItem(String name,inttype) {

//点击电站列表,弹出弹框

if(myPopupWindow !=null&& myPopupWindow.isShowing()) {

myPopupWindow.dismiss();

}

tv_display.setText(name);

switch(type){

caseMyPopupWindow.POP_WINDOW_ITEM_1:

Toast.makeText(context,"我是弹窗A, 我的英文名是"+ name, Toast.LENGTH_SHORT).show();

break;

caseMyPopupWindow.POP_WINDOW_ITEM_2:

Toast.makeText(context,"我是弹窗B, 我的英文名是"+ name, Toast.LENGTH_SHORT).show();

break;

caseMyPopupWindow.POP_WINDOW_ITEM_3:

Toast.makeText(context,"我是弹窗C, 我的英文名是"+ name, Toast.LENGTH_SHORT).show();

break;

default:

break;

}

}

});

}

}

activity_main.xml

[html]view plaincopy


android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/btn_pop_window"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:layout_margin="20dp"

android:padding="20dp"

android:text="Pop Window"

android:textSize="20sp"/>

android:id="@+id/tv_display"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_marginTop="10dp"

android:gravity="center"

android:text="Hello World!"

android:textSize="30sp"/>

自定义弹窗类:MyPopupWindow.java

[java]view plaincopy

packagecom.lambdroid.callbacktest2;

importandroid.app.ActionBar;

importandroid.content.Context;

importandroid.graphics.drawable.ColorDrawable;

importandroid.view.LayoutInflater;

importandroid.view.View;

importandroid.widget.LinearLayout;

importandroid.widget.PopupWindow;

publicclassMyPopupWindowimplementsView.OnClickListener{

privatePopupWindow mPopWindow;

privateContext mContext;

privateLinearLayout llPop1;

privateLinearLayout llPop2;

privateLinearLayout llPop3;

privateintpw_height;

publicstaticfinalintPOP_WINDOW_ITEM_1 =1;

publicstaticfinalintPOP_WINDOW_ITEM_2 =2;

publicstaticfinalintPOP_WINDOW_ITEM_3 =3;

privateOnSelectItemListener listener;

publicvoidsetOnSelectItemListener(OnSelectItemListener listener){

this.listener = listener;

}

publicMyPopupWindow(Context context){

mContext = context;

initPopupWindow();//初始化弹窗

}

publicvoidinitPopupWindow(){

View view = LayoutInflater.from(mContext).inflate(R.layout.popup_window,null);

mPopWindow =newPopupWindow(view, ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT,true);

mPopWindow.setOutsideTouchable(true);

/** 为其设置背景,使得其内外焦点都可以获得 */

mPopWindow.setBackgroundDrawable(newColorDrawable());

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

publicvoidonClick(View v) {

switch(v.getId()){

caseR.id.ll_pop_1:

if(listener !=null) {

listener.selectItem("Pop Window A", POP_WINDOW_ITEM_1);//回调接口

}

break;

caseR.id.ll_pop_2:

if(listener !=null) {

listener.selectItem("Pop Window B", POP_WINDOW_ITEM_2);

}

break;

caseR.id.ll_pop_3:

if(listener !=null) {

listener.selectItem("Pop Window C", POP_WINDOW_ITEM_1);

}

break;

default:

break;

}

}

//显示弹窗,并设置弹窗基于标题栏的显示位置

publicvoidshow(View view) {

//popupwindow相对view位置x轴偏移量

View viewTemp = mPopWindow.getContentView();

viewTemp.measure(0,0);

intwidth = viewTemp.getMeasuredWidth();

intxOffset = (view.getWidth() - width) /2;

mPopWindow.showAsDropDown(view, xOffset,0);

}

/**

* 退出popupwindow

*/

publicvoiddismiss() {

if(mPopWindow !=null&& mPopWindow.isShowing()) {

mPopWindow.dismiss();

}

}

/**

* popupwindow是否正在显示

*/

publicbooleanisShowing() {

if(mPopWindow !=null) {

returnmPopWindow.isShowing();

}

returnfalse;

}

}

popup_window.xml

[html]view plaincopy


android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="vertical">

android:id="@+id/ll_alarm_type"

android:layout_width="120dp"

android:layout_height="130dp"

android:orientation="vertical"

android:background="@drawable/popupwindow"

android:paddingBottom="16dp"

android:paddingTop="16dp">

android:id="@+id/ll_pop_1"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1"

android:layout_marginTop="5dp"

android:layout_gravity="center_horizontal"

android:orientation="vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:text="窗口 A"

android:textSize="15sp"

android:textColor="#ffffff"/>

android:id="@+id/ll_pop_2"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1"

android:layout_gravity="center_horizontal"

android:orientation="vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:text="窗口 B"

android:textSize="15sp"

android:textColor="#ffffff"/>

android:id="@+id/ll_pop_3"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1"

android:layout_gravity="center_horizontal"

android:orientation="vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:text="窗口 C"

android:textSize="15sp"

android:textColor="#FFFFFF"/>

回调接口:OnSelectItemListener

[java]view plaincopy

packagecom.lambdroid.callbacktest2;

publicinterfaceOnSelectItemListener {

voidselectItem(String name,inttype);

}

Demo演示

点击Button,弹出弹窗,显示三个Item

Android接口回调总结,以及运用到弹窗PopWindow的Demo实现_第1张图片

点击第二个Item,通过回调函数,来实现TextView内容的修改,以及弹出Toast

Android接口回调总结,以及运用到弹窗PopWindow的Demo实现_第2张图片

总结

Java回调情形涉及很多,本文属于接口的异步回调:当不知道何时会执行接口的回调函数,(通过接口回调来对获取到的资源的操作)。除此还有线程间的异步回调(子线程进行耗时操作,操作完毕通知主线程或将数据传给主线程处理),以及利用接口回调来实现线程间的数据通信等等(Android可以利用Handler来实现)。等下次再举例说明Java回调函数的其它情形。

你可能感兴趣的:(Android接口回调总结,以及运用到弹窗PopWindow的Demo实现)