4

我有一个工作正常的 Datepickerdialog 片段。当我单击按钮时,datepickerdialog 打开,左侧有微调器日期选择,右侧有日历视图。

对于我在一个地方的应用程序,我只想显示微调器日期选择部分(必须删除或隐藏日历视图部分)

在其他地方,我希望必须显示日历视图部分,并且必须隐藏或删除微调器日期选择。

但是我尝试了很多不同的东西,比如隐藏日历视图,比如

 Method m = minDateSelector.getClass().getMethod("setCalendarViewShown", boolean.class);
 m.invoke(dp, false);

但是当我添加这个时,我只会得到错误......

这个人也有同样的问题 Datepicker 创建的 DialopFragment 也显示日历

我的代码是

DatePickerActivity.java

import java.util.Calendar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class DatePickerActivity extends Activity {
    DateDialogFragment frag;
    Button button;
    Calendar now;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        now = Calendar.getInstance();
        button = (Button)findViewById(R.id.date_button);
        button.setText(String.valueOf(now.get(Calendar.MONTH)+1)+"-"+String.valueOf(now.get(Calendar.DAY_OF_MONTH))+"-"+String.valueOf(now.get(Calendar.YEAR)));
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog();   
            }
        });  
    }
    public void showDialog() {
        FragmentTransaction ft = getFragmentManager().beginTransaction(); //get the fragment
        frag = DateDialogFragment.newInstance(this, new DateDialogFragmentListener(){
            public void updateChangedDate(int year, int month, int day){
                button.setText(String.valueOf(month+1)+"-"+String.valueOf(day)+"-"+String.valueOf(year));
                now.set(year, month, day);
            }
        }, now);

        frag.show(ft, "DateDialogFragment");
    }
    public interface DateDialogFragmentListener{
        //this interface is a listener between the Date Dialog fragment and the activity to update the buttons date
        public void updateChangedDate(int year, int month, int day);
    }  
}

DateDialogFragment.java

import java.util.Calendar;

import com.zeroe.DatePickerActivity.DateDialogFragmentListener;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.widget.DatePicker;

public class DateDialogFragment extends DialogFragment {

    public static String TAG = "DateDialogFragment";
    static Context mContext; //I guess hold the context that called it. Needed when making a DatePickerDialog. I guess its needed when conncting the fragment with the context
    static int mYear;
    static int mMonth;
    static int mDay;
    static DateDialogFragmentListener mListener;

    public static DateDialogFragment newInstance(Context context, DateDialogFragmentListener listener, Calendar now) {
        DateDialogFragment dialog = new DateDialogFragment();
        mContext = context;
        mListener = listener;

        mYear = now.get(Calendar.YEAR);
        mMonth = now.get(Calendar.MONTH);
        mDay = now.get(Calendar.DAY_OF_MONTH);
        /*I dont really see the purpose of the below*/
        Bundle args = new Bundle();
        args.putString("title", "Choose Date");
        dialog.setArguments(args);//setArguments can only be called before fragment is attached to an activity, so right after the fragment is created


        return dialog;
    }
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new DatePickerDialog(mContext, mDateSetListener, mYear, mMonth, mDay);
    }
    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;

            mListener.updateChangedDate(year, monthOfYear, dayOfMonth);
        }
    };
}

主要的.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button 
        android:id="@+id/date_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="03-18-2012"
        android:onClick="clickMe"/>

</LinearLayout>
4

1 回答 1

10

我使用了 Datepicker Dialog Fragment,在对话框中显示日期选择器。

这是我的代码:

MainActivity.java

package com.example.testmydrag;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.DatePicker;
import android.support.v4.app.FragmentActivity;
import android.app.Dialog;
import android.app.DatePickerDialog;
import android.support.v4.app.DialogFragment;

import java.util.Calendar;

public class MainActivity extends FragmentActivity {

EditText mEdit;
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void selectDate(View view) {

        DialogFragment newFragment = new SelectDateFragment();
        newFragment.show(getSupportFragmentManager(), "DatePicker");

    }

    public void setTheDate(int year, int month, int day) {

        mEdit = (EditText)findViewById(R.id.Text);
        mEdit.setText(month+"/"+day+"/"+year);
    }
    /*
     * Date Picker Dialog
     */
    public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {

            final Calendar c = Calendar.getInstance();
            int yy = c.get(Calendar.YEAR);
            int mm = c.get(Calendar.MONTH);
            int dd = c.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog dialog = new DatePickerDialog(getActivity(),this,c.YEAR, c.MONTH, c.DATE);

            /*Calendar View if you want to Remove Set it to False*/
            dialog.getDatePicker().setCalendarViewShown(true);

            /*Spinner View if you want to Show Set it to True*/
            dialog.getDatePicker().setSpinnersShown(false);

            dialog.setTitle("Pick a date");
            return dialog;
        }
        public void onDateSet(DatePicker view, int yy, int mm, int dd) {

            setTheDate(yy, mm+1, dd);
        }
    }

}

主要的.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <EditText android:text="@+string/date_text" 
        android:id="@+id/Text" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" />
    <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:text="@+string/pick_date" 
        android:onClick="selectDate" />

</LinearLayout>
于 2013-02-04T17:07:46.020 回答