您的位置:首页 > 移动开发 > Android开发

android popupwindow 点击按钮设置黑色背景

2017-08-02 19:36 579 查看

主要是在工作开发中遇到的:因为弄的时间有点久,这次就直接拿来记录一下。

直接上代码

package io.dcloud.H516ADA4C.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.netease.nim.demo.chatroom.activity.ChatRoomActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import io.dcloud.H516ADA4C.MyApplication;
import io.dcloud.H516ADA4C.R;
import io.dcloud.H516ADA4C.adapter.InviteGustAdapter;
import io.dcloud.H516ADA4C.api.API;
import io.dcloud.H516ADA4C.api.ErrorCode;
import io.dcloud.H516ADA4C.base.BaseActivity;
import io.dcloud.H516ADA4C.db.entity.User;
import io.dcloud.H516ADA4C.db.gen.UserDao;
import io.dcloud.H516ADA4C.util.EncryptUtils;
import io.dcloud.H516ADA4C.util.volleyutil.VolleyListener;
import io.dcloud.H516ADA4C.util.volleyutil.VolleyUtils;
import io.dcloud.H516ADA4C.view.SimpleDialog;

/**
* Created by 蛇崽 on 2017/7/28.
* 联系方式:643435675@qq.com
* 人生格言:吸收者。
*/

public class InviteGusetActivity extends BaseActivity implements View.OnClickListener {
ImageView ivBack;
TextView tvTitle;
ImageView ivRight;
ListView lv_vicator;
private Context context;
private PopupWindow popWindow;
private boolean isPopupShow = false;
private UserDao hisDao;
private List<User> histortyList;
private Button btn_invite;
SimpleDialog invitaDialog;
private boolean checkBottomClick = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.act_invitegust_rule);
context = this;
initView();
bindClick();
}

private void initView() {
ivBack = (ImageView) findViewById(R.id.iv_back);
tvTitle = (TextView) findViewById(R.id.tv_title);
ivRight = (ImageView) findViewById(R.id.iv_right);
lv_vicator = (ListView) findViewById(R.id.lv_vicator);
btn_invite = (Button) findViewById(R.id.btn_invite);
User ac = new User();
ac.setName("我是游侠");
ac.setAvatar("http://img3.redocn.com/tupian/20150430/mantenghuawenmodianshiliangbeijing_3924704.jpg");
ac.setAccount("3546546557");
hisDao = MyApplication.getInstances().getDaoSession().getUserDao();
histortyList = hisDao.loadAll();
if (histortyList.size() == 0) {
ac.setId(new Long(1));
ac.setInVitate(true);
hisDao.insert(ac);
ac.setId(new Long(2));
ac.setInVitate(true);
hisDao.insert(ac);
ac.setId(new Long(3));
ac.setInVitate(false);
hisDao.insert(ac);
}
InviteGustAdapter gustAdapter = new InviteGustAdapter(context, histortyList);
lv_vicator.setAdapter(gustAdapter);

tvTitle.setText("邀请嘉宾");
ivRight.setImageResource(R.drawable.nav_course_rule);
}

private void bindClick() {
ivBack.setOnClickListener(this);
ivRight.setOnClickListener(this);
btn_invite.setOnClickListener(this);
}

public void onClick(View view) {
switch (view.getId()) {
case R.id.iv_back:
finish();
break;
case R.id.btn_invite://邀请新嘉宾
showGustDialog();
break;
case R.id.iv_right://右边点击效果
if (popWindow == null) {
initPopupWindow();
} else {
if (isPopupShow) {
isPopupShow = false;
popWindow.dismiss();
} else {
initPopupWindow();
}
}
break;
default:
break;
}
}

private void showGustDialog() {
invitaDialog = new SimpleDialog(context);
invitaDialog.setTitle("邀请人");
invitaDialog.setYesOnclickListener("确定", new SimpleDialog.onYesOnclickListener() {
@Override
public void onYesClick(String account) {
if (TextUtils.isEmpty(account)) {
Toast.makeText(context, "请输入账号", Toast.LENGTH_SHORT).show();
} else {
setChatMemberRole(account);
}

}
});
invitaDialog.setNoOnclickListener("取消", new SimpleDialog.onNoOnclickListener() {
@Override
public void onNoClick() {
invitaDialog.dismiss();
}
});
invitaDialog.show();
}

private void initPopupWindow() {
if (popWindow == null) {
checkBottomClick = false;
View inflate = LayoutInflater.from(context).inflate(R.layout.pop_up_window_rule, null);
RelativeLayout rlBottom = (RelativeLayout) inflate.findViewById(R.id.ll_pop_bottom);
popWindow = new PopupWindow(inflate, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
popWindow.showAsDropDown(tvTitle, tvTitle.getRight() / 2, 0);
popWindow.setTouchable(true);
popWindow.setFocusable(false);
popWindow.setOutsideTouchable(true);
popWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
rlBottom.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (popWindow != null && popWindow.isShowing()) {
popWindow.dismiss();
checkBottomClick = true;
popWindow = null;
WindowManager.LayoutParams params = InviteGusetActivity.this.getWindow().getAttributes();
params.alpha = 1f;
InviteGusetActivity.this.getWindow().setAttributes(params);
}
return false;
}
});
}

popWindow.showAsDropDown(tvTitle, tvTitle.getRight() / 2, 0);
if (checkBottomClick) {
isPopupShow = true;
} else {
isPopupShow = false;
}
}

//    private void initPopupWindow() {
//        if (popWindow == null) {
//            View inflate = LayoutInflater.from(context).inflate(R.layout.pop_up_window_rule, null);
//            RelativeLayout rlBottom = (RelativeLayout) inflate.findViewById(R.id.ll_pop_bottom);
//            popWindow = new PopupWindow(inflate, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
////            popWindow.setContentView(inflate);
////            ColorDrawable dw = new ColorDrawable(0x00000000);
////            popWindow.setBackgroundDrawable(dw);
//            popWindow.showAsDropDown(tvTitle, tvTitle.getRight() / 2, 0);
//            popWindow.setTouchable(true);
//            popWindow.setFocusable(false);
//            popWindow.setOutsideTouchable(true);
//            // popWindow.setContentView(rootView);
//            //setPopupWindowSize();
//            // popWindow.setHeight(rootView.getMeasuredHeight());
//            popWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
//            rlBottom.setOnTouchListener(new View.OnTouchListener() {
//                @Override
//                public boolean onTouch(View v, MotionEvent event) {
//                    if (popWindow != null && popWindow.isShowing()) {
//                        popWindow.dismiss();
//                        popWindow = null;
//                        WindowManager.LayoutParams params = InviteGusetActivity.this.getWindow().getAttributes();
//                        params.alpha = 1f;
//                        InviteGusetActivity.this.getWindow().setAttributes(params);
//                    }
//                    return false;
//                }
//            });
//        }
//    }

private void setChatMemberRole(String account) {
HashMap<String, String> params = new HashMap<>();

params.put("roomid", ChatRoomActivity.roomId);
params.put("operator", MyApplication.user_id);
params.put("target", account);
params.put("opt", "2");
params.put("optvalue", "true");
final String data = Base64.encodeToString((new Gson().toJson(params)).getBytes(), Base64.DEFAULT);
HashMap<String, String> paramsData = new HashMap<>();

paramsData.put("data", data);

String ACCESS_TOKEN = EncryptUtils.getSha1(data + System.currentTimeMillis() / 1000 + EncryptUtils.KEY);

String url = API.CHAT_SET_USER_TYPE + "?access_token=" + ACCESS_TOKEN + "×tamp=" + System.currentTimeMillis() / 1000;

VolleyUtils.post(url, API.CHAT_SET_USER_TYPE, paramsData, null, new VolleyListener() {

@Override
public void success(String response) {
Log.i("cccccc", "success: " + response);

JSONObject jsonObject = null;
String errcode = null;
try {
jsonObject = new JSONObject(response);
errcode = jsonObject.optString(ErrorCode.ERR_CODE);
if (errcode.equals(ErrorCode.ERR_CODE_SUCCESS)) {
Toast.makeText(context, "邀请成功", Toast.LENGTH_SHORT).show();
}

} catch (JSONException e) {
e.printStackTrace();
}

}

});
}

//    private void getChatUserInfo(String account) {
//        HashMap<String, String> params = new HashMap<>();
//        params.put("accid", ChatRoomActivity.myMember.getAccount());
//        final String data = Base64.encodeToString((new Gson().toJson(params)).getBytes(), Base64.DEFAULT);
//        HashMap<String, String> paramsData = new HashMap<>();
//
//        paramsData.put("data", data);
//
//        String ACCESS_TOKEN = EncryptUtils.getSha1(data + System.currentTimeMillis() / 1000 + EncryptUtils.KEY);
//
//        String url = API.CHAT_USER_INFO + "?access_token=" + ACCESS_TOKEN + "×tamp=" + System.currentTimeMillis() / 1000;
//
//        VolleyUtils.post(url, API.CHAT_USER_INFO, paramsData, null, new VolleyListener() {
//
//            @Override
//            public void success(String response) {
//                Log.i("cccccc", "userinfo: " + response);
//
//            }
//
//        });
//    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: