1.
/** * 初始化popwindow */ public void init_popwindow(){ LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); ViewGroup menuView = (ViewGroup) mLayoutInflater.inflate( R.layout.linkman, null, true); final EditText nametext = (EditText) menuView.findViewById(R.id.editText1); final CheckBox checkbox_default = (CheckBox) menuView.findViewById(R.id.checkbox_default); final EditText teltext = (EditText) menuView.findViewById(R.id.editText2); final Button contact_concal=(Button)menuView.findViewById(R.id.contact_concal);//取消按钮 final Button contact_ok=(Button)menuView.findViewById(R.id.contact_ok);//取消按钮 final TextView text_how=(TextView)menuView.findViewById(R.id.text_how); text_how.setText("新建联系人"); pw = new PopupWindow(menuView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true); // 设置点击返回键使其消失,且不影响背景,此时setOutsideTouchable函数即使设置为false // 点击PopupWindow 外的屏幕,PopupWindow依然会消失;相反,如果不设置BackgroundDrawable // 则点击返回键PopupWindow不会消失,同时,即时setOutsideTouchable设置为true // 点击PopupWindow 外的屏幕,PopupWindow依然不会消失 // pw.setBackgroundDrawable(new BitmapDrawable()); pw.setBackgroundDrawable(new ColorDrawable(80000000)); pw.setOutsideTouchable(false); // 设置是否允许在外点击使其消失,到底有用没? pw.update(); contact_concal.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { pw.dismiss(); } }); contact_ok.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (nametext.getText().length() == 0) { Toast.makeText(ContactActivity.this, "请输入联系人姓名", Toast.LENGTH_SHORT).show(); } else if (teltext.getText().length() == 0) { Toast.makeText(ContactActivity.this, "请输入联系人电话", Toast.LENGTH_SHORT).show(); } else if (teltext.getText().length() != 11) { Toast.makeText(ContactActivity.this, "请输入正确手机号", Toast.LENGTH_SHORT).show(); } else { HashMap<String, Object> data = new HashMap<>(); data.put("token", token); data.put("name", nametext.getText()); data.put("tel", teltext.getText()); data.put("default", checkbox_default.isChecked() ? "1" : "0"); ContactDataModel.getContactadd(data); ContactDataModel.getContact(token); pw.dismiss(); } } }); }2.按钮的点击事件
@Click(R.id.action_add_contact_btn) public void nextClick(View v){ init_popwindow(); // 在父容器的什么位置,gravity 为相对位置,如:正中央 Gravity.CENTER、下方 Gravity.BOTTOM、Gravity.RIGHT|Gravity.BOTTOM 右下方等,后面两个参数为 x/y 轴的偏移量。 pw.showAtLocation(v, Gravity.CENTER, 0, 0); }3.
linkman xml的定义
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#80000000" android:weightSum="1"> <RelativeLayout android:layout_marginLeft="@dimen/base_detail_padding" android:layout_marginRight="@dimen/base_detail_padding" android:background="@color/bc_e9" android:layout_centerVertical="true" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/text_how" android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/balck_text18" android:layout_marginLeft="10dp" android:text="heshuo"/> <LinearLayout android:layout_below="@id/text_how" android:id="@+id/lin_user" style="@style/base_input_border" android:layout_marginTop="20dp" android:orientation="horizontal" android:background="@color/bc_e9" > <TextView style="@style/balck_text16" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" 用户名:"/> <EditText style="@style/base_input" android:inputType="text" android:hint="姓名" android:id="@+id/editText1"/> </LinearLayout> <LinearLayout android:layout_below="@id/lin_user" android:id="@+id/lin_photo" android:background="@null" style="@style/base_input_border" android:orientation="horizontal" android:layout_marginTop="20dp"> <TextView style="@style/balck_text16" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="手机号码:"/> <EditText style="@style/base_input" android:hint="手机号码" android:id="@+id/editText2" android:maxLength="11" android:inputType="phone"/> </LinearLayout> <LinearLayout android:id="@+id/lin_moren" android:layout_below="@id/lin_photo" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:paddingTop="10dp" android:paddingBottom="10dp"> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设为默认联系人" android:textColor="@color/black" android:id="@+id/checkbox_default" /> </LinearLayout> <LinearLayout android:layout_below="@id/lin_moren" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/contact_concal" android:layout_margin="10dp" android:background="@color/white" android:layout_weight="1" android:text="取消" style="@style/balck_text16" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/contact_ok" android:layout_margin="10dp" style="@style/balck_text16" android:background="@color/white" android:layout_weight="1" android:text="确定" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </RelativeLayout> </RelativeLayout>