标签:dism parent 收起键盘 contex odm res AC focus 简单
很常见的一种效果,点击评论弹出输入框,点空白区域或点“发送”后输入框消失并收起键盘,通常会遇到键盘遮挡输入框问题,使用Dialog的方式解决这个问题相对简单点,更重要是能代码模块化增加复用性。
评论布局文件 dialog_family_dynamic_comment.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal" android:background="@android:color/white"> <EditText android:id="@+id/et_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <Button android:id="@+id/btn_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn_send" /> </LinearLayout> </RelativeLayout>
主要代码 FamilyDynamicCommentDialog.java
import android.app.Activity; import android.app.Dialog; import android.content.Context; import android.graphics.drawable.ColorDrawable; import android.support.annotation.NonNull; import android.view.LayoutInflater; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.careeach.health.ui.view.FamilyDynamicView; import java.util.Objects; /** * 家庭动态评论 */ public class FamilyDynamicCommentDialog extends Dialog { private EditText etContent; private FamilyDynamicView familyDynamicView; public FamilyDynamicCommentDialog(@NonNull Context context, FamilyDynamicView familyDynamicView) { super(context, R.style.DialogFullScreen); this.familyDynamicView = familyDynamicView; Objects.requireNonNull(getWindow()).setBackgroundDrawable(new ColorDrawable()); View view = LayoutInflater.from(context).inflate(R.layout.dialog_family_dynamic_comment, null, false); etContent = (EditText) view.findViewById(R.id.et_content); Button btnSend = (Button) view.findViewById(R.id.btn_send); setContentView(view); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dismiss(); } }); btnSend.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String content = etContent.getText().toString().trim(); if (content.length() == 0) { Toast.makeText(getContext(), getContext().getString(R.string.toast_please_input_comment_content), Toast.LENGTH_SHORT).show(); return; } FamilyDynamicCommentDialog.this.familyDynamicView.clickCommentSend(content); dismiss(); } }); } @Override public void dismiss() { etContent.setFocusable(false); etContent.setFocusableInTouchMode(false); InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Activity.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(etContent.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); super.dismiss(); } @Override public void show() { super.show(); etContent.setFocusable(true); etContent.setFocusableInTouchMode(true); etContent.post(new Runnable() { @Override public void run() { InputMethodManager inputMethodManager = (InputMethodManager) getContext(). getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); } }); } }
FamilyDynamicView为回调接口,因为使用是mvp结构,可根据实据情况回调即可。这里通过familyDynamicView.clickCommentSend(content)来回调触发发送功能。
R.style.DialogFullScreen 让Dialog全屏。
<style name="DialogFullScreen"> <item name="android:windowFullscreen">true</item> <item name="android:windowNoTitle">true</item> </style>
使用Dialog
FamilyDynamicCommentDialog familyDynamicCommentPopup = new FamilyDynamicCommentDialog(this,this);
familyDynamicCommentPopup.show();
标签:dism parent 收起键盘 contex odm res AC focus 简单
原文地址:https://www.cnblogs.com/markdev/p/9242841.html