Android 仿今日头条评论时键盘自动弹出的效果:当点击评论时,弹出对话框,同时弹出软键盘,当点击返回键时,将对话框关闭,不只是关闭软键盘。
效果图:
对这个对话框设置一个style效果:
<style name="inputDialog" parent="@android:style/Theme.Holo.Light.Dialog">
<item name="android:windowBackground">@color/dialog_bg</item>
<!--背景-->
<item name="android:windowFrame">@null</item>
<!--设置无边框-->
<item name="android:windowNoTitle">true</item>
<!-- 无标题 -->
<item name="android:backgroundDimEnabled">true</item>
<!-- 模糊 -->
<item name="android:windowSoftInputMode">stateAlwaysVisible</item>
<!--显示软件盘-->
</style>
并设置Dialog的监听返回键事件,不然默认是隐藏软键盘:
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialogInterface, int keyCode, KeyEvent keyEvent) {
if (keyCode == KeyEvent.KEYCODE_BACK && keyEvent.getRepeatCount() == 0)
dialog.cancel();
return false;
}
});
做完以上两步,就可以实现与今日头条评论一样的效果了。
附上源码:
package com.gdjw.qmcg.dialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import com.gdjw.qmcg.R;
import com.gdjw.qmcg.tools.DrawableUtil;
import butterknife.Bind;
import butterknife.ButterKnife;
/**
* Created by WW on 2017/2/23.
* 评论对话框
*/
public class CommentDialog extends Dialog implements View.OnClickListener {
@Bind(R.id.tv_commit)
TextView tv_commit;//提交
@Bind(R.id.et_comment)
EditText et_comment;//评论内容
@Bind(R.id.tv_location)
TextView tv_location;//定位
@Bind(R.id.view_line)
View view_line;//竖线
@Bind(R.id.ib_delete)
ImageButton ib_delete;//删除按钮
@Bind(R.id.cb_anonymous)
CheckBox cb_anonymous;//匿名
private Context context;
private OnCommitListener listener;
public CommentDialog(Context context) {
this(context, R.style.inputDialog);
this.context = context;
}
public CommentDialog(Context context, int themeResId) {
super(context, themeResId);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.comment_dialog_layout);
ButterKnife.bind(this);
initListener();
}
private void initListener() {
//设置显示对话框时的返回键的监听
this.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialogInterface, int keyCode, KeyEvent keyEvent) {
if (keyCode == KeyEvent.KEYCODE_BACK && keyEvent.getRepeatCount() == 0)
CommentDialog.this.cancel();
return false;
}
});
//设置EditText内容改变的监听
et_comment.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) {
if (!TextUtils.isEmpty(s)) {
tv_commit.setBackgroundDrawable(
DrawableUtil.getImageDrawable(context, R.drawable.comment_dialog_btn_process_shape));
tv_commit.setClickable(true);
} else {
tv_commit.setBackgroundDrawable(
DrawableUtil.getImageDrawable(context, R.drawable.comment_dialog_btn_normal_shape));
tv_commit.setClickable(false);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
//匿名
cb_anonymous.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (null != listener) {
listener.onAnonymousChecked(buttonView, isChecked);
}
}
});
tv_commit.setOnClickListener(this);//提交
tv_location.setOnClickListener(this);//定位
ib_delete.setOnClickListener(this);//删除
}
public void setOnCommitListener(OnCommitListener listener) {
this.listener = listener;
}
public interface OnCommitListener {
void onCommit(EditText et, View v);//提交数据
void onGetLocation();//定位
void onDeleteLocation();//删除定位
void onAnonymousChecked(CompoundButton buttonView, boolean isChecked);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_commit:
if (null != listener) {
listener.onCommit(et_comment, v);
}
break;
case R.id.tv_location:
if (null != listener) {
listener.onGetLocation();
}
break;
case R.id.ib_delete:
if (null != listener) {
listener.onDeleteLocation();
}
break;
}
}
public void setLocationState(boolean state, String str) {
if (state) {//定位状态
tv_location.setCompoundDrawables(DrawableUtil.
setDrawableLeft(context, R.mipmap.icon_location_bg_pressed), null, null, null);
view_line.setVisibility(View.VISIBLE);
ib_delete.setVisibility(View.VISIBLE);
tv_location.setText(str);
} else {
//设置drawableLeft
tv_location.setCompoundDrawables(DrawableUtil.
setDrawableLeft(context, R.mipmap.icon_location_bg), null, null, null);
view_line.setVisibility(View.GONE);
ib_delete.setVisibility(View.GONE);
tv_location.setText("点击获取位置");
}
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<View
android:layout_width="0dp"
android:layout_height="1px"
android:layout_weight="1" />
<TextView
android:id="@+id/tv_commit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="@drawable/comment_dialog_btn_normal_shape"
android:clickable="false"
android:text="发 表"
android:textColor="@color/white"
android:textSize="15dp" />
</LinearLayout>
<EditText
android:id="@+id/et_comment"
android:layout_width="match_parent"
android:layout_height="110dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/comment_dialog_et_selector"
android:gravity="left|top"
android:hint="请输入评论内容"
android:inputType="textMultiLine"
android:textColor="@android:color/black"
android:textColorHint="@color/colorGray_999"
android:textCursorDrawable="@drawable/comment_dialog_et_cursor_shape"
android:textSize="14sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/comment_location_bg"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="10dp"
android:clickable="true"
android:drawableLeft="@mipmap/icon_location_bg"
android:drawablePadding="3dp"
android:gravity="center_vertical"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:singleLine="true"
android:text="点击获取位置"
android:textColor="@color/black_2b"
android:textSize="12sp" />
<View
android:id="@+id/view_line"
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginLeft="3dp"
android:background="@color/colorGray_999"
android:visibility="gone" />
<ImageButton
android:id="@+id/ib_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:background="@mipmap/icon_red_delete"
android:visibility="gone" />
</LinearLayout>
<CheckBox
android:id="@+id/cb_anonymous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:button="@null"
android:checked="false"
android:clickable="true"
android:drawableLeft="@drawable/cb_anonymity_bg_selector"
android:focusable="true"
android:gravity="center_vertical"
android:text="匿名"
android:textColor="@color/colorGray_666"
android:textSize="14sp" />
</RelativeLayout>
</LinearLayout>
最后还要在清单文件对应的Activity上加上:
android:windowSoftInputMode="adjustPan"