DialogFragment 代替PopupWindows的用法

解决这个问题,需要使用到DialogFragment

下面这个代码继承 DialogFragment

public class PopupWindowDialogFragment extends DialogFragment implements View.OnTouchListener{
    private static final java.lang.String TAG = "PopupWindowDialogFragment";
    private Context mContext;
    public static final String LAYOUTID = "resourceLayoutId";
    public static final String VIEWIDS = "viewIds";
    LinearLayout popContent;
    GetViewsListener listener;
    RelativeLayout contentOutside;
    public static PopupWindowDialogFragment newInstance(int resourceLayoutId,int[] viewIds){
        PopupWindowDialogFragment fragment = new PopupWindowDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(LAYOUTID, resourceLayoutId);
        bundle.putIntArray(VIEWIDS,viewIds);
        fragment.setArguments(bundle);
        return fragment;
    }
    public void setListener(GetViewsListener listener){
        this.listener = listener;
    }
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mContext =activity;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.dialog_fragment);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        LogUtils.debug(TAG, "onCreateView....... ");
//        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);//取消标题
        View view = inflater.inflate(getArguments().getInt(LAYOUTID), null);
        popContent = (LinearLayout)view.findViewById(R.id.pop_content);
        contentOutside = (RelativeLayout)view.findViewById(R.id.content_outside);
        contentOutside.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        LogUtils.debug(TAG,"onCreateView view = "+view);
        int[] ids = getArguments().getIntArray(VIEWIDS);
        List<View> views = new ArrayList<>();
        for(int i=0;i<ids.length;i++){
            views.add(view.findViewById(ids[i]));
        }

        if(listener!=null){
            listener.getViews(views);
        }
        LogUtils.debug(TAG,"getDialog() = "+getDialog());
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        return view;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        LogUtils.debug(TAG,"before onTouch popContent.getTop()="+popContent.getTop()+" event.getRawY() ="+event.getRawY());
        if(event.getRawY()<popContent.getTop()){
            LogUtils.debug(TAG,"onTouch popContent.getTop()="+popContent.getTop()+" event.getRawY() ="+event.getRawY());
            dismiss();
        }
        return false;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
    }
    public interface GetViewsListener{
        void getViews(List<View> views);
    }
}

下面是

R.sytle.dialog_fragment
@drawable/popupwindowfragmentbackgroud@nulltrue@style/AnimationDialogFragment@anim/fadein_from@anim/fadeout_to

其中

popupwindowfragmentbackgroud

的值是#00FFFFFF透明白色

下面是:fadein_from文件

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" >

   <alpha
           android:duration="100"
           android:fromAlpha="0.0"
           android:interpolator="@android:anim/accelerate_interpolator"
           android:toAlpha="1.0" />
</set>

下面是fadeout_to文件

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" >

   <alpha
           android:duration="100"
           android:fromAlpha="1.0"
           android:interpolator="@android:anim/accelerate_interpolator"
           android:toAlpha="0.0" />
</set>

下面是布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
               android:layout_width="match_parent"
               android:layout_height="match_parent">
   <RelativeLayout android:layout_width="match_parent"
                   android:layout_height="match_parent"
                   android:id="@+id/content_outside"
                   >

   </RelativeLayout>
   <LinearLayout
           android:id="@+id/pop_content"
           android:layout_width="match_parent"
           android:layout_height="45dp"
           android:focusable="true"
           android:focusableInTouchMode="true"
           android:orientation="vertical"
           android:layout_alignParentBottom="true"
           android:background="@color/transparent"
           >

       <LinearLayout
               android:layout_width="fill_parent"
               android:layout_height="match_parent"
               android:background="@color/bg_color"
               android:orientation="horizontal"
               android:paddingLeft="10dp"
               android:paddingRight="10dp">
           <EditText
                   android:id="@+id/content"
                   android:layout_width="0dp"
                   android:layout_height="32dp"
                   android:layout_gravity="center_vertical"
                   android:layout_marginLeft="10dp"
                   android:layout_marginRight="10dp"
                   android:layout_weight="1.0"
                   android:background="@drawable/note_replay_edittext_bg"
                   android:hint="我也说一句..."
                   android:paddingLeft="10dp"
                   android:textColorHint="@color/disabled_font_color"
                   android:longClickable="true"
                   android:focusable="true"
                   android:textSize="12dp"/>

           <Button
                   android:id="@+id/send_btn"
                   android:layout_width="50dp"
                   android:layout_height="32dp"
                   android:layout_gravity="center_vertical"
                   android:background="@drawable/note_replay_edittext_bg"
                   android:text="发送"
                   android:textColor="@color/primary_font_color"
                   android:textSize="14dp"/>
       </LinearLayout>
   </LinearLayout>
</RelativeLayout>

在Activity中调用DialogFragment

() {
    [] ids = {R.id.R.id.}= tag=PopupWindowDialogFragment.(R.layout.ids).setListener(PopupWindowDialogFragment.GetViewsListener() {
        (List<View> views) {
            (View view : views) {
                (view.getId()) {
                    R.id.:
                        view.setOnClickListener(OnNotDoubleClickListener() {
                            (View v) {
                                (!=) {
                                    String text = .getText().toString().trim()(!TextUtils.(text) && text.length() >= ) {
                                        showTipMsg(getString(R.string.))}
                                    (!TextUtils.(text)) {
                                        (CommonUtils.()) {
                                            ReplyPostTask().execute(text)} {
                                            showTipMsg(getString(R.string.))}
                                    } {
                                        showTipMsg(getString(R.string.))}
                                }
                            }
                        })R.id.:
                        = (EditText) view.requestFocus()
                        }
            }
        }
    })LogUtils.(+ floor).show(getSupportFragmentManager())}


用一个相对布局来代表输入框外面的区域,当点击外部区域的时候,便dismiss();   这样在消失的效果上有点像popupWindow,同时能支持系统自带的复制粘贴功能,达到了预期效果,有问题请发送邮箱:104234636@qq.com,欢迎探讨android问题



转载于:https://my.oschina.net/u/195533/blog/539751

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值