Android编程:底部弹出输入框
来源:互联网 发布:mac 钥匙串访问 编辑:程序博客网 时间:2024/06/05 10:48
Android编程:底部弹出的对话框
本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.
环境:
主机:WIN10
开发环境:Android Studio 2.2 Preview 3
说明:
在《Android编程:底部弹出的对话框》中实现了底部弹出的对话框。在此基础上实现通用的输入框,效果如下图所示。输入框类中通过回调机制通知活动,从而实现了活动与对话框的解耦。
效果图:
布局文件dialog_input.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="16dp" android:paddingRight="16dp"> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:paddingTop="16dp" android:paddingBottom="16dp" android:text="标题"/> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.design.widget.TextInputEditText android:id="@+id/input" android:layout_width="match_parent" android:layout_height="wrap_content" android:maxLines="1" android:singleLine="true" /> </android.support.design.widget.TextInputLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="16dp" android:paddingBottom="16dp"> <Button android:id="@+id/cancel" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:text="取消" /> <Button android:id="@+id/ok" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:text="确定" /> </LinearLayout></LinearLayout>
源代码:
InputDialogInterface.class:
package com.bazhangkeji.classroom.common;public interface InputDialogInterface { void onClick(); FilterResult filter(String inputText); class FilterResult { public boolean result; // 错误提示.result为false时有效 public String errorHint = ""; }}
InputDialog.class:
package com.bazhangkeji.classroom.common;import android.app.Dialog;import android.graphics.Color;import android.os.Bundle;import android.support.annotation.NonNull;import android.support.design.widget.TextInputEditText;import android.text.TextUtils;import android.view.Gravity;import android.view.LayoutInflater;import android.view.MotionEvent;import android.view.View;import android.view.Window;import android.view.WindowManager;import android.widget.Button;import android.widget.RelativeLayout;import android.widget.TextView;import com.bazhangkeji.classroom.R;import com.bazhangkeji.classroom.database.DBPersonalInfo;import com.bazhangkeji.classroom.database.PersonalInfo;import com.bazhangkeji.classroom.session.VideoSessionActivity;import com.bazhangkeji.classroom.session.VoiceSessionActivity;import com.jakewharton.rxbinding2.view.RxView;import com.trello.rxlifecycle2.android.ActivityEvent;import com.trello.rxlifecycle2.android.FragmentEvent;import com.trello.rxlifecycle2.components.RxDialogFragment;import java.util.concurrent.TimeUnit;public class InputDialog extends RxDialogFragment { private Dialog dialog; private InputDialogInterface observer; private String title; private String hint; private TextInputEditText editTextInput; /** * 初始化.必须调用一次 * @param title: 标题 * @param hint: 提示 * @param observer: 观察者对象 */ public void init(String title, String hint, InputDialogInterface observer) { this.title = title; this.hint = hint; this.observer = observer; } @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { dialog = new Dialog(getActivity(), R.style.DialogPopBottom); View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_input, null); dialog.setContentView(inflate); dialog.setCanceledOnTouchOutside(true); Window window = dialog.getWindow(); WindowManager.LayoutParams lp = window.getAttributes(); lp.gravity = Gravity.BOTTOM; lp.width = WindowManager.LayoutParams.MATCH_PARENT; window.setAttributes(lp); TextView textViewTitle = (TextView) inflate.findViewById(R.id.title); Button buttonCancel = (Button) inflate.findViewById(R.id.cancel); Button buttonOK = (Button) inflate.findViewById(R.id.ok); editTextInput = (TextInputEditText) inflate.findViewById(R.id.input); textViewTitle.setText(title); editTextInput.setHint(hint); RxView.clicks(buttonCancel) .throttleFirst(1, TimeUnit.SECONDS) .compose(this.bindUntilEvent(FragmentEvent.DESTROY)) .subscribe(v -> dialog.cancel()); RxView.clicks(buttonOK) .throttleFirst(1, TimeUnit.SECONDS) .compose(this.bindUntilEvent(FragmentEvent.DESTROY)) .subscribe(v -> { InputDialogInterface.FilterResult filterResult = observer.filter(editTextInput.getText().toString()); if (filterResult == null || filterResult.result) { dialog.cancel(); observer.onClick(); } else { editTextInput.setError(filterResult.errorHint); editTextInput.requestFocus(); } }); return dialog; } /** * 得到输入内容 * @return 输入内容 */ public String getInput() { return editTextInput.getText().toString(); }}
初始化:
private InputDialog addFriendDialog = new InputDialog();
addFriendDialog.init("搜索好友", "请输入用户id", new InputDialogInterface() { @Override public void onClick() { Logging.e("click!!!" + addFriendDialog.getInput()); } @Override public FilterResult filter(String inputText) { return isInputValid(inputText); } });
private InputDialogInterface.FilterResult isInputValid(String inputText) { InputDialogInterface.FilterResult filterResult = new InputDialogInterface.FilterResult(); if (TextUtils.isEmpty(inputText)) { filterResult.result = false; filterResult.errorHint = "请输入用户号"; } else if (isContainIllegalChar(inputText)) { filterResult.result = false; filterResult.errorHint = "用户号不能包含中文空格等字符"; } else if (inputText.length() < 5 || inputText.length() > 20) { filterResult.result = false; filterResult.errorHint = "用户号长度需要5"; } else if (inputText.startsWith("g")) { filterResult.result = false; filterResult.errorHint = "用户号不能以g开头"; } else { filterResult.result = true; } return filterResult; } private boolean isContainIllegalChar(String str) { char[] ch = str.toCharArray(); for (char c : ch) { if (c <= 0x20 || c >= 0x7f) { return true; } } return false; }
显示:
addFriendDialog.show(getFragmentManager(), "");
0 0
- Android编程:底部弹出输入框
- 底部弹出密码输入框
- android底部弹出框
- Android编程:底部弹出的对话框
- 底部弹出带输入dialog
- Android 富有动感的底部弹出框
- Android 富有动感的底部弹出框
- Android View 仿iOS7 底部弹出框
- android FloatingActionMenuAndroid 底部弹出框效果
- Android 富有动感的底部弹出框
- Android仿ios底部弹出框效果
- Android实现底部半透明弹出框PopUpWindow
- android底部弹出菜单
- android Dialog 底部弹出
- android 弹出底部dialog
- android 底部弹出dailog
- Android 底部弹出动画
- android 底部弹出菜单
- jsonb格式在postgre中的读写
- HTTP请求中POST与GET的区别
- 【转载】c语言数据的左移右移
- Read_HashMap
- 对飞控的理解(一)
- Android编程:底部弹出输入框
- 【stm32中断优先级--珍藏版】
- 【转载】深入理解嵌入式开发
- ST-LINK调试完成
- Leetcode——97. Interleaving String
- java.net.ConnectException: Connection refused: no further information
- 常用数字滤波算法总结
- el表达式,取集合长度
- 【转载】stm32的GPIO八种工作模式