如何在android中添加带textview的时间选择器

时间:2016-09-24 05:20:41

标签: java android time timepicker

enter image description here

我想设计带有textview时间选择器的对话框。 我已经为xml编写代码,但是java部分有问题。

如何为此对话框编写java代码。

once.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                LayoutInflater layoutInflaterAndroid = LayoutInflater.from(getApplicationContext());
                View mView = layoutInflaterAndroid.inflate(R.layout.add_time_quantity, null);

                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(Reminders.this, R.style.myDialog));

                alertDialogBuilder.setView(mView);

                //To set the dialog box title in center
                TextView title = new TextView(Reminders.this);
                // You Can Customise your Title here
                title.setText("Set Time And Quantity");
                title.setBackgroundColor(Color.DKGRAY);
                title.setPadding(10, 10, 10, 10);
                title.setGravity(Gravity.CENTER);
                title.setTextColor(Color.WHITE);
                title.setTextSize(20);

                alertDialogBuilder.setCustomTitle(title);


                MedicineQuantity=(TextView) mView.findViewById(R.id.setdosequantity);
                timePicker=(TimePicker) mView.findViewById(R.id.timePicker);
                increseQuantity=(Button) mView.findViewById(R.id.increase);
                decreaseQuantity=(Button) mView.findViewById(R.id.decrease);

                Calendar calendar=Calendar.getInstance();

                hour=calendar.get(Calendar.HOUR_OF_DAY);
                min=calendar.get(Calendar.MINUTE);

                if (hour == 0) {
                    hour += 12;
                    format = "AM";
                }
                else if (hour == 12) {
                    format = "PM";
                } else if (hour > 12) {
                    hour -= 12;
                    format = "PM";
                } else {
                    format = "AM";
                }


                increseQuantity.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        floatvalue+=0.5f;
                        MedicineQuantity.setText(Float.toString(floatvalue));
                    }
                });

                decreaseQuantity.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        floatvalue-=0.5f;
                        MedicineQuantity.setText(Float.toString(floatvalue));
                    }
                });



                alertDialogBuilder
                        .setCancelable(false)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {



                                medicineTime1.setText(new StringBuilder().append(hour).append(" : ").append(min).append(" ").append(format));
                                medicineDose1.setText(MedicineQuantity.getText().toString());
                            }
                        })
                        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        });


                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();


            }
        });

    }

仅提供当前时间而非更新时间。我该怎么做

0 个答案:

没有答案