问题:PopupWindow中,EditText无法使用复制粘贴功能。
方法:使用DialogFragment实现。
package com.example.administrator.dialogfragmentdemo;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by qhm on 2018/10/29.
* Blog : https://blog..net/qq_37077360
*
* DialogFragment取代PopupWindow,解决PopupWindow中EditText无法复制粘贴问题
*/
public class EditDialog extends DialogFragment {
private View mLayout;
Context mContext;
private EditText et_content;
CommonInterface listener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置样式、传值等。
// setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Holo_Light_DialogWhenLarge_NoActionBar);
setStyle(STYLE_NO_TITLE, R.style.dialog_edit);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
this.mContext = getActivity();
mLayout = inflater.inflate(R.layout.pop_send_comment, container);
et_content = (EditText) mLayout.findViewById(R.id.et_content);
TextView tv_send=(TextView) mLayout.findViewById(R.id.tv_send) ;//我知道了
final Window window = getDialog().getWindow();
//设置dialog的位置
window.setGravity(Gravity.BOTTOM);
//去除白色背景【必须设置,否则宽高设置等可能不生效】。style中已通过windowBackground设置
// window.setBackgroundDrawable(new BitmapDrawable());
//去除原来的标题。style中已通过windowNoTitle设置
// getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
// 自动显示软键盘。【方法一】
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
getDialog().setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
//设置宽度占满全屏。必须在show()之后设置,此处已监听show();或者在其生命周期onstart()或者onResume()中设置
WindowManager.LayoutParams attributes = window.getAttributes();
attributes.width= ViewGroup.LayoutParams.MATCH_PARENT;
window.setAttributes(attributes);
// //获取焦点,并自动弹出键盘【方法二】
// InputMethodManager inputmanger = (InputMethodManager) mContext
// .getSystemService(Context.INPUT_METHOD_SERVICE);
// inputmanger.showSoftInput(et_content, 0);
}
});
//设置外部点击可以取消
getDialog().setCancelable(true);
getDialog().setCanceledOnTouchOutside(true);
tv_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(TextUtils.isEmpty(et_content.getText().toString())){
Toast.makeText(mContext,"说点什么吧",Toast.LENGTH_SHORT).show();
return;
}
// dismiss();
//调用接口发表评论。当后台返回success,再调用dismiss()关闭
if (listener!=null){
listener.operate(et_content.getText().toString().trim());
}
}
});
return mLayout;
}
public void setOnSendingListener(CommonInterface listener){
this.listener=listener;
}
}
style.xml文件:
adjustPan
@null
true
false
true
@null
最终使用:
dialog = new EditDialog();
dialog.setOnSendingListener(new CommonInterface() {
@Override
public void operate(Object object) {
//todo 保存评论,成功则关闭对话框
if (1==1)//测试
dialog.dismiss();
}
});
FragmentManager fm=getSupportFragmentManager();
dialog.show(fm,"tag");