如何使用对话框片段? (的ShowDialog德preciated)的Andr​​oid [英] How to use Dialog Fragment? (showDialog depreciated) Android

查看:137
本文介绍了如何使用对话框片段? (的ShowDialog德preciated)的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,有这个文件

<一个href="http://developer.android.com/reference/android/app/DialogFragment.html#AlertDialog">http://developer.android.com/reference/android/app/DialogFragment.html#AlertDialog

但作为一个新的Andr​​oid / Java的学习是不容易理解的code写入弹出了2个选项一个简单的警告对话框涉及的金额(是/否)消息。

but as a new Android/Java learner it is not easy to understand the amount of code involved from writing a simple alert dialog that pops up with 2 options (yes/no) message.

下面是code我目前在我的MainActivity文件:

Here is the code I currently have in my MainActivity file:

final private int RESET_DIALOG = 0;

    private OnClickListener resetButtonListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            showDialog(RESET_DIALOG);

        }
    };

    protected android.app.Dialog onCreateDialog(int id) {
        switch(id) {
        case RESET_DIALOG: 
            AlertDialog.Builder builder = new Builder(this);
            return builder
                    .setMessage("Are you sure you want to reset the count?")
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {    

                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            Toast.makeText(MainActivity.this, "Did not reset!", 5).show();

                        }
                    })

                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {


                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            Toast.makeText(MainActivity.this, "Did Reset!", 5).show();

                        }
                    })
                    .create();
        }
        return null;
    };

这是我尝试下在Android网站上的说明:主要活动文件:

This is my attempt to following the instructions on the android site: Main Activity file:

final private int RESET_DIALOG = 0;

    private OnClickListener resetButtonListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, MainDialog.class);
            startActivity(intent);

        }
    };

    protected android.app.Dialog onCreateDialog(int id) {
        switch(id) {
        case RESET_DIALOG: 
            AlertDialog.Builder builder = new Builder(this);
            return builder
                    .setMessage("Are you sure you want to reset the count?")
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {    

                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            Toast.makeText(MainActivity.this, "Did not reset!", 5).show();

                        }
                    })

                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {


                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            Toast.makeText(MainActivity.this, "Did Reset!", 5).show();

                        }
                    })
                    .create();
        }
        return null;
    };

然后创建一个MainDialog类:(我居然瘦了如何正确地做到这一点,或申请吧)

Then created a MainDialog class: (I am actually lost in how to do this correctly or apply it)

package com.proteintracker;

import android.support.v4.app.DialogFragment;

public class MainDialog 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;
    }
}

我不知道如果我想创造片段一个新的类,以及如何将其应用到我的活动画面当前对话框。

I am not sure if I was suppose to create a new class for the fragment and how to apply it to my current dialog in the activity screen.

推荐答案

您可以告诉你 FragmentDialog 是这样的:

You can show you FragmentDialog like this:

void showDialog() {
    DialogFragment newFragment = MyAlertDialogFragment.newInstance(
            R.string.alert_dialog_two_buttons_title);
    newFragment.show(getFragmentManager(), "dialog");
}

在你片段对话框你应该重写 onCreateDialog 键,返回简单的对话框,例如<$ c您实例$ C> AlertDialog 。

In you fragment dialog you should override onCreateDialog and return you instance of simple Dialog, for example AlertDialog.

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");

    AlertDialog.Builder builder = new Builder(this);
    return builder
                .setMessage("Are you sure you want to reset the count?")
                .setNegativeButton("No", new DialogInterface.OnClickListener() {    

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        Toast.makeText(MainActivity.this, "Did not reset!", 5).show();

                    }
                })

                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {


                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        Toast.makeText(MainActivity.this, "Did Reset!", 5).show();

                    }
                })
                .create();
}
}

这篇关于如何使用对话框片段? (的ShowDialog德preciated)的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆