当前位置:   article > 正文

Android编程:底部弹出输入框_android底部弹出输入框

android底部弹出输入框

Android编程:底部弹出的对话框


本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.


环境:

主机:WIN10

开发环境:Android Studio 2.2 Preview 3


说明:

《Android编程:底部弹出的对话框》中实现了底部弹出的对话框。在此基础上实现通用的输入框,效果如下图所示。输入框类中通过回调机制通知活动,从而实现了活动与对话框的解耦。



效果图:



布局文件dialog_input.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingLeft="16dp"
  6. android:paddingRight="16dp">
  7. <TextView
  8. android:id="@+id/title"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:layout_gravity="center_horizontal"
  12. android:paddingTop="16dp"
  13. android:paddingBottom="16dp"
  14. android:text="标题"/>
  15. <android.support.design.widget.TextInputLayout
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content">
  18. <android.support.design.widget.TextInputEditText
  19. android:id="@+id/input"
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"
  22. android:maxLines="1"
  23. android:singleLine="true" />
  24. </android.support.design.widget.TextInputLayout>
  25. <LinearLayout
  26. android:layout_width="match_parent"
  27. android:layout_height="wrap_content"
  28. android:paddingTop="16dp"
  29. android:paddingBottom="16dp">
  30. <Button
  31. android:id="@+id/cancel"
  32. android:layout_width="0dp"
  33. android:layout_height="wrap_content"
  34. android:layout_weight="1"
  35. android:layout_marginLeft="5dp"
  36. android:layout_marginRight="5dp"
  37. android:text="取消" />
  38. <Button
  39. android:id="@+id/ok"
  40. android:layout_width="0dp"
  41. android:layout_height="wrap_content"
  42. android:layout_weight="1"
  43. android:layout_marginLeft="5dp"
  44. android:layout_marginRight="5dp"
  45. android:text="确定" />
  46. </LinearLayout>
  47. </LinearLayout>


源代码:

InputDialogInterface.class:

  1. package com.bazhangkeji.classroom.common;
  2. public interface InputDialogInterface {
  3. void onClick();
  4. FilterResult filter(String inputText);
  5. class FilterResult {
  6. public boolean result;
  7. // 错误提示.result为false时有效
  8. public String errorHint = "";
  9. }
  10. }


InputDialog.class:

  1. package com.bazhangkeji.classroom.common;
  2. import android.app.Dialog;
  3. import android.graphics.Color;
  4. import android.os.Bundle;
  5. import android.support.annotation.NonNull;
  6. import android.support.design.widget.TextInputEditText;
  7. import android.text.TextUtils;
  8. import android.view.Gravity;
  9. import android.view.LayoutInflater;
  10. import android.view.MotionEvent;
  11. import android.view.View;
  12. import android.view.Window;
  13. import android.view.WindowManager;
  14. import android.widget.Button;
  15. import android.widget.RelativeLayout;
  16. import android.widget.TextView;
  17. import com.bazhangkeji.classroom.R;
  18. import com.bazhangkeji.classroom.database.DBPersonalInfo;
  19. import com.bazhangkeji.classroom.database.PersonalInfo;
  20. import com.bazhangkeji.classroom.session.VideoSessionActivity;
  21. import com.bazhangkeji.classroom.session.VoiceSessionActivity;
  22. import com.jakewharton.rxbinding2.view.RxView;
  23. import com.trello.rxlifecycle2.android.ActivityEvent;
  24. import com.trello.rxlifecycle2.android.FragmentEvent;
  25. import com.trello.rxlifecycle2.components.RxDialogFragment;
  26. import java.util.concurrent.TimeUnit;
  27. public class InputDialog extends RxDialogFragment {
  28. private Dialog dialog;
  29. private InputDialogInterface observer;
  30. private String title;
  31. private String hint;
  32. private TextInputEditText editTextInput;
  33. /**
  34. * 初始化.必须调用一次
  35. * @param title: 标题
  36. * @param hint: 提示
  37. * @param observer: 观察者对象
  38. */
  39. public void init(String title, String hint, InputDialogInterface observer) {
  40. this.title = title;
  41. this.hint = hint;
  42. this.observer = observer;
  43. }
  44. @NonNull
  45. @Override
  46. public Dialog onCreateDialog(Bundle savedInstanceState) {
  47. dialog = new Dialog(getActivity(), R.style.DialogPopBottom);
  48. View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_input, null);
  49. dialog.setContentView(inflate);
  50. dialog.setCanceledOnTouchOutside(true);
  51. Window window = dialog.getWindow();
  52. WindowManager.LayoutParams lp = window.getAttributes();
  53. lp.gravity = Gravity.BOTTOM;
  54. lp.width = WindowManager.LayoutParams.MATCH_PARENT;
  55. window.setAttributes(lp);
  56. TextView textViewTitle = (TextView) inflate.findViewById(R.id.title);
  57. Button buttonCancel = (Button) inflate.findViewById(R.id.cancel);
  58. Button buttonOK = (Button) inflate.findViewById(R.id.ok);
  59. editTextInput = (TextInputEditText) inflate.findViewById(R.id.input);
  60. textViewTitle.setText(title);
  61. editTextInput.setHint(hint);
  62. RxView.clicks(buttonCancel)
  63. .throttleFirst(1, TimeUnit.SECONDS)
  64. .compose(this.bindUntilEvent(FragmentEvent.DESTROY))
  65. .subscribe(v -> dialog.cancel());
  66. RxView.clicks(buttonOK)
  67. .throttleFirst(1, TimeUnit.SECONDS)
  68. .compose(this.bindUntilEvent(FragmentEvent.DESTROY))
  69. .subscribe(v -> {
  70. InputDialogInterface.FilterResult filterResult = observer.filter(editTextInput.getText().toString());
  71. if (filterResult == null || filterResult.result) {
  72. dialog.cancel();
  73. observer.onClick();
  74. } else {
  75. editTextInput.setError(filterResult.errorHint);
  76. editTextInput.requestFocus();
  77. }
  78. });
  79. return dialog;
  80. }
  81. /**
  82. * 得到输入内容
  83. * @return 输入内容
  84. */
  85. public String getInput() {
  86. return editTextInput.getText().toString();
  87. }
  88. }


 初始化:

private InputDialog addFriendDialog = new InputDialog();

  1. addFriendDialog.init("搜索好友", "请输入用户id", new InputDialogInterface() {
  2. @Override
  3. public void onClick() {
  4. Logging.e("click!!!" + addFriendDialog.getInput());
  5. }
  6. @Override
  7. public FilterResult filter(String inputText) {
  8. return isInputValid(inputText);
  9. }
  10. });
  1. private InputDialogInterface.FilterResult isInputValid(String inputText) {
  2. InputDialogInterface.FilterResult filterResult = new InputDialogInterface.FilterResult();
  3. if (TextUtils.isEmpty(inputText)) {
  4. filterResult.result = false;
  5. filterResult.errorHint = "请输入用户号";
  6. } else if (isContainIllegalChar(inputText)) {
  7. filterResult.result = false;
  8. filterResult.errorHint = "用户号不能包含中文空格等字符";
  9. } else if (inputText.length() < 5 || inputText.length() > 20) {
  10. filterResult.result = false;
  11. filterResult.errorHint = "用户号长度需要5";
  12. } else if (inputText.startsWith("g")) {
  13. filterResult.result = false;
  14. filterResult.errorHint = "用户号不能以g开头";
  15. } else {
  16. filterResult.result = true;
  17. }
  18. return filterResult;
  19. }
  20. private boolean isContainIllegalChar(String str) {
  21. char[] ch = str.toCharArray();
  22. for (char c : ch) {
  23. if (c <= 0x20 || c >= 0x7f) {
  24. return true;
  25. }
  26. }
  27. return false;
  28. }


显示:

addFriendDialog.show(getFragmentManager(), "");



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/770008
推荐阅读
相关标签
  

闽ICP备14008679号