直接上代码
//点击发表,内容不为空时的回调 public SendBackListener sendBackListener; public interface SendBackListener { void sendBack(String inputText); } private ProgressDialog progressDialog; private String texthint; private Dialog dialog; private EditText inputDlg; private int numconut = 300; private String tag = null; public KeyMapDailog() { } @SuppressLint("ValidFragment") public KeyMapDailog(String texthint, SendBackListener sendBackListener) {//提示文字 this.texthint = texthint; this.sendBackListener = sendBackListener; } public Dialog onCreateDialog(Bundle savedInstanceState) { // 使用不带Theme的构造器, 获得的dialog边框距离屏幕仍有几毫米的缝隙。 dialog = new Dialog(getActivity(), R.style.inputDialog); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定 // getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); View contentview = View.inflate(getActivity(), R.layout.comment_dialog_layout, null); dialog.setContentView(contentview); dialog.setCanceledOnTouchOutside(true); // 外部点击取消 // 设置宽度为屏宽, 靠近屏幕底部。 Window window = dialog.getWindow(); WindowManager.LayoutParams lp = window.getAttributes(); lp.gravity = Gravity.BOTTOM; // 紧贴底部 lp.alpha = 1; //设置透明度0.0f全透明 lp.dimAmount = 0.0f; lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平 window.setAttributes(lp); window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); inputDlg = (EditText) contentview.findViewById(R.id.dialog_comment_content); inputDlg.setHint(texthint); final TextView tv_send = (TextView) contentview.findViewById(R.id.dialog_comment_send); inputDlg.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { } }); tv_send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (TextUtils.isEmpty(inputDlg.getText().toString())) { Toast.makeText(getActivity(), "输入内容为空", Toast.LENGTH_LONG).show(); return; } else { //出现加载画面 //progressDialog = new ProgressDialog(getActivity()); //progressDialog.setCanceledOnTouchOutside(false); //progressDialog.show(); sendBackListener.sendBack(inputDlg.getText().toString()); } } }); inputDlg.setFocusable(true); inputDlg.setFocusableInTouchMode(true); inputDlg.requestFocus(); //hideSoftkeyboard(); //为Window设置动画 window.setWindowAnimations(R.style.CustomDialog); return dialog; } public void hideProgressdialog() { progressDialog.cancel(); } //这个方法可以实现输入法在窗口上切换显示,如果输入法在窗口上已经显示,则隐藏,如果隐藏,则显示输入法到窗口上 public void hideSoftkeyboard() { InputMethodManager m = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); }
在anim中,设置动画
pop_in
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromYDelta="100%p" android:toYDelta="0%p" /> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" /> </set>
pop_out
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="300" android:fromYDelta="0%p" android:toYDelta="100%p" /> </set>
需要给dialog设置样式
<style name="inputDialog" parent="@android:style/Theme.Holo.Light.Dialog"> <item name="android:windowBackground">@color/common_white</item> <!--背景--> <item name="android:windowFrame">@null</item> <!--设置无边框--> <item name="android:windowNoTitle">true</item> <!-- 无标题 --> <item name="android:backgroundDimEnabled">false</item> <!-- 模糊 --> <item name="android:windowSoftInputMode">stateAlwaysVisible</item> <!--显示软件盘--> </style>
加一个动画style
<style name="CustomDialog"> <item name="android:windowEnterAnimation">@anim/pop_in</item> <item name="android:windowExitAnimation">@anim/pop_out</item> </style>
ok,一个完整的DialogFragment的编辑框就完成了
完整的demo,https://download.csdn.net/download/qq_30711091/10522274
希望大家可以互相学习