公司的项目中几乎都会有走工作流这个环节,为了提高效率,现在特意把弹出的填写审批意见PopWindow改转成工具类,提高效率,免得下次又得整。先看运行效果。

           

现在先看下PopWindowHelper,备注写的非常清楚啦。

package com.example.keranbin.myapplication;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
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.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

/**
 * Created by keranbin on 2016/7/18.
 */
public class PopWindowHelper implements View.OnClickListener {
    private PopupWindow popupWindow;
    private PopWindowListener popWindowListener;

    private Button btnSure;
    private Button btnCancel;
    private EditText etOpnion;


    /*
    * layout 自定义的popWindow样式
    * sureId 自定义的popWindow确定键Id
    * cancleId 自定义的popWindow取消键Id
    * approvalMsg 自定义的popWindow审批意见文本框Id
    * strHint自定义的popWindow审批意见文本框的提示信息
    * */
    public void createApprovalPopWindow(Activity context, View parent, final Window window, int layout, int sureId, int cancleId, int approvalMsg, String strHint){
        final View view =LayoutInflater.from(context).inflate(layout,null);
        btnSure= (Button) view.findViewById(sureId);
        btnSure.setOnClickListener(this);
        btnCancel= (Button) view.findViewById(cancleId);
        btnSure.setOnClickListener(this);
        etOpnion= (EditText) view.findViewById(approvalMsg);
        etOpnion.setHint(strHint);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(params);
        popupWindow= new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        //设置PopupWindow的View
        popupWindow.setContentView(view);
        //设置PopupWindow的View弹出窗体的宽
        popupWindow.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
        //设置PopupWindow的View弹出窗体的高
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置PopupWindow的View弹出窗体可点击,如果不添加这个属性,那么点击EditText无法弹出输入法
        popupWindow.setFocusable(true);
        //设置PopupWindow去除边际黑线
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //避免输入法覆盖掉popWindow
        popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
     //设置PopupWindow的显示位置 popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0); setWindowGray(window); //popWindow消失后,还原页面背景 popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { setWindowGray(window); } }); view.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { int height = view.findViewById(R.id.ll_layout_xxtx_spyj).getTop(); int y = (int) event.getY(); if (event.getAction() == MotionEvent.ACTION_UP) { if (y < height) { popWindowListener.popWindowTouch(popupWindow); } } return true; } }); } public void setWindowGray(Window window) { WindowManager.LayoutParams lp = window.getAttributes(); if (popupWindow.isShowing()) { lp.alpha = 0.5f; window.setAttributes(lp); } else { lp.alpha = 1.0f; window.setAttributes(lp); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_approval_sure: popWindowListener.getOpnion(etOpnion.getText().toString().trim(),popupWindow); break; case R.id.btn_approval_cancel: popupWindow.dismiss(); break; } } public void setPopWindowListener(PopWindowListener popWindowListener) { this.popWindowListener = popWindowListener; } public interface PopWindowListener { //回调用户填写的审批意见strOpnion到Activity中,用户得到可进行下一步操作 public void getOpnion(String strOpnion,PopupWindow popupWindow); public void popWindowTouch(PopupWindow popupWindow); } }

 再看下在MainActivity中是怎么调用的。

package com.example.keranbin.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.Toast;

public class MainActivity extends Activity implements PopWindowHelper.PopWindowListener {
    private PopWindowHelper popWindowHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        popWindowHelper = new PopWindowHelper();
        popWindowHelper.setPopWindowListener(this);
        findViewById(R.id.btnRight).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popWindowHelper.createApprovalPopWindow(
                        MainActivity.this,
                        MainActivity.this.findViewById(R.id.rl),
                        MainActivity.this.getWindow(),
                        R.layout.layout_popwindow_approval,
                        R.id.btn_approval_sure,
                        R.id.btn_approval_cancel,
                        R.id.et_layout_approval,
                        "请您填写审批意见"
                );
            }
        });
    }


    @Override
    public void getOpnion(String strOpnion,PopupWindow popupWindow) {
        if (!strOpnion.equals("")){
            popupWindow.dismiss();
            Toast.makeText(MainActivity.this, "您填写的审批意见为" + strOpnion, Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(MainActivity.this,"请填写审批意见",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void popWindowTouch(PopupWindow popupWindow) {
        popupWindow.dismiss();
    }
}

 自定义的popwindow样式xml文件代码如下

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:background="#999999"
 6     android:orientation="vertical">
 7 
 8     <LinearLayout
 9         android:id="@+id/ll_layout_xxtx_spyj"
10         android:layout_width="match_parent"
11         android:layout_height="match_parent"
12         android:orientation="vertical">
13 
14         <EditText
15             android:id="@+id/et_layout_approval"
16             android:layout_width="match_parent"
17             android:layout_height="200dip"
18             android:layout_marginLeft="25dip"
19             android:layout_marginRight="25dip"
20             android:layout_marginTop="25dip"
21             android:background="@drawable/spyj_popwindow"
22             android:gravity="left"
23             android:paddingBottom="2dip"
24             android:paddingLeft="2dip"
25             android:paddingRight="2dip"
26             android:paddingTop="2dip" />
27 
28         <RelativeLayout
29             android:layout_width="match_parent"
30             android:layout_height="60dip"
31             android:layout_alignParentBottom="true"
32             android:layout_marginTop="5dip"
33             android:background="#999999"
34             android:gravity="center_vertical">
35 
36             <Button
37                 android:id="@+id/btn_approval_cancel"
38                 android:layout_width="120dip"
39                 android:layout_height="40dip"
40                 android:layout_alignParentLeft="true"
41                 android:layout_marginLeft="20dip"
42                 android:background="@drawable/orange_click_selector"
43                 android:text="取消"
44                 android:textColor="#FFF"
45                 android:textSize="18sp" />
46 
47             <Button
48                 android:id="@+id/btn_approval_sure"
49                 android:layout_width="120dip"
50                 android:layout_height="40dip"
51                 android:layout_alignParentRight="true"
52                 android:layout_marginRight="20dip"
53                 android:background="@drawable/blue_click_selector"
54                 android:text="确定"
55                 android:textColor="#FFF"
56                 android:textSize="18sp" />
57         </RelativeLayout>
58 
59     </LinearLayout>
60 
61 </LinearLayout>