本文主要是介绍Android 封装popwindow类,实现所有activity共用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
popwindow是一个弹出window,在此对其进行封装,使这个popwindow可以在所有页面都显示。
在CSDN上有类似的封装类供下载,可以去自行下载然后做修改。附上地址:http://download.csdn.net/detail/jdsjlzx/9583823
这样一来就会出现一个问题,定义popwindow上的item的点击事件做相应的动作的时候,就会提示空指针异常,从而强制退出程序。如下将分析如何解决。
定义公用类 以便调用所有activity调用。
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;public class CommonPopWindowActivity extends Activity {private PopMenu popMenu; // 此popmenu是2中定义的popwindow生成类private Context context;private Context current_activity;private static CommonPopWindowActivity instance;public synchronized static CommonPopWindowActivity getInstance() {if (null == instance) {instance = new CommonPopWindowActivity();}return instance;}public void buildPopWindow(Activity activity, ImageButton popWind) {this.context = activity;popMenu = new PopMenu(context);popMenu.addItems(new String[] { "登录", "注销", "使用说明", "关于软件" });// 菜单项点击监听器popMenu.setOnItemClickListener(popmenuItemClickListener);popWind.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {popMenu.showAsDropDown(v);}});}// 弹出菜单监听器OnItemClickListener popmenuItemClickListener = new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position,long id) {current_activity = view.getContext();// current_activity 的定义是为了取得调用此方Activity//的本身实例,这样才不会报空指针异常。System.out.println("下拉菜单点击" + position);// 取得item上的值// HashMap<String, Object> item = (HashMap<String, Object>)// adapterView// .getItemAtPosition(position);Intent intent = new Intent();intent.putExtra("Flag", RS_FLAG);intent.setClass(current_activity, LoginActivity.class);((Activity) current_activity).startActivityForResult(intent, 5);popMenu.dismiss();};
}
定义pop类
import java.util.ArrayList;
import com.ytumobilelibrary.R;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;public class PopMenu {private ArrayList<String> itemList;private Context context;private PopupWindow popupWindow;private ListView listView;// private OnItemClickListener listener;public PopMenu(Context context) {this.context = context;itemList = new ArrayList<String>(5);View view = LayoutInflater.from(context).inflate(R.layout.popmenu, null);// 设置 listviewlistView = (ListView) view.findViewById(R.id.popwin_listView);listView.setAdapter(new PopAdapter());listView.setFocusableInTouchMode(true);listView.setFocusable(true);// popupWindow = new PopupWindow(view, 100, LayoutParams.WRAP_CONTENT);popupWindow = new PopupWindow(view, context.getResources().getDimensionPixelSize(R.dimen.popmenu_width),context.getResources().getDimensionPixelSize(R.dimen.popmenu_height));// context.getResources()// .getDimensionPixelSize(R.dimen.popmenu_width)// 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景(很神奇的)popupWindow.setBackgroundDrawable(new BitmapDrawable());}// 设置菜单项点击监听器public void setOnItemClickListener(OnItemClickListener listener) {// this.listener = listener;listView.setOnItemClickListener(listener);}// 批量添加菜单项public void addItems(String[] items) {for (String s : items)itemList.add(s);}// 单个添加菜单项public void addItem(String item) {itemList.add(item);}// 下拉式 弹出 pop菜单 parent 右下角public void showAsDropDown(View parent) {popupWindow.showAsDropDown(parent, 10,// 保证尺寸是根据屏幕像素密度来的context.getResources().getDimensionPixelSize(R.dimen.popmenu_yoff));// 使其聚集popupWindow.setFocusable(true);// 设置允许在外点击消失popupWindow.setOutsideTouchable(true);// // 刷新状态popupWindow.update();}// 隐藏菜单public void dismiss() {popupWindow.dismiss();}// 适配器private final class PopAdapter extends BaseAdapter {@Overridepublic int getCount() {// TODO Auto-generated method stubreturn itemList.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn itemList.get(position);}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubViewHolder holder;if (convertView == null) {convertView = LayoutInflater.from(context).inflate(R.layout.pomenu_item, null);holder = new ViewHolder();convertView.setTag(holder);holder.groupItem = (TextView) convertView.findViewById(R.id.popwin_textView);} else {holder = (ViewHolder) convertView.getTag();}holder.groupItem.setText(itemList.get(position));return convertView;}private final class ViewHolder {TextView groupItem;}}
}
pomenu_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="120.0dip"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_vertical"
android:background="@drawable/bg_pop_item" > <TextView
android:id="@+id/popwin_textView"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="10.0dip"android:gravity="center"android:text="测试"android:textColor="#FFFFFF"android:textSize="17.0dip" />
</LinearLayout>
pomenu.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_view_cont"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
><ListView
android:id="@+id/popwin_listView"android:layout_width="fill_parent"android:layout_height="0.0dip"android:layout_weight="1.0"android:background="@drawable/map_tips" android:cacheColorHint="#00000000"android:divider="@drawable/line2"android:dividerHeight="4.0dip"android:focusable="true"android:scrollbars="none" ></ListView>
</LinearLayout>
bg_pop_item.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/home_btn_bg_s"android:state_pressed="true"/><item android:drawable="@drawable/home_btn_bg_s"android:state_enabled="true"android:state_focused="true"/><item android:drawable="@drawable/home_btn_bg_s"android:state_checked="true"android:state_enabled="true"></item>
</selector>
在所有Activity中调用公用类CommonPopWindowActivity 的方法如下:
//ImageButton 是所有activity中定义的ImageButton,点击此bt可以弹出popwindow
ImageButton popWind = (ImageButton) findViewById(R.id.radio_button4);
//加载popwindow this就是该activity的实例,这样在CommonPopWindowActivity类中跳转页面或者弹出toast才不会报空指针异常
CommonPopWindowActivity.getInstance().buildPopWindow(this,popWind);
这篇关于Android 封装popwindow类,实现所有activity共用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!