android– 向DialogFragment添加OkCancel按钮

android– 向DialogFragment添加OkCancel按钮,第1张

概述我有一个对话框Fragment,我想把ok/cancel按钮放在对话框的底部.我试过,但我唯一得到的是编辑文本上的按钮(在DialogFragment中).这是我的对话:这是我的对话框代码:publicclassdialogNewFileextendsDialogFragment{privateEditTexttextNewFile;publicdialogNewFile()

我有一个对话框Fragment,我想把ok / cancel按钮放在对话框的底部.我试过,但我唯一得到的是编辑文本上的按钮(在DialogFragment中).这是我的对话:


这是我的对话框代码:

public class dialogNewfile extends DialogFragment {private EditText textNewfile;public dialogNewfile(){}public static dialogNewfile newIstance(String Title){    dialogNewfile frag=new dialogNewfile();    Bundle args=new Bundle();    args.putString("Title",Title);    frag.setArguments(args);    return frag;}@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedistanceState ){    return inflater.inflate(R.layout.fragment_newfile,container);}@OverrIDepublic voID onVIEwCreated(VIEw vIEw, @Nullable Bundle savedistanceState){    super.onVIEwCreated(vIEw, savedistanceState);    textNewfile=(EditText) vIEw.findVIEwByID(R.ID.edit_text_newfile);    String Title=getArguments().getString("Title","Enter @R_419_6889@");    getDialog().setTitle(Title);    textNewfile.requestFocus();    getDialog().getwindow().setSoftinputMode(WindowManager.LayoutParams.soFT_input_STATE_VISIBLE);}@OverrIDepublic voID onResume() {    Window window = getDialog().getwindow();    Point size = new Point();    // Store dimensions of the screen in `size`    display display = window.getwindowManager().getDefaultdisplay();    display.getSize(size);    // Set the wIDth of the dialog proportional to 75% of the screen wIDth    window.setLayout((int) (size.x * 0.75), (int) (size.x * 0.50));    window.setGravity(Gravity.CENTER);    // Call super onResume after sizing    super.onResume();}

这是对话框片段的布局:

<?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:layout_gravity="center"androID:ID="@+ID/dialogNewfile"><EditText    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:padding="35dp"    androID:hint="@string/hint_new_file"    androID:inputType="text"    androID:ID="@+ID/edit_text_newfile"/></linearLayout>

解决方法:

您将覆盖onCreateDialog并使用AlertDialog.Builder设置正面和负面按钮,如下所示:

@OverrIDepublic Dialog onCreateDialog(Bundle savedInstanceState) {    String Title = getArguments().getString("Title");    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());    builder.setTitle(Title);    builder.setMessage("Are you sure?");    // Edited: OverrIDing onCreateVIEw is not necessary in your case    LayoutInflater inflater = LayoutInflater.from(getContext());    VIEw newfileVIEw = inflater.inflate(R.layout.fragment_newfile, null);    builder.setVIEw(newfileVIEw);    builder.setPositivebutton("OK",  new DialogInterface.OnClickListener() {        @OverrIDe        public voID onClick(DialogInterface dialog, int which) {            // on success        }    });    builder.setNegativebutton("Cancel", new DialogInterface.OnClickListener() {        @OverrIDe        public voID onClick(DialogInterface dialog, int which) {            dialog.dismiss();        }    });    return builder.create();}
总结

以上是内存溢出为你收集整理的android – 向DialogFragment添加Ok / Cancel按钮全部内容,希望文章能够帮你解决android – 向DialogFragment添加Ok / Cancel按钮所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1108485.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存