1、原來是將EditView放到了popupwindow,發現EditView原有的複製、粘貼、全選、選擇功能失效了,所以便用DialogFragment代替了popupWindow 直接上代碼 ①、先看佈局文件 ②、看自定義diaglogFragment的代碼 3、如何使用 效果圖 ...
1、原來是將EditView放到了popupwindow,發現EditView原有的複製、粘貼、全選、選擇功能失效了,所以便用DialogFragment代替了popupWindow
直接上代碼
①、先看佈局文件
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:gravity="bottom" 6 android:orientation="vertical"> 7 <LinearLayout 8 android:id="@+id/ll_background_dialog" 9 android:layout_width="match_parent" 10 android:layout_height="0dp" 11 android:layout_weight="1" 12 android:background="#000" 13 android:alpha="0.7" 14 android:orientation="horizontal"> 15 16 </LinearLayout> 17 <ScrollView 18 android:layout_width="match_parent" 19 android:layout_height="wrap_content" 20 android:background="@android:color/white"> 21 22 <LinearLayout 23 android:layout_width="match_parent" 24 android:layout_height="wrap_content" 25 android:background="#ffcdcdcd" 26 android:orientation="vertical"> 27 28 <RelativeLayout 29 android:layout_width="match_parent" 30 android:layout_height="wrap_content"> 31 32 <ImageView 33 android:id="@+id/iv_quxiao_popup" 34 android:layout_width="wrap_content" 35 android:layout_height="wrap_content" 36 android:padding="16dp" 37 android:src="@drawable/popup_comment_no" /> 38 39 <TextView 40 android:layout_width="wrap_content" 41 android:layout_height="wrap_content" 42 android:layout_centerInParent="true" 43 android:text="發言" 44 android:textColor="#000" 45 android:textSize="16sp" /> 46 47 <ImageView 48 android:id="@+id/iv_write_popup" 49 android:layout_width="wrap_content" 50 android:layout_height="wrap_content" 51 android:layout_alignParentEnd="true" 52 android:layout_alignParentRight="true" 53 android:padding="16dp" 54 android:src="@drawable/popup_commnet_ok" /> 55 </RelativeLayout> 56 57 <EditText 58 android:id="@+id/et_comment_popup" 59 android:layout_width="match_parent" 60 android:layout_height="wrap_content" 61 android:layout_margin="16dp" 62 android:background="#ffffff" 63 android:gravity="top" 64 android:hint="在這裡留言" 65 android:minLines="3" /> 66 67 <RelativeLayout 68 android:layout_width="match_parent" 69 android:layout_height="wrap_content" 70 android:layout_marginBottom="16dp" 71 android:layout_marginLeft="16dp" 72 android:layout_marginRight="16dp"> 73 74 <TextView 75 android:layout_width="wrap_content" 76 android:layout_height="wrap_content" 77 android:layout_alignParentRight="true" 78 android:text="文明上網" 79 android:textSize="12sp" /> 80 </RelativeLayout> 81 </LinearLayout> 82 83 </ScrollView> 84 85 </LinearLayout>
②、看自定義diaglogFragment的代碼
1 import android.annotation.SuppressLint; 2 import android.content.Context; 3 import android.graphics.Color; 4 import android.graphics.drawable.ColorDrawable; 5 import android.os.Bundle; 6 import android.app.Fragment; 7 import android.support.v4.app.DialogFragment; 8 import android.view.LayoutInflater; 9 import android.view.View; 10 import android.view.ViewGroup; 11 import android.view.WindowManager; 12 import android.view.inputmethod.InputMethodManager; 13 import android.widget.EditText; 14 import android.widget.ImageView; 15 import android.widget.LinearLayout; 16 import android.widget.Toast; 17 18 import com.android.volley.AuthFailureError; 19 import com.android.volley.Request; 20 import com.android.volley.Response; 21 import com.android.volley.VolleyError; 22 import com.android.volley.toolbox.StringRequest; 23 24 import org.json.JSONException; 25 import org.json.JSONObject; 26 27 import java.util.HashMap; 28 import java.util.Map; 29 30 import newairtek.com.app.AppApplication; 31 import newairtek.com.sdnewsandroid.R; 32 import newairtek.com.url.SdNewsUrl; 33 34 /** 35 * A simple {@link Fragment} subclass. 36 */ 37 @SuppressLint("ValidFragment") 38 public class CustomDialogFragment extends DialogFragment { 39 40 private ImageView iv_quxiao_popup;//取消按鈕 41 private ImageView iv_write_popup; //確認按鈕 42 private EditText et_comment_popup;//評論內容 43 private LinearLayout ll_background_dialog;//容器 44 45 private boolean isCommenting = false; 46 47 private String uid; 48 private String id; 49 private String cid; 50 51 public CustomDialogFragment(String uid, String id, String cid) { 52 this.uid = uid; 53 this.id = id; 54 this.cid = cid; 55 } 56 57 @Override 58 public void onCreate(Bundle savedInstanceState) { 59 super.onCreate(savedInstanceState); 60 //這句代碼的意思是讓dialogFragment彈出時沾滿全屏 61 setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Holo_Light_DialogWhenLarge_NoActionBar); 62 } 63 64 @Override 65 public View onCreateView(LayoutInflater inflater, ViewGroup container, 66 Bundle savedInstanceState) { 67 View view = inflater.inflate(R.layout.popup_write_comment, null); 68 //讓DialogFragment的背景為透明 69 getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 70 initView(view); 71 initEvent(); 72 return view; 73 } 74 75 //初始化view 76 private void initView(View view) { 77 iv_quxiao_popup = (ImageView) view.findViewById(R.id.iv_quxiao_popup); 78 iv_write_popup = (ImageView) view.findViewById(R.id.iv_write_popup); 79 et_comment_popup = (EditText) view.findViewById(R.id.et_comment_popup); 80 ll_background_dialog = (LinearLayout) view.findViewById(R.id.ll_background_dialog); 81 } 82 83 private void initEvent(){ 84 //取消 85 iv_quxiao_popup.setOnClickListener(new View.OnClickListener() { 86 @Override 87 public void onClick(View view) { 88 dismiss(); 89 } 90 }); 91 //確認發送 92 iv_write_popup.setOnClickListener(new View.OnClickListener() { 93 @Override 94 public void onClick(View view) { 95 if (et_comment_popup.getText().toString().length() > 1) { 96 if (!isCommenting) { 97 isCommenting = true; 98 99 } else { 100 Toast.makeText(getActivity(), "正在評論,請勿重覆操作", Toast.LENGTH_LONG).show(); 101 } 102 } else { 103 Toast.makeText(getActivity(), "內容不能為空", Toast.LENGTH_SHORT).show(); 104 } 105 } 106 }); 107 ll_background_dialog.setOnClickListener(new View.OnClickListener() { 108 @Override 109 public void onClick(View view) { 110 dismiss(); 111 } 112 }); 113 } 114 115 116 117 118 119 }
3、如何使用
1 FragmentManager manager = getSupportFragmentManager();//區分是v4的Fragment還是app包裡面的 2 CustomDialogFragment dialogFragment = new CustomDialogFragment(uid, id, cid); 3 dialogFragment.show(manager, "custom");
效果圖