Android 评论输入框弹窗+软键盘自动弹起
程序员文章站
2022-03-26 10:10:43
需求是输入框和软键盘一起弹出,效果图如下:可以使用Dialog 的形式,因为Dialog中如果有输入框的话在弹出软键盘后会将弹窗顶起,这样就实现了这个效果:本次使用DialogFragment 可以方便管理Dialog的生命周期代码如下:public class InputDialogForFeedBack extends DialogFragment { private Dialog dialog; private EditText editText; private...
需求是输入框和软键盘一起弹出,效果图如下:
可以使用Dialog 的形式,因为Dialog中如果有输入框的话在弹出软键盘后会将弹窗顶起,这样就实现了这个效果:
本次使用DialogFragment 可以方便管理Dialog的生命周期
代码如下:
public class InputDialogForFeedBack extends DialogFragment {
private Dialog dialog;
private EditText editText;
private View contentView;
private Handler handler = new Handler();
private IInputFinishCallback callback;
private TextView cancel;
private TextView submit;
private TextView textNumber;
public interface IInputFinishCallback{
void sendStr(String inputStr);
}
public InputDialogForFeedBack(IInputFinishCallback callback) {
this.callback = callback;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
dialog = new Dialog(getActivity(),R.style.Dialog_NoTitle);
initView();
setDialgParms();
initEvent();
return dialog;
}
private void initEvent() {
submit.setOnClickListener(v -> {
String trim = editText.getText().toString().trim();
if (!TextUtils.isEmpty(trim) && trim.length() <= 200){
callback.sendStr(trim);
dialog.dismiss();
}else {
ToastUtils.showSmallCenter("输入为空");
}
});
cancel.setOnClickListener(v -> {
dialog.dismiss();
});
editText.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) {
String s1 = s.toString();
if (!TextUtils.isEmpty(s1)){
if (s1.length() > 200){
ToastUtils.showSmallCenter("不能超过200个字符");
return;
}
textNumber.setText(s1.length()+"/200");
}
}
});
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
public InputMethodManager mInputMethodManager;
@Override
public void onDismiss(DialogInterface dialog) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
hideSoftkeyboard();
}
}, 200);
}
});
}
private void setDialgParms() {
Window window = dialog.getWindow();
WindowManager.LayoutParams wml = window.getAttributes();
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
wml.width = dm.widthPixels;
wml.gravity = Gravity.BOTTOM;
window.setAttributes(wml);
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
}
private void initView() {
contentView = LayoutInflater.from(getContext()).inflate(R.layout.input_feed_back_dialog_layout,null);
editText = contentView.findViewById(R.id.dialog_input_et);
cancel = contentView.findViewById(R.id.dialog_cancel_tv);
submit = contentView.findViewById(R.id.dialog_submit_tv);
textNumber = contentView.findViewById(R.id.dialog_text_number_tv);
dialog.setContentView(contentView);
}
public static void showKeyboard(View view) {
InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
view.requestFocus();
imm.showSoftInput(view, 0);
}
}
public void hideSoftkeyboard() {
try {
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
} catch (NullPointerException e) {
}
}
@Override
public void onResume() {
super.onResume();
handler.postDelayed(new Runnable() {
@Override
public void run() {
showKeyboard(editText);
}
},300);
}
@Override
public void onDestroy() {
super.onDestroy();
if (dialog != null){
dialog.dismiss();
dialog = null;
}
if (callback != null){
callback = null;
}
if (handler != null){
handler.removeCallbacksAndMessages(null);
handler = null;
}
}
}
使用的话很简单
InputDialogForFeedBack dialog = new InputDialogForFeedBack(new InputDialogForFeedBack.IInputFinishCallback() {
@Override
public void sendStr(String inputStr) {
//TODO 提交反馈信息
}
});
dialog.showNow(fragmentManager,"input");
附:弹窗的样式代码
<style name="Dialog.NoTitle" parent="Theme.AppCompat.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@style/AnimationPicker</item>
</style>
本文地址:https://blog.csdn.net/sinat_35670989/article/details/110550133
下一篇: mysql的触发器使用