Android中自定义对话框中的日期选择器 [英] Date picker inside custom dialog in Android

查看:667
本文介绍了Android中自定义对话框中的日期选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在自定义对话框中使用日期选择器.在按钮上单击日历将打开以选择用户的日期.我的customDilaog类中有Button,然后在该按钮上单击我要打开日历视图".如果单击此按钮,我的应用程序将崩溃. 我已经完成了.

I wanted to use date picker inside my custom dialog. On button click calendar will open to choose date for user. I have Button inside my customDilaog class and on that button click I want to open calendar view. My app crashes if click this button. I have done with this.

CustomDialog.java

public class CustomDialog extends Dialog implements android.view.View.OnClickListener {

     private Button date;
     DateFormat format = DateFormat.getDateInstance();
     Calendar calender = Calendar.getInstance();

     public CustomDialog(Context context) {
         super(context);
     }

     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
              date=(Button)findViewById(R.id.dateText2);
     }
     public void onClick(View v) {
         if (v.getId()==R.id.datePick) {
             openDatePicker(v);
         //another if-else statements
         }

     public void updateDate(){
          date.setText(format.format(calender.getTime()));
     }

     public void setDate(){
           new DatePickerDialog(getContext(),dp,calender.get(Calendar.YEAR) ,calender.get(Calendar.MONTH),calender.get(Calendar.DAY_OF_MONTH)).show();                   
     }

         DatePickerDialog.OnDateSetListener dp = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
                calender.set(Calendar.YEAR,year);
                calender.set(Calendar.MONTH, monthOfYear);
                calender.set(Calendar.DAY_OF_MONTH, dayOfMonth);
               updateDate();                
            }
        };
     }

我不知道我在哪里弄错了.

I don't know where I made mistake.

推荐答案

正确使用此功能

 @OnClick(R.id.fab)
public void fabbuttonclicked(){
    // get prompts.xml view
    LayoutInflater layoutInflater = LayoutInflater.from(context.getContext());

      View promptView = layoutInflater.inflate(R.layout.dialog_add_new_book, null);


    final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context.getContext());

    // set prompts.xml to be the layout file of the alertdialog builder
    alertDialogBuilder.setView(promptView);

    final EditText titlebookEditText = (EditText) promptView.findViewById(R.id.book_title_edit_text);
    final EditText authorbookEditText=(EditText) promptView.findViewById(R.id.book_author_edit_text);
    final Button dateboughtbookButton=(Button) promptView.findViewById(R.id.book_datebought_button);
    final EditText conditionbookEditText=(EditText) promptView.findViewById(R.id.book_condition_edit_text);
    final EditText urlimagebookEditText=(EditText) promptView.findViewById(R.id.book_Image_edit_text);

// Show a datepicker when the dateButton is clicked
    dateboughtbookButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Calendar now = Calendar.getInstance();
            final Calendar c = Calendar.getInstance();

            DatePickerDialog dpd = new DatePickerDialog(context.getContext(),
                    new DatePickerDialog.OnDateSetListener() {

                        @Override
                        public void onDateSet(DatePicker view, int year,
                                              int monthOfYear, int dayOfMonth) {
                            dateboughtbookButton.setText(dayOfMonth + "-"
                                    + (monthOfYear + 1) + "-" + year);

                        }
                    }, c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DATE));
                    dpd.show();


            }
        }

        );


        alertDialogBuilder
                .setCancelable(false)
                .

        setPositiveButton("Add Alarm",new DialogInterface.OnClickListener() {
            @Override
            public void onClick (DialogInterface dialog,int id){
                String titlebook = titlebookEditText.getText().toString();
                String authorbook = authorbookEditText.getText().toString();
                String dateboughtbook = dateboughtbookButton.getText().toString();
                String conditionbook = conditionbookEditText.getText().toString();
                String urlimagebook = urlimagebookEditText.getText().toString();


                if (titlebook.isEmpty()) {
                    Toast.makeText(getActivity(), " Amount is required ! \n Alarm not set", Toast.LENGTH_LONG).show();
                    return;
                } else {
                    int nextID;
                    nextID = (int) (realm.where(Book.class).maximumInt("bookId") + 1);
                    Book book = new Book(nextID, titlebook, authorbook, dateboughtbook, conditionbook, urlimagebook);

                    realm.beginTransaction();
                    realm.copyToRealmOrUpdate(book);
                    realm.commitTransaction();

                    dialog.dismiss();
                    loadBooks();
                    Toast.makeText(getActivity(), " New BOOK added ! \n ", Toast.LENGTH_LONG).show();

                    //Toast.makeText(getActivity(), "Alarm is Set", Toast.LENGTH_LONG).show();
                }
            }
        }

        )
                .

        setNegativeButton("Cancel",
                                  new DialogInterface.OnClickListener() {
            public void onClick (DialogInterface dialog,int id){
                dialog.cancel();
            }
        });

    // create an alert dialog
    AlertDialog alertD = alertDialogBuilder.create();

    alertD.show();

}

这篇关于Android中自定义对话框中的日期选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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