Android中解决EditText放到popupWindow中,原有复制、粘贴、全选、选择功能失效问题
程序员文章站
2024-02-29 15:03:28
1、原来是将editview放到了popupwindow,发现editview原有的复制、粘贴、全选、选择功能失效了,所以便用dialogfragment代替了popupw...
1、原来是将editview放到了popupwindow,发现editview原有的复制、粘贴、全选、选择功能失效了,所以便用dialogfragment代替了popupwindow
直接上代码
①、先看布局文件
<?xml version="." encoding="utf-"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="bottom" android:orientation="vertical"> <linearlayout android:id="@+id/ll_background_dialog" android:layout_width="match_parent" android:layout_height="dp" android:layout_weight="" android:background="#" android:alpha="." android:orientation="horizontal"> </linearlayout> <scrollview android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/white"> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffcdcdcd" android:orientation="vertical"> <relativelayout android:layout_width="match_parent" android:layout_height="wrap_content"> <imageview android:id="@+id/iv_quxiao_popup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="dp" android:src="@drawable/popup_comment_no" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerinparent="true" android:text="发言" android:textcolor="#" android:textsize="sp" /> <imageview android:id="@+id/iv_write_popup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentend="true" android:layout_alignparentright="true" android:padding="dp" android:src="@drawable/popup_commnet_ok" /> </relativelayout> <edittext android:id="@+id/et_comment_popup" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="dp" android:background="#ffffff" android:gravity="top" android:hint="在这里留言" android:minlines="" /> <relativelayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginbottom="dp" android:layout_marginleft="dp" android:layout_marginright="dp"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:text="文明上网" android:textsize="sp" /> </relativelayout> </linearlayout> </scrollview> </linearlayout>
②、看自定义diaglogfragment的代码
import android.annotation.suppresslint; import android.content.context; import android.graphics.color; import android.graphics.drawable.colordrawable; import android.os.bundle; import android.app.fragment; import android.support.v.app.dialogfragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.view.windowmanager; import android.view.inputmethod.inputmethodmanager; import android.widget.edittext; import android.widget.imageview; import android.widget.linearlayout; import android.widget.toast; import com.android.volley.authfailureerror; import com.android.volley.request; import com.android.volley.response; import com.android.volley.volleyerror; import com.android.volley.toolbox.stringrequest; import org.json.jsonexception; import org.json.jsonobject; import java.util.hashmap; import java.util.map; import newairtek.com.app.appapplication; import newairtek.com.sdnewsandroid.r; import newairtek.com.url.sdnewsurl; /** * a simple {@link fragment} subclass. */ @suppresslint("validfragment") public class customdialogfragment extends dialogfragment { private imageview iv_quxiao_popup;//取消按钮 private imageview iv_write_popup; //确认按钮 private edittext et_comment_popup;//评论内容 private linearlayout ll_background_dialog;//容器 private boolean iscommenting = false; private string uid; private string id; private string cid; public customdialogfragment(string uid, string id, string cid) { this.uid = uid; this.id = id; this.cid = cid; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //这句代码的意思是让dialogfragment弹出时沾满全屏 setstyle(dialogfragment.style_normal, android.r.style.theme_holo_light_dialogwhenlarge_noactionbar); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.popup_write_comment, null); //让dialogfragment的背景为透明 getdialog().getwindow().setbackgrounddrawable(new colordrawable(color.transparent)); initview(view); initevent(); return view; } //初始化view private void initview(view view) { iv_quxiao_popup = (imageview) view.findviewbyid(r.id.iv_quxiao_popup); iv_write_popup = (imageview) view.findviewbyid(r.id.iv_write_popup); et_comment_popup = (edittext) view.findviewbyid(r.id.et_comment_popup); ll_background_dialog = (linearlayout) view.findviewbyid(r.id.ll_background_dialog); } private void initevent(){ //取消 iv_quxiao_popup.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { dismiss(); } }); //确认发送 iv_write_popup.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { if (et_comment_popup.gettext().tostring().length() > ) { if (!iscommenting) { iscommenting = true; } else { toast.maketext(getactivity(), "正在评论,请勿重复操作", toast.length_long).show(); } } else { toast.maketext(getactivity(), "内容不能为空", toast.length_short).show(); } } }); ll_background_dialog.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { dismiss(); } }); } }
3、如何使用
fragmentmanager manager = getsupportfragmentmanager();//区分是v的fragment还是app包里面的 customdialogfragment dialogfragment = new customdialogfragment(uid, id, cid); dialogfragment.show(manager, "custom");
效果图
上一篇: php版微信发红包接口用法示例