我一直在尝试为我的应用程序制作一个对话片段,但事实证明,这是很难做到的,因为缺乏互联网上可用的示例。下面是我想要的类似的图片。
有一个类似的设计上面的形象将是伟大的。我不确定我所做的是否正确,到目前为止这就是我所做的。
我不知道如何像第一张图片那样布局对话框片段,也不知道如何获取用户可以输入数据的字段。下面是我目前拥有的代码。

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;

public class Reminders extends Activity
{

    Button add, edit, remove;
    ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reminders);
        initializeVariables();

        add.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog();
            }
        });

        List<ListViewItem> items = new ArrayList<Reminders.ListViewItem>();
        items.add(new ListViewItem()
        {
            {
                ThumbnailResource = R.drawable.ic_launcher;
                Title = "Item1 Title";
                Date = "Item1 Date";
                Time = "Item1 Time";
                Amount = "£0.00";
            }
        });
        items.add(new ListViewItem()
        {
            {
                ThumbnailResource = R.drawable.ic_launcher;
                Title = "Item2 Title";
                Date = "Item2 Date";
                Time = "Item2 Time";
                Amount = "£0.00";
            }
        });

        CustomListViewAdapter adapter = new CustomListViewAdapter(this, items);
        lv.setAdapter(adapter);

    }

    private void initializeVariables()
    {
        add = (Button) findViewById(R.id.bAdd);
        edit = (Button) findViewById(R.id.bEdit);
        remove = (Button) findViewById(R.id.bRemove);
        lv = (ListView) findViewById(R.id.LVReminder);
    }

    class ListViewItem
    {
        public int ThumbnailResource;
        public String Title;
        public String Date;
        public String Time;
        public String Amount;
    }


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

    public void doPositiveClick() {
        // Do stuff here.
        Log.i("FragmentAlertDialog", "Positive click!");
    }

    public void doNegativeClick() {
        // Do stuff here.
        Log.i("FragmentAlertDialog", "Negative click!");
    }

    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.ic_launcher)
                    .setTitle(title)
                    .setPositiveButton(R.string.alert_dialog_ok,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                                    int whichButton) {
                                    ((Reminders) getActivity())
                                            .doPositiveClick();
                                }
                            })
                    .setNegativeButton(R.string.alert_dialog_cancel,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                                    int whichButton) {
                                    ((Reminders) getActivity())
                                            .doNegativeClick();
                                }
                            }).create();
        }
    }
}

如果你能给我一些建议,比如我提供的第一张图片中的对话片段,我将不胜感激。感谢所有帮助我的人。

最佳答案

您可以通过xml文档创建希望实现的布局。然后,您可以按如下方式更改onCreateDialog

@Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        int title = getArguments().getInt("title");
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View v = inflater.inflate(R.layout.my_dialog_layout, null);

        return new AlertDialog.Builder(getActivity())
                .setView(v)
                .setIcon(R.drawable.ic_launcher)
                .setTitle(title)
                .setPositiveButton(R.string.alert_dialog_ok,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                                int whichButton) {
                                ((Reminders) getActivity())
                                        .doPositiveClick();
                            }
                        })
                .setNegativeButton(R.string.alert_dialog_cancel,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                                int whichButton) {
                                ((Reminders) getActivity())
                                        .doNegativeClick();
                            }
                        }).create();
    }
}

10-07 15:48