赞
踩
Android编程:底部弹出的对话框
环境:
主机:WIN10
开发环境:Android Studio 2.2 Preview 3
说明:
在《Android编程:底部弹出的对话框》中实现了底部弹出的对话框。在此基础上实现通用的输入框,效果如下图所示。输入框类中通过回调机制通知活动,从而实现了活动与对话框的解耦。
效果图:
布局文件dialog_input.xml:
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp">
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:layout_width="match_parent"
android:layout_height="wrap_content">
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:singleLine="true" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:paddingBottom="16dp">
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="取消" />
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="确定" />
源代码:
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(), "");
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。