安卓 Activity 给 popupwindow 传值

功能简单介绍:在Activity页面中点击某个按钮,弹出自定义popupwindow页面,同时将Activity中的电话号码字符串传递给popupwindow页面。再点击popupwindow页面中的按键直接跳转到安卓拨号页面,同时电话号码也在页面上。

声明:值传递其实用的是安卓的全局变量。

全局变量类中的代码如下:

package com.cn.gordon.exhibition.walk.application;

import android.app.Application;

public final class GlobalVaries extends Application {
private String telNum;

public String getTelNum() {
return telNum;
}

public void setTelNum(String telNum) {
this.telNum = telNum;
}

@Override
public void onCreate() {
telNum = " ";
super.onCreate();
}
}

其中使用全局变量需要在AndroidMenifest.xml中进行一个声明:

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:allowBackup="true"
android:name="com.cn.gordon.exhibition.walk.application.GlobalVaries">
<activity
android:name=".LoadingActivity"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<intent-filter>

.........以上是部分代码,其中android:name="com.cn.gordon.exhibition.walk.application.GlobalVaries"就是声明全局变量的使用。

以下是调用popupwindow的MybusicardholderActivity页面布局,名称是:mybusicardholder.xml

<?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="match_parent"
android:orientation="vertical"
android:background="#FFFFFFFF" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dip"
android:background="#FF333333" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/mybusinesscardholder"
android:textColor="#FFFFFFFF"
android:textSize="20sp" />

<Button
android:layout_width="80dip"
android:layout_height="50dip"
android:id="@+id/btn_menu"
android:background="@drawable/btn_login_back"
/>
<Button
android:layout_width="30dip"
android:layout_height="30dip"
android:id="@+id/btn_query"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="20dip"
android:background="@drawable/querybtn_pressed_bg" />
</RelativeLayout>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/mybusicardlistView"
android:divider="@color/lightgray"
android:dividerHeight="1dip"
android:footerDividersEnabled="true"
android:headerDividersEnabled="true"
>
</ListView>
</LinearLayout>

MybusicardholderActivity中的源码如下:

/**
* @author Seawin龙
*/
package com.cn.gordon.exhibition.walk.activity;

import java.util.ArrayList;
import java.util.HashMap;

import com.cn.gordon.exhibition.walk.application.GlobalVaries;
import com.cn.gordon.exhibition.walk.entity.MyBusiCardHolderBean;
import com.cn.gordon.exhibition.walk.popup.BusicardPopupWindow;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

/**
* @author Seawin龙
*
*/
public class MybusicardholderActivity extends Activity {
private ListView listView;
private SimpleAdapter simpleAdapter;

private String telNum;

// private ProgressDialog pd;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mybusicardholder);
GlobalVaries globalTelNum = (GlobalVaries)getApplication();
init(globalTelNum);
setAction();
}

/**
* @author Seawin龙
* @param globalTelNum
*/
public void init(final GlobalVaries globalTelNum) {

listView = (ListView)findViewById(R.id.mybusicardlistView);
ArrayList<HashMap<String, Object>> list;
list = this.getItem();
simpleAdapter = new SimpleAdapter(this,list , R.layout.busicardlistviewitem, new String[]{"companyname","getcardtime","exhibitionname","companyphonenum"}, new int[]{R.id.companyname,R.id.getcardtime,R.id.exhibitionname,R.id.companyphonenum});
// btnMenu = (Button) findViewById(R.id.btn_menu);
// btnSubmit = (Button) findViewById(R.id.submit_btn);
listView.setAdapter(simpleAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
ArrayList<MyBusiCardHolderBean> list = getmylist();
MyBusiCardHolderBean bean = new MyBusiCardHolderBean();
BusicardPopupWindow bpw = new BusicardPopupWindow(MybusicardholderActivity.this);
int[] location = new int[2];
arg1.getLocationOnScreen(location);
bean = list.get(arg2);
telNum = bean.getCompanyPhonenum();
bpw.showAtLocation(getCurrentFocus(),0,location[0]+150, location[1]+20);
globalTelNum.setTelNum(telNum.substring(3, telNum.length()));
Toast.makeText(MybusicardholderActivity.this, telNum.substring(3, telNum.length()), Toast.LENGTH_SHORT).show();
}
});
}

private ArrayList<MyBusiCardHolderBean> getmylist(){
ArrayList<MyBusiCardHolderBean> list = new ArrayList<MyBusiCardHolderBean>();
MyBusiCardHolderBean mbchBean = new MyBusiCardHolderBean();
mbchBean.setCompanyName("深圳市远望谷信息技术股份有限公司");
mbchBean.setGetcardTime("2014.08.11 11:52:08");
mbchBean.setExhibitionName("物联网博览会");
mbchBean.setCompanyPhonenum("电话:0755-26711633");
list.add(0, mbchBean);
MyBusiCardHolderBean mbchBean1 = new MyBusiCardHolderBean();
mbchBean1.setCompanyName("大唐电信科技各分有限公司");
mbchBean1.setGetcardTime("2014.08.11 12:20:05");
mbchBean1.setExhibitionName("物联网博览会");
mbchBean1.setCompanyPhonenum("电话:010-58919000");
list.add(1, mbchBean1);
MyBusiCardHolderBean mbchBean2 = new MyBusiCardHolderBean();
mbchBean2.setCompanyName("天津中兴智联科技有限公司");
mbchBean2.setGetcardTime("2014.08.11 08:50:12");
mbchBean2.setExhibitionName("物联网博览会");
mbchBean2.setCompanyPhonenum("电话:022-84809202");
list.add(2, mbchBean2);
MyBusiCardHolderBean mbchBean3 = new MyBusiCardHolderBean();
mbchBean3.setCompanyName("美国意联科技有限公司");
mbchBean3.setGetcardTime("2014.08.11 08:50:12");
mbchBean3.setExhibitionName("物联网博览会");
mbchBean3.setCompanyPhonenum("电话:+86-21-62122066");
list.add(3, mbchBean3);
MyBusiCardHolderBean mbchBean4 = new MyBusiCardHolderBean();
mbchBean4.setCompanyName("深圳德卡科技有限公司");
mbchBean4.setGetcardTime("2014.08.11 08:20:12");
mbchBean4.setExhibitionName("物联网博览会");
mbchBean4.setCompanyPhonenum("电话:400-888-7816");
list.add(4, mbchBean4);
return list;
}
public ArrayList<HashMap<String,Object>> getItem(){
ArrayList<HashMap<String, Object>> item = new ArrayList<HashMap<String, Object>>();
MyBusiCardHolderBean mbchBean = new MyBusiCardHolderBean();
for (int i = 0; i < getmylist().size(); i++) {
mbchBean = getmylist().get(i);
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("companyname", mbchBean.getCompanyName());
map.put("getcardtime", mbchBean.getGetcardTime());
map.put("exhibitionname", mbchBean.getExhibitionName());
map.put("companyphonenum", mbchBean.getCompanyPhonenum());
item.add(map);
}
return item;
}

}

以下是popupwindow的页面布局,名称:popupwindow_busicardholder.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="180dip"
android:layout_height="50dip"
android:gravity="center"
android:background="@drawable/layout_sub_itempress_bg"
>
<Button
android:layout_width="60dip"
android:layout_height="50dip"
android:id="@+id/dailphonenumber"
android:layout_gravity="center"
android:background="@drawable/busicardholder_phonecall_press_bg">
</Button>
<Button
android:layout_width="60dip"
android:layout_height="50dip"
android:id="@+id/checkbusicard"
android:layout_gravity="center"
android:background="@drawable/busicardholder_checkcard_press_bg">
</Button>
<Button
android:layout_width="60dip"
android:layout_height="50dip"
android:id="@+id/checkcompany"
android:layout_gravity="center"
android:background="@drawable/busicardholder_phonecall_press_bg">
</Button>
</LinearLayout>

下面是popupwindow,具体名称是:BusicardPopupWindow

package com.cn.gordon.exhibition.walk.popup;

import com.cn.gordon.exhibition.walk.activity.R;
import com.cn.gordon.exhibition.walk.application.GlobalVaries;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RelativeLayout.LayoutParams;

public class BusicardPopupWindow extends PopupWindow {
private View contentView;

private String telNum="";
public BusicardPopupWindow(final Activity context){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
contentView = inflater.inflate(R.layout.popupwindow_busicardholder, null);
//int h = context.getWindowManager().getDefaultDisplay().getHeight();
int w = context.getWindowManager().getDefaultDisplay().getWidth();
// 设置SelectPicPopupWindow的View
this.setContentView(contentView);
// 设置SelectPicPopupWindow弹出窗体的宽
this.setWidth(w / 2 + 50);
// 设置SelectPicPopupWindow弹出窗体的高
this.setHeight(LayoutParams.WRAP_CONTENT);
// 设置SelectPicPopupWindow弹出窗体可点击
this.setFocusable(true);
this.setOutsideTouchable(true);
// 刷新状态
this.update();
// 实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0000000000);
// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作
this.setBackgroundDrawable(dw);
// mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
// 设置SelectPicPopupWindow弹出窗体动画效果
//this.setAnimationStyle(R.style.AnimationPreview);
Button btn_dailnumber = (Button)contentView.findViewById(R.id.dailphonenumber);
Button btn_checkcard = (Button)contentView.findViewById(R.id.checkbusicard);
Button btn_checkcompany = (Button)contentView.findViewById(R.id.checkcompany);
btn_dailnumber.setOnClickListener(new OnClickListener() {
final GlobalVaries globalTelNum = (GlobalVaries) context.getApplication();
@Override
public void onClick(View v) {
if (globalTelNum!=null) {

telNum = globalTelNum.getTelNum();
}
Uri uri = Uri.parse("tel:"+telNum);
Intent intent = new Intent(Intent.ACTION_DIAL,uri);
v.getContext().startActivity(intent);
BusicardPopupWindow.this.dismiss();
}
});
btn_checkcard.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
BusicardPopupWindow.this.dismiss();
}
});

btn_checkcompany.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
BusicardPopupWindow.this.dismiss();
}
});
}
public void showPopupWindow(View parent) {
if (!this.isShowing()) {
// 以下拉方式显示popupwindow
this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18);
} else {
this.dismiss();
}
}
}

本文主要是讲述Activity通过全局变量把数据给popupwindow。仅供参考。同时也感谢那些无私向互联网上发技术贴的童鞋。

参考资料:http://blog.csdn.net/feiyangxiaomi/article/details/9966215

http://blog.csdn.net/junjieking/article/details/7352571

http://blog.csdn.net/l_serein/article/details/6398342

http://bbs.csdn.net/topics/360052162

http://www.oschina.net/question/157182_46417

无论遇到什么问题,解决不了,那就找AndroidAPI。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。