安卓开发:模仿微信,QQ评论输入框,使用PopupWindow完美实现(新版)
2017-08-29 16:05
916 查看
首先说一下实现的效果,点击外部消失,有个缺点,键盘弹出时,点击back不会消失,第二次点击back或者点外部才可以消失;
我的直播项目里用的,有“弹幕”按钮(已隐藏),上效果
布局左边是有弹幕按钮的,隐藏了,有需要打开就可以了
上布局代码:
重写控件继承自PopupWindow
写一个回调来处理点击发送时的操作
声明控件
构造方法
开放出一个方法供外部调用
源码附上
http://download.csdn.net/download/ximen_qing/9955633
我的直播项目里用的,有“弹幕”按钮(已隐藏),上效果
布局左边是有弹幕按钮的,隐藏了,有需要打开就可以了
上布局代码:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/popup_comment_parent" android:layout_width="match_parent" android:layout_height="wrap_content" android:alpha="0.9" android:background="@color/white"> <!-- 这是标记是否是弹幕 --> <CheckBox android:id="@+id/popup_comment_bull_cb" android:layout_width="24dp" android:layout_height="24dp" android:layout_centerVertical="true" android:layout_marginLeft="6dp" android:layout_marginRight="6dp" android:background="@drawable/tm_selector_bull" android:button="@null" android:checked="false" android:visibility="gone" /> <View android:layout_width="1px" android:layout_height="34dp" android:layout_centerVertical="true" android:layout_toRightOf="@+id/popup_comment_bull_cb" android:background="@color/tm_titlebar_div_line" android:visibility="gone" /> <EditText android:id="@+id/popup_comment_edt" android:layout_width="match_parent" android:layout_height="44dp" android:layout_marginLeft="4dp" android:layout_marginRight="4dp" android:layout_toLeftOf="@+id/popup_comment_send_tv" android:layout_toRightOf="@+id/popup_comment_bull_cb" android:background="@null" android:gravity="center_vertical" android:hint="说点什么吧" android:imeOptions="actionNone" android:maxLength="30" android:paddingLeft="6dp" android:paddingRight="6dp" android:singleLine="true" android:textColor="@color/tm_black_tv" android:textColorHint="@color/tm_blue_dark" android:textCursorDrawable="@drawable/text_cursor_cyan" android:textSize="15dp" /> <View android:layout_width="1px" android:layout_height="34dp" android:layout_centerVertical="true" android:layout_toLeftOf="@+id/popup_comment_send_tv" android:background="@color/tm_titlebar_div_line" /> <TextView android:id="@+id/popup_comment_send_tv" android:layout_width="60dp" android:layout_height="44dp" android:layout_alignParentRight="true" android:gravity="center" android:text="发送" android:textColor="@color/tm_blue" android:textSize="16dp" /> </RelativeLayout>
重写控件继承自PopupWindow
class CommentPopupWindow extends PopupWindow
写一个回调来处理点击发送时的操作
public interface LiveCommentSendClick { //第二个参数标记是不是弹幕,第三个是评论内容 void onSendClick(View view, boolean isBull, String comment); }
声明控件
//点击“发送”回调 private LiveCommentSendClick sendClick; private View contentView; private CheckBox popupCommentBullCb; private EditText popupCommentEdt; private TextView popupCommentSendTv;
构造方法
//构造方法 public CommentPopupWindow(FragmentActivity context, LiveCommentSendClick sendClick) { super(context); this.context = context; this.sendClick = sendClick; foundPopup(); } private void foundPopup() { contentView = View.inflate(context, R.layout.popup_comment_input, null); popupCommentParent = (RelativeLayout) contentView.findViewById(R.id.popup_comment_parent); popupCommentBullCb = (CheckBox) contentView.findViewById(R.id.popup_comment_bull_cb); popupCommentEdt = (EditText) contentView.findViewById(R.id.popup_comment_edt); popupCommentSendTv = (TextView) contentView.findViewById(R.id.popup_comment_send_tv); setContentView(contentView); setWidth(ViewGroup.LayoutParams.MATCH_PARENT); setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); //设置弹出动画 setAnimationStyle(R.style.popWindow_animation_in2out); //使其聚集 ,要想监听菜单里控件的事件就必须要调用此方法 setFocusable(true); //设置允许在外点击消失 setOutsideTouchable(true); //这个是为了点击“Back”也能使其消失,不会影响背景 setBackgroundDrawable(new BitmapDrawable()); //显示在键盘上方 setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); popupCommentSendTv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String result = popupCommentEdt.getText().toString().trim(); if (result.length() <= 0) { Toast.makeText(context, "还没有填写任何内容哦!", Toast.LENGTH_SHORT).show(); } else { //第二个参数标记是否是弹幕 sendClick.onSendClick(v, popupCommentBullCb.isChecked(), result); dismiss(); } } }); setOnDismissListener(new OnDismissListener() { @Override public void onDismiss() { AppUtils.hideSoftInput(context, popupCommentEdt.getWindowToken()); } }); }
开放出一个方法供外部调用
public void showReveal() { if (contentView == null) { Toast.makeText(context, "创建失败", Toast.LENGTH_SHORT).show(); } else { //延时显示软键盘 new Timer().schedule(new TimerTask() { @Override public void run() { AppUtils.showKeyboard(popupCommentEdt); } }, 200); //显示并设置位置 showAtLocation(contentView, Gravity.BOTTOM, 0, 0); } }
源码附上
http://download.csdn.net/download/ximen_qing/9955633
相关文章推荐
- 安卓开发:模仿微信,QQ评论输入框,使用PopupWindow完美实现
- 安卓开发-最简单快速的仿微信聊天实现-附赠微信原生表情,QQ原生表情
- 安卓开发:自定义PopupWindow,实现模仿iOS底部弹出菜单
- Android开发之使用SharedPreferences实现QQ登陆的选项框记忆功能(源代码分享)
- Android开发技巧——使用PopupWindow实现弹出菜单
- 安卓开发笔记——多种方式实现底部菜单栏(仿微信界面)
- Android开发之自定义控件-模仿QQ输入框
- 【android开发】使用PopupWindow实现页面点击顶部弹出下拉菜单
- C#开发微信门户及应用(28)--微信“摇一摇·周边”功能的使用和接口的实现
- android 自定义listview实现仿微信/QQ设置界面的开发
- 使用Spannablestring和ClickableSpan实现仿手机qq评论点击效果
- Android开发模仿微信发表朋友圈图片多选及图片浏览实现-Assur
- 使用Mina框架开发 QQ Android 客户端(3) 登陆功能的实现
- 安卓开发笔记——多种方式实现底部菜单栏(仿微信界面)
- ios开发——仿新版QQ侧滑导航栏的实现
- 安卓开发中Tab标签页的使用——TabHo… 分类: Android开发 2014-05-30 10:57 85人阅读 评论(0) 收藏
- 关于安卓开发实现拖动条和星级评论
- 关于安卓开发实现底部菜单栏(已过时做法,不建议使用)
- 安卓开发20:动画之Animation 详细使用-主要通过java代码实现动画效果
- 安卓开发20:动画之Animation 详细使用-主要通过java代码实现动画效果