Android 输入框自定义样式位于输入键盘上方
程序员文章站
2022-03-16 15:22:51
自定义一个输入弹框的InputEditDialog/** * date:2020/11/27 0027 * author:wsm (Administrator) * funcation:输入消息弹框 */public class InputEditDialog extends Dialog { public interface OnTextSendListener { void onTextSend(String msg); } private Im....
自定义一个输入弹框的InputEditDialog
/**
* date:2020/11/27 0027
* author:wsm (Administrator)
* funcation:输入消息弹框
*/
public class InputEditDialog extends Dialog {
public interface OnTextSendListener {
void onTextSend(String msg);
}
private ImageView confirmBtn;
private AutoCompleteTextView messageTextView;
private static final String TAG = TCInputTextMsgDialog.class.getSimpleName();
private Context mContext;
private InputMethodManager imm;
private RelativeLayout rlDlg;
private int mLastDiff = 0;
private OnTextSendListener mOnTextSendListener;
public InputEditDialog(Context context, int theme, String hint) {
super(context, theme);
mContext = context;
setContentView(R.layout.dialog_input_edit);
messageTextView = findViewById(R.id.et_input_message);
messageTextView.setInputType(InputType.TYPE_CLASS_TEXT);
//修改下划线颜色
messageTextView.setHint(hint);
messageTextView.getBackground().setColorFilter(context.getResources().getColor(R.color.transparent), PorterDuff.Mode.CLEAR);
confirmBtn = (ImageView) findViewById(R.id.confrim_btn);
imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
confirmBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String msg = messageTextView.getText().toString().trim();
if (!TextUtils.isEmpty(msg)) {
mOnTextSendListener.onTextSend(msg);
imm.showSoftInput(messageTextView, InputMethodManager.SHOW_FORCED);
imm.hideSoftInputFromWindow(messageTextView.getWindowToken(), 0);
messageTextView.setText("");
dismiss();
} else {
ToastUtil.show("输入信息不能为空");
}
messageTextView.setText(null);
}
});
messageTextView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
switch (actionId) {
case KeyEvent.KEYCODE_ENDCALL:
case KeyEvent.KEYCODE_ENTER:
if (messageTextView.getText().length() > 0) {
// mOnTextSendListener.onTextSend("" + messageTextView.getText(), mDanmuOpen);
//sendText("" + messageTextView.getText());
//imm.showSoftInput(messageTextView, InputMethodManager.SHOW_FORCED);
imm.hideSoftInputFromWindow(messageTextView.getWindowToken(), 0);
// messageTextView.setText("");
dismiss();
} else {
Toast.makeText(mContext, "input can not be empty!", Toast.LENGTH_LONG).show();
}
return true;
case KeyEvent.KEYCODE_BACK:
dismiss();
return false;
default:
return false;
}
}
});
messageTextView.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
LogUtils.d("My test", "onKey " + keyEvent.getCharacters());
return false;
}
});
rlDlg = (RelativeLayout) findViewById(R.id.rl_outside_view);
rlDlg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() != R.id.rl_inputdlg_view)
dismiss();
}
});
final LinearLayout rldlgview = (LinearLayout) findViewById(R.id.rl_inputdlg_view);
rldlgview.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
Rect r = new Rect();
//获取当前界面可视部分
getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
//获取屏幕的高度
int screenHeight = getWindow().getDecorView().getRootView().getHeight();
//此处就是用来获取键盘的高度的, 在键盘没有弹出的时候 此高度为0 键盘弹出的时候为一个正数
int heightDifference = screenHeight - r.bottom;
if (heightDifference <= 0 && mLastDiff > 0) {
//imm.hideSoftInputFromWindow(messageTextView.getWindowToken(), 0);
dismiss();
}
mLastDiff = heightDifference;
}
});
rldlgview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imm.hideSoftInputFromWindow(messageTextView.getWindowToken(), 0);
dismiss();
}
});
}
public void setmOnTextSendListener(OnTextSendListener onTextSendListener) {
this.mOnTextSendListener = onTextSendListener;
}
@Override
public void dismiss() {
super.dismiss();
//dismiss之前重置mLastDiff值避免下次无法打开
mLastDiff = 0;
}
@Override
public void show() {
super.show();
}
}
布局文件,一个editview加一个右侧发送图标
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_outside_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/rl_inputdlg_view"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="@color/main_bg"
android:orientation="horizontal"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="5dp">
<EditText
android:id="@+id/et_input_message"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="12dp"
android:layout_weight="1"
android:background="@color/main_bg"
android:dropDownVerticalOffset="1.0dip"
android:focusable="true"
android:imeOptions="actionSearch"
android:inputType="text|textAutoComplete"
android:padding="5dp"
android:textColor="@color/txt0"
android:textColorHint="@color/txtgray"
android:textSize="16sp" />
<ImageView
android:id="@+id/confrim_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@mipmap/sendimage" />
</LinearLayout>
</RelativeLayout>
代码中调用使用
oncrete()方法中
InputEditDialog mInputEditDialog = new InputEditDialog(this, R.style.InputDialog, "请输入修改的内容");
mInputEditDialog.setmOnTextSendListener(this);
需要弹出输入框的点击事件下
case R.id.lin_click:
showInputMsgDialog();
break;
/**
* 发消息弹出框
*/
private void showInputMsgDialog() {
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
WindowManager.LayoutParams lp = mInputEditDialog.getWindow().getAttributes();
lp.width = (int) (display.getWidth()); //设置宽度
mInputEditDialog.getWindow().setAttributes(lp);
mInputEditDialog.setCancelable(true);
mInputEditDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
mInputEditDialog.show();
}
@Override
public void onTextSend(String msg) {
if ( !TextUtils.isEmpty(msg)) {
//这里就是你的输入框点击发送之后需要做的一些操作
}
}
本文地址:https://blog.csdn.net/weixin_43117800/article/details/110235989