片段上的Datepicker对话框使用警告对话框构建器

时间:2014-03-07 06:22:16

标签: android android-fragments datepicker

我正在使用导航抽屉开发一个应用程序。我的基本活动是仪表板活动。这是一个导航列表。当我点击导航列表中的项目时,它会显示一些使用片段的界面(替换Dashboard Activity)。在AddPatient片段中,我正在显示form.I正在添加Date_of_birth字段,当我点击该date_of_birth时(编辑) -text)查看日期选择器对话框应弹出(使背景透明但黑暗),获取输入,将其显示在片段上。

问题是我没有找到如何在片段上显示日期选择器,基本活动(仪表板活动)扩展了片段活动,而AddPatient片段扩展了DailogFragment。代码如下。我不知道我哪里出错了。

我的仪表板活动的代码是

    public class DashboardActivity extends FragmentActivity {
    Fragment fragment;
private DrawerLayout mDrawerLayout;
private ExpandableListView mDrawerList;
private ArrayList<NavDrawerItem> navDrawerItems;
private int groupP, childP;
private SwipeListView swipeListView;
List<CalenderDrawable> list;
List<String> menu_items;
Map<String, List<String>> childCollection;
public static List<String> childList;
public static List<String> childListForConnectedService;

private ActionBarDrawerToggle mDrawerToggle;
Session session;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
    setContentView(R.layout.activity_dashboard);
    session = new Session(this);

    fragment = new DashboardFragment();
    getFragmentManager().beginTransaction()
            .replace(R.id.frame_container, fragment).commit();

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ExpandableListView) findViewById(R.id.list_slidermenu);

    createMenuItemList();
    createCollection();
    final ExpandableListAdapterNew adapter = new ExpandableListAdapterNew(
            DashboardActivity.this, menu_items, childCollection);
    mDrawerList.setAdapter(adapter);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    // getActionBar().setHomeButtonEnabled(true);
    getActionBar().setDisplayShowTitleEnabled(false);
    getActionBar().setBackgroundDrawable(
            new ColorDrawable(Color.parseColor("#33ffffff")));

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, R.string.empty, R.string.empty) {

        public void onDrawerClosed(View view) {

            invalidateOptionsMenu(); // Setting, Refresh and Rate App
        }

        public void onDrawerOpened(View drawerView) {

            invalidateOptionsMenu();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    mDrawerList.setOnGroupExpandListener(new OnGroupExpandListener() {

        int previousGroup = -1;

        @Override
        public void onGroupExpand(int groupPosition) {
            Toast.makeText(getApplicationContext(),
                    menu_items.get(groupPosition) + " Expanded",
                    Toast.LENGTH_SHORT).show();

            if (groupPosition != previousGroup)
                mDrawerList.collapseGroup(previousGroup);
            previousGroup = groupPosition;

        }
    });

    mDrawerList.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            // TODO Auto-generated method stub
            Toast.makeText(
                    getApplicationContext(),
                    menu_items.get(groupPosition)
                            + " : "
                            + childCollection.get(
                                    menu_items.get(groupPosition)).get(
                                    childPosition), Toast.LENGTH_SHORT)
                    .show();
            groupP = groupPosition;
            childP = childPosition;
            switch (groupPosition) {
            case 0:
                switch (childPosition) {
                case 0:
                    Log.d("CASE Patient ", "Add Patient");
                    fragment = new AddPatient();
                    getFragmentManager().beginTransaction()
                            .replace(R.id.frame_container, fragment)
                            .commit();
                    break;
                case 1:
                    Log.d("CASE Patient ", "View Patient");
                    fragment = new DashboardFragment();
                    getFragmentManager().beginTransaction()
                            .replace(R.id.frame_container, fragment)
                            .commit();
                    break;
    default:
                break;
            }

            mDrawerLayout.closeDrawer(mDrawerList);
            return false;
        }
    });
}

添加患者分数的代码将显示日期选择器是这个..

    public class AddPatient extends Fragment implements OnClickListener {
View rootView;
private EditText edtGender, edtUsername, edtMobNo, edtFname, edtLname;
private static TextView edtDOB;
private TextView Heading, textWelcm;
private Button btnSubmit;
Session session;

public AddPatient() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.addpatient, container, false);
    init();
    Heading = (TextView) rootView.findViewById(R.id.textView1);
    Heading.setText("Add Patient");
    session = new Session(getActivity());
    String userType = session.restoreName(Session.USERTPYE);
    String userN = session.restoreName(Session.FULLNAME);
    textWelcm = (TextView) rootView.findViewById(R.id.textWelcm);
    if (userType.equals("D")) {
        textWelcm
                .setText(Html.fromHtml("<b>Welcome, </b>" + "Dr." + userN));
    } else if (userType.equals("O")) {
        textWelcm.setText(Html
                .fromHtml("<b>Welcome, </b>" + "Opt." + userN));
    }

    return rootView;
}

private void init() {
    // TODO Auto-generated method stub
    edtUsername = (EditText) rootView.findViewById(R.id.edtUsername);
    edtMobNo = (EditText) rootView.findViewById(R.id.edtMobNo);
    edtFname = (EditText) rootView.findViewById(R.id.edtFname);
    edtLname = (EditText) rootView.findViewById(R.id.edtLname);
    edtDOB = (TextView) rootView.findViewById(R.id.edtDOB);
    edtDOB.setOnClickListener(this);
    edtGender = (EditText) rootView.findViewById(R.id.edtGender);
    btnSubmit = (Button) rootView.findViewById(R.id.btnSubmit);
    btnSubmit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
        case R.id.edtDOB:
        Log.d("showDatePickerDialogBox", "showDatePickerDialogBox");
        LayoutInflater inflater = (LayoutInflater) getActivity()
                .getLayoutInflater();
        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                getActivity());
        View customView = inflater.inflate(R.layout.datepicker, null);
        alertDialog.setView(customView);
        final Calendar now = Calendar.getInstance();
        final DatePicker datePicker = (DatePicker) customView
                .findViewById(R.id.dialog_datepicker);
        final TextView dateTextView = (TextView) customView
                .findViewById(R.id.dialog_dateview);
        final SimpleDateFormat dateViewFormatter = new SimpleDateFormat(
                "EEEE, dd.MM.yyyy", Locale.ENGLISH);
        final SimpleDateFormat formatter = new SimpleDateFormat(
                "dd.MM.yyyy", Locale.ENGLISH);
        // Minimum date
        Calendar minDate = Calendar.getInstance();
        try {
            minDate.setTime(formatter.parse("12.12.2010"));
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        datePicker.setMinDate(minDate.getTimeInMillis());
        // View settings
        alertDialog.setTitle("Choose a date");
        Calendar choosenDate = Calendar.getInstance();
        int year = choosenDate.get(Calendar.YEAR);
        int month = choosenDate.get(Calendar.MONTH);
        int day = choosenDate.get(Calendar.DAY_OF_MONTH);
        try {
            Date choosenDateFromUI = formatter
                    .parse(edtDOB.getText().toString());
            choosenDate.setTime(choosenDateFromUI);
            year = choosenDate.get(Calendar.YEAR);
            month = choosenDate.get(Calendar.MONTH);
            day = choosenDate.get(Calendar.DAY_OF_MONTH);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Calendar dateToDisplay = Calendar.getInstance();
        dateToDisplay.set(year, month, day);
        dateTextView.setText(dateViewFormatter.format(dateToDisplay
                .getTime()));
        // Buttons
        alertDialog.setNegativeButton("Go to today",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        edtDOB.setText(formatter
                                .format(now.getTime()));
                        dialog.dismiss();
                    }
                });
        alertDialog.setPositiveButton("Choose",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Calendar choosen = Calendar.getInstance();
                        choosen.set(datePicker.getYear(),
                                datePicker.getMonth(),
                                datePicker.getDayOfMonth());
                        edtDOB.setText(dateViewFormatter.format(choosen
                                        .getTime()));
                        dialog.dismiss();
                    }
                });
        final AlertDialog dialog = alertDialog.create();
        // Initialize datepicker in dialog datepicker
        datePicker.init(year, month, day,
                new DatePicker.OnDateChangedListener() {
                    public void onDateChanged(DatePicker view, int year,
                            int monthOfYear, int dayOfMonth) {
                        Calendar choosenDate = Calendar.getInstance();
                        choosenDate.set(year, monthOfYear, dayOfMonth);
                        dateTextView.setText(dateViewFormatter
                                .format(choosenDate.getTime()));
                        if (choosenDate.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY
                                || now.compareTo(choosenDate) < 0) {
                            dateTextView.setTextColor(Color
                                    .parseColor("#ff0000"));
                            ((Button) dialog
                                    .getButton(AlertDialog.BUTTON_POSITIVE))
                                    .setEnabled(false);
                        } else {
                            dateTextView.setTextColor(Color
                                    .parseColor("#000000"));
                            ((Button) dialog
                                    .getButton(AlertDialog.BUTTON_POSITIVE))
                                    .setEnabled(true);
                        }
                    }
                });
        // Finish
        dialog.show();
    default:
        break;
    }
}

}

修改: 从www.open-sourced.de/show_article.php找到解决方案

1 个答案:

答案 0 :(得分:1)

使用

DialogFragment picker = new AddPatient();
picker.show(getFragmentManager(), "datePicker");

您可以找到更多信息@

http://developer.android.com/reference/android/app/DialogFragment.html

示例:

How to transfer the formatted date string from my DatePickerFragment?