dialog代码,基本与普通写法一样,只是加一个回调方法
package com.cn.wq.dialog;
import android.app.Activity;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import com.cn.wq.dialog.view.ResizeLayout;
/**
* Created by dick on 2017/9/22.
*/
public class DialogInputUtils {
private static DialogInputUtils instance;
private AlertDialog dialog;
private EditText mEditText;
private TextView mTvSend;
private boolean isShowInput = false;
private OnTalkClickListener onTalkClick;
private DialogInputUtils() {
}
public static DialogInputUtils getInstance() {
if (instance == null) {
synchronized (DialogInputUtils.class) {
if (instance == null) {
instance = new DialogInputUtils();
}
}
}
return instance;
}
public void setOnTalkClick(OnTalkClickListener onTalkClick) {
this.onTalkClick = onTalkClick;
}
public interface OnTalkClickListener {
void onTalkClick(String content);
}
/**
* cid救援单ID
*
* @param activity
*/
public void showInputDialog(final Activity activity) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity, R.style.Dialog_Fullscreen);
final View view = LayoutInflater.from(activity).inflate(R.layout.dialog_edittext, null);
builder.setView(view);
builder.setCancelable(true);
dialog = builder.create();
dialog.setCanceledOnTouchOutside(true);
final Window dialogWindow = dialog.getWindow();
dialogWindow.setGravity(Gravity.BOTTOM);
addListener(view);
dialog.show();
setDialog(activity);
}
private void setDialog(final Activity activity) {
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.x = 0;
lp.y = 0;
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
final int height = (int) activity.getResources().getDimension(R.dimen.parent_list_height);
lp.height = height;
dialog.getWindow().setAttributes(lp);
mEditText = (EditText) dialog.getWindow().findViewById(R.id.edt_text);
mTvSend = (TextView) dialog.getWindow().findViewById(R.id.tv_send);
mEditText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_BACK) {
dialog.dismiss();
}
return false;
}
});
mEditText.postDelayed(new Runnable() {
@Override
public void run() {
KeyboardUtils.showSoftInput(mEditText);
}
}, 150);
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(final DialogInterface dg) {
if (isShowInput) {
KeyboardUtils.toggleSoftInput(activity);
}
}
});
mTvSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.w("input", "点击了发送");
if (DialogInputUtils.this.onTalkClick != null) {
DialogInputUtils.this.onTalkClick.onTalkClick(mEditText.getText().toString());
}
}
});
}
private void addListener(View view) {
final ResizeLayout rootLayout = (ResizeLayout) view.findViewById(R.id.resize_layout);
rootLayout.setListener(new ResizeLayout.Listener() {
@Override
public void onSoftKeyboardShown(boolean isShowing) {
Log.w("input", "--------->isShowingL:" + isShowing);
isShowInput = isShowing;
}
});
}
}
2 resizeLayout 内容,由于需要监听dialog的关闭与打开,系统提供的方法很不管用,只能通过自定义方式实现,在这里我是通过dialog打开与关闭时的坐标变化,来判断dialog是打开还是关闭状态
package com.cn.wq.dialog.view;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.EditText;
import android.widget.LinearLayout;
import com.cn.wq.dialog.R;
public class ResizeLayout extends LinearLayout {
private static int count = 0;
private final int keyHeight;
private EditText editText;
private int[] position = new int[2];
private final int screenHeight;
public ResizeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
screenHeight = ScreenUtils.getScreenHeight(context);
keyHeight = screenHeight / 3;
Log.w("input", "screenHeight:" + screenHeight);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (editText == null) {
editText = (EditText) this.findViewById(R.id.edt_text);
}
if (editText != null) {
editText.getLocationOnScreen(position);
Log.w("input", "\t position on screenY:" + position[1]);
if (listener != null)
listener.onSoftKeyboardShown(position[1] < screenHeight * 0.7);
}
}
public interface Listener {
public void onSoftKeyboardShown(boolean isShowing);
}
private Listener listener;
public void setListener(Listener listener) {
this.listener = listener;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
3 layout比较简单
<?xml version="1.0" encoding="utf-8"?>
<com.cn.wq.dialog.view.ResizeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/resize_layout"
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:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edt_text"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:hint="说说看"
android:background="@drawable/edit_bg_shape"
android:textSize="15sp"
android:imeOptions="actionDone"
android:paddingLeft="20dp" />
<TextView
android:id="@+id/tv_send"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="center"
android:paddingRight="20dp"
android:text="发送"
android:textSize="17sp"
android:textColor="@android:color/holo_green_dark" />
</RelativeLayout>
</LinearLayout>
</com.cn.wq.dialog.view.ResizeLayout>