http://developer.android.com/design/building-blocks/dialogs.html中的Android设计文档明确区分了对话框,警报,弹出窗口和Toast.它还建议通过类和Toasts通过类来实现Dialogs.但是,我不清楚是否应该使用或使用弹出窗口.DialogFragmentToastPopupWindowDialogFragment
我知道DialogFragments通常带有Ok/Cancel按钮,并且PopupWindows可以定义位置,但是:
DialogFragment的继任者PopupWindow会在某个时候被弃用吗?如何在使用AlertDialog创建DialogFragment时禁用"确定/取消"按钮?我尝试调用myAlertDialogFragment.getDialog(),但即使显示片段,它也始终返回null
public static class MyAlertDialogFragment extends DialogFragment {
public static MyAlertDialogFragment newInstance(int title) {
MyAlertDialogFragment frag = new MyAlertDialogFragment();
Bundle args = new Bundle();
args.putInt("title", title);
frag.setArguments(args);
return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int title = getArguments().getInt("title");
return new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(title)
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((FragmentAlertDialog)getActivity()).doPositiveClick();
}
}
)
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((FragmentAlertDialog)getActivity()).doNegativeClick();
}
}
)
.create();
}
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过膨胀包含取消和确定按钮的布局来实现它,但我宁愿使用AlertDialog解决方案,如果可能的话
如果当前正在关注edittext并且用户在DialogFragment外部单击; 我希望屏幕键盘消失.当DialogFragment以这种方式被解雇时,我可以让它工作:
InputMethodManager imm;
public View onCreateView(LayoutInflater inflator, ViewGroup container,
Bundle savedInstanceState) {
imm = (InputMethodManager)getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
...}
@Override
public void dismiss(){
imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
super.dismiss();
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试通过触摸对话片段之外取消它时同样的事情,它将无法工作.我试图通过覆盖onCancel来做到这一点:
@Override
public void onCancel(DialogInterface dialog){
imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
super.onCancel(dialog);
}
Run Code Online (Sandbox Code Playgroud)
当外部触摸事件发生时调用该函数,但不删除键盘.
我想要实现的目标
谷歌在其日历应用中实施的小/大屏幕行为.
例如,当您创建新事件时,在大屏幕上会打开"对话框"
在纵向模式下,对话框工具栏的底部与活动的"扩展"工具栏的底部对齐.
上下文操作栏(例如用于文本选择)是在活动的工具栏上绘制的,并向下推动对话框,因此它不再与活动的工具栏相关联.
在横向模式下,它似乎固定在通知栏下方; 上下文操作栏是在活动的工具栏上绘制的
在小屏幕上
到目前为止我尝试过的
A.从android文档中全屏显示DialogFragment示例(不能再添加缺少声誉的b/c链接)
B.添加(感谢stackoverflow上的一些帖子)
对话框片段样式:
setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialogFragmentStyle);
<style name="MyDialogFragmentStyle" parent="@style/MyTheme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">false</item>
<item name="android:windowIsFloating">false</item>
</style>
Run Code Online (Sandbox Code Playgroud)删除了窗口标题
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
Run Code Online (Sandbox Code Playgroud)添加了自定义工具栏
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:homeAsUpIndicator="@drawable/ic_arrow_back_white_24dp"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar" >
</FrameLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)并在小型设备上将对话框设置为全屏
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
Run Code Online (Sandbox Code Playgroud)这有点工作,它解决了我想要达到的一些要点和A.'Android样本'问题(透明背景,...)
使背景中的活动变暗
工具栏始终位于同一位置,并且永远不会被屏幕键盘推出视图
在纵向模式下,对话框工具栏的底部与Activity的"扩展/大"工具栏的底部对齐.上下文操作栏(例如用于文本选择)是在活动的工具栏上绘制的,并向下推动对话框,因此它不再与活动的工具栏相关联.
在横向模式下,它似乎固定在通知栏下方; 上下文操作栏是在活动的工具栏上绘制的.
我有一个DialogFragment包含AutoCompleteTextView,以及Cancel和OK按钮.
该AutoCompleteTextView是给我是从服务器获取用户名的建议.
我想要做的是限制用户只能输入现有的用户名.
我知道我可以在用户点击时检查该用户名是否存在OK,但是还有其他方式,假设如果不存在此类用户名,则不允许用户输入字符.我不知道该怎么做,因为在每个输入的角色上我只得到5个建议.服务器以这种方式实现.
欢迎任何建议.谢谢
android autocompletetextview android-dialogfragment dialogfragment
我使用DialogFragment与ListView(列出所有客户)和EditText(从列表中进行搜索),它的正常工作.但是,每当从片段显示对话框时,始终显示键盘并且用户需要辞职.有没有办法在显示对话框片段时第一次隐藏它?然后,当用户点击编辑文本时,键盘应该出现.
我已经尝试android:focusable="false"在我的XML中进行设置但是,它在点击后也总是隐藏键盘EditText也没有显示.
然后我尝试设置,android:focusableInTouchMode="true"但是,如上所述
我使用官方指南创建了一个全屏对话框
问题是,我的工具栏与状态栏重叠,我无法弄清楚如何解决这个问题.
DialogFragment
public class CreateAccountDialogFragment extends BaseDialogFragment {
@Inject
CreateAccountViewModel viewModel;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//InjectDependencies....
View rootView = createDataBinding(inflater, container);
createToolbar(rootView);
return rootView;
}
private View createDataBinding(LayoutInflater inflater, ViewGroup container) {
CreateAccountDialogFragmentBinding binding =
DataBindingUtil.inflate(inflater, R.layout.create_account_dialog_fragment, container, false);
binding.setViewModel(viewModel);
return binding.getRoot();
}
private void createToolbar(View rootView) {
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
// Set an OnMenuItemClickListener to handle menu item clicks
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) { …Run Code Online (Sandbox Code Playgroud) 我的对话框片段顶部出现了一条蓝线,我无法摆脱(我甚至不知道为什么它出现在第一位.有人知道如何摆脱这个吗?
我已经在几个设备上测试了它,它在以后的Android版本上运行得很好.
我的代码:
private void setupDialog() {
final Dialog dialog = getDialog();
final Window window = dialog.getWindow();
window.setBackgroundDrawable(new ColorDrawable(0));
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}
Run Code Online (Sandbox Code Playgroud)
布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tiktok="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/settings_bg">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<ImageView
android:id="@+id/close_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="15dp"
android:src="@drawable/pressable_close_btn"/>
<com.cyscorpions.timekeeper.customviews.TKTextView
android:id="@+id/settings_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/dp_40"
android:text="@string/settings_allcaps"
android:textColor="@color/timekeeper_blue"
android:textSize="@dimen/sp_60"
tiktok:useBoldFont="true"/>
<com.cyscorpions.timekeeper.customviews.TKTextView
android:id="@+id/account_name_instruction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/settings_title"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/dp_20"
android:text="@string/subdomain_instruction"
android:textColor="@color/gray"
android:textSize="@dimen/sp_30"/>
<RelativeLayout
android:id="@+id/info_field"
android:layout_width="wrap_content"
android:layout_height="@dimen/dp_70"
android:layout_alignLeft="@+id/submit_btn"
android:layout_below="@id/account_name_instruction"
android:layout_centerHorizontal="true"
android:layout_marginLeft="2dp"
android:layout_marginTop="@dimen/dp_20">
<com.cyscorpions.timekeeper.customviews.TKAppCompatEditText
android:id="@+id/subdomain_textfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="@drawable/text_field_bg"
android:hint="@string/textfield_account_hint"
android:inputType="text"
android:textColor="@color/gray" …Run Code Online (Sandbox Code Playgroud) java android android-fragments android-dialogfragment dialogfragment
我android.support.v4.app.DialogFragment在Google上搜索找不到合适的生命周期.我需要这个用于一些实现.我们知道DialogFragment有一些相同的方法Dialog.
DialogFragment延伸Fragment所以它的生命周期与之相同Fragment.但是其他方法DialogFragment呢?
这是Fragment生命周期.可以提供DialogFragment吗?
android android-dialogfragment dialogfragment appcompatdialogfragment
我从片段A开始bottomSheetDialogFragment。我想从该bottomSheetDialogFragment中选择日期,然后将其设置在片段A中。
选择日期已经完成,我只想将其放在片段A中以在某些字段中进行设置。
我如何获得价值?有什么建议怎么做吗?
android ×10
dialogfragment ×10
java ×2
bottom-sheet ×1
button ×1
dialog ×1
fragment ×1
keyboard ×1
popup ×1
popupwindow ×1
screen-size ×1
show-hide ×1