Android 仿今日头条评论输入框
来源:互联网 发布:办公软件2007 编辑:程序博客网 时间:2024/06/05 16:34
相信不少小伙伴,在做评论功能的时候,都被输入框恶心过。
产品总会要求,自己的产品输入框要有自己特色。
既然如此,那就自己写一个呗。
先上效果图:
做一个输入框dialog很难吗?可能有些接触android不久的小小伙伴会觉得,难以入手。那我们就拆成几部来做,其实很简单。
第一步: 写弹出框样式布局
布局就一个EditText和一个TextView,做了下背景的圆角和描边处理,很快我们就可以撸出来了。在自定义View的时候,将这个布局inflate出来。
第二步 软键盘顶起dialog
可能有的手机什么都不设置,软键盘就会把dialog弹起,有的手机则会遮住dialog布局。
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
详情参考 谷哥的小弟 探索Android软键盘的疑难杂症http://blog.csdn.net/lfdfhl/article/details/52415390
第三步 布局做好了,你的软键盘也确定在挡住dialog位置的时候,会把dialog弹起,那剩下的事,就是讲dialog置于屏幕底部,点击需要评论的时候,showDialog,然后EditText聚焦,软键盘弹出讲dialog顶起,自然我们想要的效果就出来了
自定义评论框Dialog代码
import android.annotation.SuppressLint;import android.app.Dialog;import android.app.ProgressDialog;import android.content.Context;import android.content.DialogInterface;import android.os.Bundle;import android.os.Handler;import android.support.v4.app.DialogFragment;import android.text.Editable;import android.text.TextUtils;import android.text.TextWatcher;import android.view.Gravity;import android.view.View;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;public class KeyMapDailog extends DialogFragment { //点击发表,内容不为空时的回调 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.BottomDialog); 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; lp.dimAmount = 0.5f; 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) { if (s.length() > 0) { tv_send.setBackgroundResource(R.drawable.corners_review_cansend); } else { tv_send.setBackgroundResource(R.drawable.corners_review_send); } } }); tv_send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (TextUtils.isEmpty(inputDlg.getText().toString())) {// ToastUtils.getInstance().showToast("输入内容为空奥"); 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(); final Handler hanler = new Handler(); dialog.setOnDismissListener(new DialogInterface.OnDismissListener() { public InputMethodManager mInputMethodManager; @Override public void onDismiss(DialogInterface dialog) { hanler.postDelayed(new Runnable() { @Override public void run() { hideSoftkeyboard(); } }, 200); } }); return dialog; } public void hideProgressdialog(){ progressDialog.cancel(); } public void hideSoftkeyboard() { try { ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)) .hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } catch (NullPointerException e) { } }}
使用:
KeyMapDailog dialog =new KeyMapDailog("隐藏文字", new KeyMapDailog.SendBackListener() { @Override public void sendBack(String inputText) { //TODO 点击发表后业务逻辑 } });
很多IM软件,需要在软键盘上加一行选择表情,拍照等功能的菜单栏,再上面是输入框。效果自己脑补~
源代码链接http://download.csdn.net/detail/qq_31390699/9717746
0 0
- Android 仿今日头条评论输入框
- Android-仿今日头条评论
- 仿今日头条滑动评论效果
- Android 仿今日头条评论时键盘自动弹出的效果
- 仿今日头条
- 仿今日头条
- css+html仿今日头条的评论框动画效果
- Android 频道管理仿今日头条
- viewpagerindacator仿今日头条
- 精仿今日头条
- 精仿今日头条
- 精仿今日头条
- 精仿今日头条
- 精仿今日头条
- 仿今日头条Fragment
- 仿今日头条adapter
- Android Android HorizontalScrollView滚动实现仿今日头条滑动
- android高仿今日头条 --新闻阅读器
- Android应用程序进行系统签名
- spring boot 自动化配置原理和配置查看
- (实用篇)php中计算中文字符串长度、截取中文字符串的函数代码
- 大数据架构:flume-ng+Kafka+Storm+HDFS 实时系统组合
- servlet 学习心得
- Android 仿今日头条评论输入框
- MyEclipse安装后,新建Java项目为什么会报错?
- Java实现ANN神经网络之BP代码参考
- 汇编语言
- bzoj 3674: 可持久化并查集加强版 可持久化线段树
- The Winter Is Coming
- java封装redis工具类
- linux下实现对framebuffer(/dev/fb0)的截屏操作
- 分享功能