解決這個問題,須要使用到DialogFragmentjava
下面這個代碼繼承 DialogFragmentandroid
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); } }
下面是ide
R.sytle.dialog_fragment
@drawable/popupwindowfragmentbackgroud@nulltrue@style/AnimationDialogFragment@anim/fadein_from@anim/fadeout_to
其中佈局
popupwindowfragmentbackgroud
的值是#00FFFFFF透明白色this
下面是:fadein_from文件spa
<?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文件debug
<?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>
下面是佈局文件:code
<?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
xml
() { [] 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問題繼承