关闭

android----popuwindow的使用,自定义的方式

标签: androidjavapopuwindow
127人阅读 评论(0) 收藏 举报
分类:

1自定义的popuwindow类

package com.dr.Custom.View;


import com.dr.UI.Activity.CityActivity;
import com.example.meituanbydr.R;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;


public class MyCityPopWindow extends PopupWindow {
TextView txt_changeCity;
View ppwView;
TextView txt_changeCityName;


public MyCityPopWindow(final Context context, OnClickListener onClick) {
ppwView = LayoutInflater.from(context).inflate(R.layout.popwindow_city,
null);


txt_changeCity = (TextView) ppwView
.findViewById(R.id.txt_groupBuy_ChangeCity);
  txt_changeCityName = (TextView) ppwView
.findViewById(R.id.txt_groupBuy_CurCityName);

// 取出"selectedCity"的值,并显示出来
SharedPreferences sharedPreference = context
.getSharedPreferences("selectedCity", Context.MODE_PRIVATE);
   String cityName = sharedPreference.getString("cityName", "未定位");


   txt_changeCityName.setText(cityName);

//txt_changeCity添加监听事件,跳转到城市列表界面
txt_changeCity.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

Intent intent = new Intent(context, CityActivity.class);
context.startActivity(intent);
dismiss();

}
});


// 窗体添加布局
this.setContentView(ppwView);
// 窗体高铺满
this.setWidth(LayoutParams.MATCH_PARENT);
// 窗体高自适应
this.setHeight(LayoutParams.MATCH_PARENT);


// 弹出窗体可点击
this.setFocusable(true);
// 弹出窗体动画效果
// this.setAnimationStyle(R.style.AnimBottom);
// this.setAnimationStyle(R.anim.push_bottom_out);
// 实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0xb0000000);
// 弹出窗体的背景
this.setBackgroundDrawable(dw);


// ppwView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
ppwView.setOnTouchListener(new OnTouchListener() {


public boolean onTouch(View v, MotionEvent event) {


int upper = ppwView.findViewById(R.id.ll_ppw_city).getTop();
int height = ppwView.findViewById(R.id.ll_ppw_city).getHeight();

int y = (int) event.getY();
if (event.getAction() == MotionEvent.ACTION_UP) {
if (y < upper || y> (upper+height)  ) {
dismiss();
}
}
return true;
}
});
}




}

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:32772次
    • 积分:985
    • 等级:
    • 排名:千里之外
    • 原创:67篇
    • 转载:5篇
    • 译文:0篇
    • 评论:3条
    最新评论