android - 从对话框调用 onCreateDialog()

标签 android android-fragments actionbarsherlock datepickerdialog

我从 SherlockFragmentActivity 创建了一个 SherlockDialogFragment..

这是我到目前为止所做的::

DatePickerFragment newFragment = DatePickerFragment.newInstance(ReportDisplayActivity.this);
newFragment.show(getSupportFragmentManager(), "dialog");

SherlockDialogFragment

public class DatePickerFragment extends SherlockDialogFragment implements android.view.View.OnClickListener
{

    private Button btnOk;
    private TextView txtFromDate;
    private TextView txtToDate;
    private int setFromYear;
    private int setFromMonth;
    private int setFromDay;
    private int setToYear;
    private int setToMonth;
    private int setToDay;

    private final static int DIALOG_FROM_DATE_PICKER = 0;
    private final static int DIALOG_TO_DATE_PICKER = 1;
    private static Context context;

    public DatePickerFragment() {
    }

    public static DatePickerFragment newInstance(Context ctx) {
        DatePickerFragment frag = new DatePickerFragment();
        Bundle args = new Bundle();
        //args.putInt("title", title);
        frag.setArguments(args);
        context=ctx;
        return frag;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
    {
        View  rootView = inflater.inflate(R.layout.temp, container, false);
        txtFromDate=(TextView)rootView.findViewById(R.id.dialog_fromDate);
        txtToDate=(TextView)rootView.findViewById(R.id.dialog_toDate);
        btnOk=(Button)rootView.findViewById(R.id.btn_ok);
        btnOk.setOnClickListener(this);
        txtFromDate.setOnClickListener(this);
        txtToDate.setOnClickListener(this);

        Calendar today = Calendar.getInstance();
        setFromYear = today.get(Calendar.YEAR);
        setFromMonth = today.get(Calendar.MONDAY);
        setFromDay = today.get(Calendar.DAY_OF_MONTH);

        setToYear = today.get(Calendar.YEAR);
        setToMonth = today.get(Calendar.MONDAY);
        setToDay = today.get(Calendar.DAY_OF_MONTH);

        return rootView;
    }

    //@Override
    protected Dialog onCreateDialog(int id)
    {
        switch(id)
        {
        case DIALOG_FROM_DATE_PICKER:
            return new DatePickerDialog(context, mDateFromSetListener, setFromYear, setFromMonth, setFromDay);

        case DIALOG_TO_DATE_PICKER:
            return new DatePickerDialog(context, mDateToSetListener, setToYear, setToMonth, setToDay);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener mDateFromSetListener = new DatePickerDialog.OnDateSetListener(){
        @Override
        public void onDateSet(android.widget.DatePicker view, int year, int month, int day) {
            setFromYear = year;
            setFromMonth = month;
            setFromDay = day;
            //returnDate();
            txtFromDate.setText(setFromDay+("-")+setFromMonth+("-")+setFromYear);
        }
    };

    private DatePickerDialog.OnDateSetListener mDateToSetListener = new DatePickerDialog.OnDateSetListener(){
        @Override
        public void onDateSet(android.widget.DatePicker view, int year, int month, int day) {
            setToYear = year;
            setToMonth = month;
            setToDay = day;
            txtFromDate.setText(setToDay+("-")+setToMonth+("-")+setToYear);
            //returnDate();
        }
    };
    public void onClick(View v) 
    {
        switch (v.getId()) {
        case R.id.btn_ok:
            Toast.makeText(context,"Button ok",Toast.LENGTH_SHORT).show();
            break;

        case R.id.dialog_fromDate :
            Toast.makeText(context,"From Date",Toast.LENGTH_SHORT).show();
            ((Activity) context).showDialog(DIALOG_FROM_DATE_PICKER); 
            break;
        case R.id.dialog_toDate :
            Toast.makeText(context,"To Date",Toast.LENGTH_SHORT).show();
            ((Activity) context).showDialog(DIALOG_TO_DATE_PICKER); 
            break;
        default:
            break;
        }

    }
}

我无法从 SherlockDialogFragment 打开 Datepicker 对话框。我无法在我的实现中找到问题所在。

如果我使用 Activity 而不是 SherlockDialogFragment 那么它工作正常。

编辑::

public class DatePickerFragment extends DialogFragment implements android.view.View.OnClickListener
{

    private Button btnOk;
    private TextView txtFromDate;
    private TextView txtToDate;
    private int setFromYear;
    private int setFromMonth;
    private int setFromDay;
    private int setToYear;
    private int setToMonth;
    private int setToDay;

    private final static int DIALOG_FROM_DATE_PICKER = 0;
    private final static int DIALOG_TO_DATE_PICKER = 1;
    private static Context context;

    public DatePickerFragment() {
    }

    public static DatePickerFragment newInstance(Context ctx) {
        DatePickerFragment frag = new DatePickerFragment();
        Bundle args = new Bundle();
        //args.putInt("title", title);
        frag.setArguments(args);
        context=ctx;
        return frag;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
    {
        View  rootView = inflater.inflate(R.layout.temp, container, false);
        txtFromDate=(TextView)rootView.findViewById(R.id.dialog_fromDate);
        txtToDate=(TextView)rootView.findViewById(R.id.dialog_toDate);
        btnOk=(Button)rootView.findViewById(R.id.btn_ok);
        btnOk.setOnClickListener(this);
        txtFromDate.setOnClickListener(this);
        txtToDate.setOnClickListener(this);

        Calendar today = Calendar.getInstance();
        setFromYear = today.get(Calendar.YEAR);
        setFromMonth = today.get(Calendar.MONDAY);
        setFromDay = today.get(Calendar.DAY_OF_MONTH);

        setToYear = today.get(Calendar.YEAR);
        setToMonth = today.get(Calendar.MONDAY);
        setToDay = today.get(Calendar.DAY_OF_MONTH);

        return rootView;
    }

    //@Override
    protected Dialog onCreateDialog(int id)
    {
        switch(id)
        {
        case DIALOG_FROM_DATE_PICKER:
            return new DatePickerDialog(context, mDateFromSetListener, setFromYear, setFromMonth, setFromDay);

        case DIALOG_TO_DATE_PICKER:
            return new DatePickerDialog(context, mDateToSetListener, setToYear, setToMonth, setToDay);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener mDateFromSetListener = new DatePickerDialog.OnDateSetListener(){
        @Override
        public void onDateSet(android.widget.DatePicker view, int year, int month, int day) {
            setFromYear = year;
            setFromMonth = month;
            setFromDay = day;
            //returnDate();
            txtFromDate.setText(setFromDay+("-")+setFromMonth+("-")+setFromYear);
        }
    };

    private DatePickerDialog.OnDateSetListener mDateToSetListener = new DatePickerDialog.OnDateSetListener(){
        @Override
        public void onDateSet(android.widget.DatePicker view, int year, int month, int day) {
            setToYear = year;
            setToMonth = month;
            setToDay = day;
            txtFromDate.setText(setToDay+("-")+setToMonth+("-")+setToYear);
            //returnDate();
        }
    };
    public void onClick(View v) 
    {
        switch (v.getId()) {
        case R.id.btn_ok:
            Toast.makeText(context,"Button ok",Toast.LENGTH_SHORT).show();
            break;

        case R.id.dialog_fromDate :
            Toast.makeText(context,"From Date",Toast.LENGTH_SHORT).show();
            ((Activity) context).showDialog(DIALOG_FROM_DATE_PICKER); 
            showDatePickerDialog(txtFromDate);
            break;
        case R.id.dialog_toDate :
            Toast.makeText(context,"To Date",Toast.LENGTH_SHORT).show();
            ((Activity) context).showDialog(DIALOG_TO_DATE_PICKER); 
            break;
        default:
            break;
        }
    }

    public void showDatePickerDialog(View v) 
    {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "datePicker");
    }
}

逻辑猫::

01-02 15:37:55.013: E/AndroidRuntime(933): FATAL EXCEPTION: main
01-02 15:37:55.013: E/AndroidRuntime(933): java.lang.NoClassDefFoundError: com.example.myapp.home.DatePickerFragment
01-02 15:37:55.013: E/AndroidRuntime(933):  at com.example.myapp.report.ReportDisplayActivity$2.onItemSelected(ReportDisplayActivity.java:275)
01-02 15:37:55.013: E/AndroidRuntime(933):  at com.actionbarsherlock.internal.widget.IcsAdapterView.fireOnSelected(IcsAdapterView.java:861)
01-02 15:37:55.013: E/AndroidRuntime(933):  at com.actionbarsherlock.internal.widget.IcsAdapterView.access$2(IcsAdapterView.java:854)
01-02 15:37:55.013: E/AndroidRuntime(933):  at com.actionbarsherlock.internal.widget.IcsAdapterView$SelectionNotifier.run(IcsAdapterView.java:827)
01-02 15:37:55.013: E/AndroidRuntime(933):  at android.os.Handler.handleCallback(Handler.java:587)
01-02 15:37:55.013: E/AndroidRuntime(933):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-02 15:37:55.013: E/AndroidRuntime(933):  at android.os.Looper.loop(Looper.java:130)
01-02 15:37:55.013: E/AndroidRuntime(933):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-02 15:37:55.013: E/AndroidRuntime(933):  at java.lang.reflect.Method.invokeNative(Native Method)
01-02 15:37:55.013: E/AndroidRuntime(933):  at java.lang.reflect.Method.invoke(Method.java:507)
01-02 15:37:55.013: E/AndroidRuntime(933):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-02 15:37:55.013: E/AndroidRuntime(933):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-02 15:37:55.013: E/AndroidRuntime(933):  at dalvik.system.NativeStart.main(Native Method)

编辑 2::

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp"
    android:versionCode="3"
    android:versionName="3.0.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:name=".MyApplication"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/Custom_ThemeSherlockLight" >
        <activity
            android:name=".SplashActivity"
            android:clearTaskOnLaunch="true"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@style/Custom_ThemeSherlockLight" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Login"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@style/Custom_ThemeSherlockLight"
            android:windowSoftInputMode="stateHidden" >
        </activity>

        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait"
            android:theme="@style/Theme.Sherlock.Light"
            android:windowSoftInputMode="stateHidden" />

        <!-- Reports -->

        <activity
            android:name=".report.ReportDisplayActivity"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateHidden" >
        </activity>

        <activity
            android:name=".home.DateSelector"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateHidden" >
        </activity>
    </application>

</manifest>

temp.xml(布局文件)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/popup_top_up"
    android:orientation="vertical"
    android:paddingBottom="5dp" >

    <TextView
        android:id="@+id/lbl_fromDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="45dp"
        android:text="From Date :"
        android:textColor="#000000"
        android:textSize="20sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/dialog_fromDate"
        style="@style/DropDownNav.Custom_ThemeSherlockLight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/lbl_fromDate"
        android:hint="@string/lbl_select_from_date"
        android:layout_alignBottom="@+id/lbl_fromDate"
        android:layout_marginLeft="15dp"
        android:layout_toRightOf="@+id/lbl_fromDate"
        android:text="@string/lbl_select_from_date"
        android:textColor="#000000"
        android:textSize="12sp"/>

    <TextView
        android:id="@+id/lbl_toDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/lbl_fromDate"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/lbl_fromDate"
        android:layout_marginTop="22dp"
        android:text="To Date :"
        android:textColor="#000000"
        android:textSize="20sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/dialog_toDate"
        style="@style/DropDownNav.Custom_ThemeSherlockLight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/lbl_toDate"
        android:layout_alignBottom="@+id/lbl_toDate"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@+id/lbl_toDate"
        android:text="@string/lbl_select_to_date"
        android:textColor="#000000"
        android:textSize="12sp"/>

    <Button
        android:id="@+id/btn_ok"
        android:layout_width="81dip"
        android:layout_height="35dp"
        android:layout_below="@+id/dialog_toDate"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="45dp"
        android:background="@drawable/button_selector"
        android:text="OK"
        android:textColor="@color/white" />

</RelativeLayout>

我可以像这样弹出对话框,但是当我点击文本“从日期选择”或“选择到日期”(从我想显示日期选择器对话框的地方)时,日期选择器对话框不显示。

enter image description here

样式.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="Custom_ThemeSherlockLight" parent="@style/Theme.Sherlock.Light">
        <item name="actionBarItemBackground">@drawable/selectable_background_exampthemesherlocklightle</item>
        <item name="popupMenuStyle">@style/PopupMenu.Custom_ThemeSherlockLight</item>
        <item name="dropDownListViewStyle">@style/DropDownListView.Custom_ThemeSherlockLight</item>
        <item name="actionBarTabStyle">@style/ActionBarTabStyle.Custom_ThemeSherlockLight</item>
        <item name="actionDropDownStyle">@style/DropDownNav.Custom_ThemeSherlockLight</item>
        <item name="actionBarStyle">@style/ActionBar.Solid.Custom_ThemeSherlockLight</item>
        <item name="actionModeBackground">@drawable/cab_background_top_exampthemesherlocklightle</item>
        <item name="actionModeSplitBackground">@drawable/cab_background_bottom_exampthemesherlocklightle</item>
        <item name="actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Custom_ThemeSherlockLight</item>
    </style>

    <style name="ActionBar.Solid.Custom_ThemeSherlockLight" parent="@style/Widget.Sherlock.Light.ActionBar.Solid">
        <item name="background">@drawable/ab_solid_exampthemesherlocklightle</item>
        <item name="backgroundStacked">@drawable/ab_stacked_solid_exampthemesherlocklightle</item>
        <item name="backgroundSplit">@drawable/ab_bottom_solid_exampthemesherlocklightle</item>
        <item name="progressBarStyle">@style/ProgressBar.Custom_ThemeSherlockLight</item>
    </style>

    <style name="ActionBar.Transparent.Custom_ThemeSherlockLight" parent="@style/Widget.Sherlock.Light.ActionBar">
        <item name="background">@drawable/ab_transparent_exampthemesherlocklightle</item>
        <item name="progressBarStyle">@style/ProgressBar.Custom_ThemeSherlockLight</item>
    </style>

    <style name="PopupMenu.Custom_ThemeSherlockLight" parent="@style/Widget.Sherlock.Light.ListPopupWindow">
        <item name="android:popupBackground">@drawable/menu_dropdown_panel_exampthemesherlocklightle</item>
    </style>

    <style name="DropDownListView.Custom_ThemeSherlockLight" parent="@style/Widget.Sherlock.Light.ListView.DropDown">
        <item name="android:listSelector">@drawable/selectable_background_exampthemesherlocklightle</item>
    </style>

    <style name="ActionBarTabStyle.Custom_ThemeSherlockLight" parent="@style/Widget.Sherlock.Light.ActionBar.TabView">
        <item name="android:background">@drawable/tab_indicator_ab_exampthemesherlocklightle</item>
    </style>

    <style name="DropDownNav.Custom_ThemeSherlockLight" parent="@style/Widget.Sherlock.Light.Spinner.DropDown.ActionBar">
        <item name="android:dropDownSelector">@drawable/selectable_background_exampthemesherlocklightle</item>
    </style>

    <style name="ProgressBar.Custom_ThemeSherlockLight" parent="@style/Widget.Sherlock.Light.ProgressBar.Horizontal">
        <item name="android:progressDrawable">@drawable/progress_horizontal_exampthemesherlocklightle</item>
    </style>

    <style name="ActionButton.CloseMode.Custom_ThemeSherlockLight" parent="@style/Widget.Sherlock.Light.ActionButton.CloseMode">
        <item name="android:background">@drawable/btn_cab_done_exampthemesherlocklightle</item>
    </style>

    <!-- this style is only referenced in a Light.DarkActionBar based theme -->
    <style name="Theme.Custom_ThemeSherlockLight.Widget" parent="@style/Theme.Sherlock">
        <item name="popupMenuStyle">@style/PopupMenu.Custom_ThemeSherlockLight</item>
        <item name="dropDownListViewStyle">@style/DropDownListView.Custom_ThemeSherlockLight</item>
    </style>

    <style name="Theme.Sherlock.Translucent" parent="@style/Theme.Sherlock">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:backgroundDimEnabled">false</item>
        <item name="android:backgroundDimEnabled">true</item>
    </style>

</resources>

最佳答案

这是解决方案,

主要 Activity

public class MainActivity extends SherlockFragmentActivity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        DatePickerFragment newFragment = DatePickerFragment.newInstance(this);
        newFragment.show(ft, "dialog");
    }
}

主.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

</RelativeLayout>

日期选择器 fragment

public class DatePickerFragment extends SherlockDialogFragment implements android.view.View.OnClickListener
{
    private final static int DIALOG_FROM_DATE_PICKER = 0;
    private final static int DIALOG_TO_DATE_PICKER = 1;
    private static Context context;
    private Button btnOk;
    private TextView txtFromDate;
    private TextView txtToDate;
    private int setFromYear;
    private int setFromMonth;
    private int setFromDay;
    private int setToYear;
    private int setToMonth;
    private int setToDay;
    private int dialog=0;

    public static DatePickerFragment newInstance( Context ctx ) 
    {
        DatePickerFragment frag = new DatePickerFragment();
        Bundle args = new Bundle();
        frag.setArguments(args);
        context=ctx;
        return frag;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
    {
        View  rootView = inflater.inflate(R.layout.fragment_dialog, container, false);
        txtFromDate=(TextView)rootView.findViewById(R.id.dialog_fromDate);
        txtToDate=(TextView)rootView.findViewById(R.id.dialog_toDate);
        btnOk=(Button)rootView.findViewById(R.id.btn_ok);
        btnOk.setOnClickListener(this);
        txtFromDate.setOnClickListener(this);
        txtToDate.setOnClickListener(this);

        Calendar today = Calendar.getInstance();
        setFromYear = today.get(Calendar.YEAR);
        setFromMonth = today.get(Calendar.MONTH);
        setFromDay = today.get(Calendar.DAY_OF_MONTH);

        setToYear = today.get(Calendar.YEAR);
        setToMonth = today.get(Calendar.MONTH);
        setToDay = today.get(Calendar.DAY_OF_MONTH);

        return rootView;
    }

    private void showDatePickerDialog ( int id )
    {
        switch ( id )
        {
            case DIALOG_FROM_DATE_PICKER:
                new DatePickerDialog(context, mDateSetListener, setFromYear, setFromMonth, setFromDay).show();
                break;
            case DIALOG_TO_DATE_PICKER:
                new DatePickerDialog(context, mDateSetListener, setToYear, setToMonth, setToDay).show();
        }
    }

    @Override
    public void onClick( View v ) 
    {
        switch (v.getId()) 
        {
            case R.id.btn_ok:
                Toast.makeText(context,"Button ok",Toast.LENGTH_SHORT).show();
                break;
            case R.id.dialog_fromDate :
                dialog = DIALOG_FROM_DATE_PICKER;
                showDatePickerDialog (DIALOG_FROM_DATE_PICKER);
                break;
            case R.id.dialog_toDate :
                dialog = DIALOG_TO_DATE_PICKER;
                showDatePickerDialog(DIALOG_TO_DATE_PICKER);
                break;
            default:
                break;
        }           
    }

    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener()
    {
        @Override
        public void onDateSet(android.widget.DatePicker view, int year, int month, int day) 
        {
            if ( dialog == DIALOG_FROM_DATE_PICKER )
            {
                setFromYear = year;
                setFromMonth = month+1;
                setFromDay = day;
                txtFromDate.setText(setFromDay+("-")+setFromMonth+("-")+setFromYear);
            }
            else if ( dialog == DIALOG_TO_DATE_PICKER )
            {
                setToYear = year;
                setToMonth = month+1;
                setToDay = day;
                txtToDate.setText(setToDay+("-")+setToMonth+("-")+setToYear);
            }
        }
    };
}

fragment _对话框.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingBottom="5dp" >

    <TextView
        android:id="@+id/lbl_fromDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="45dp"
        android:text="From Date :"
        android:textColor="#000000"
        android:textSize="20sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/dialog_fromDate"
        style="@style/DropDownNav.Custom_ThemeSherlockLight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/lbl_fromDate"
        android:hint="@string/lbl_select_from_date"
        android:layout_alignBottom="@+id/lbl_fromDate"
        android:layout_marginLeft="15dp"
        android:layout_toRightOf="@+id/lbl_fromDate"
        android:text="@string/lbl_select_from_date"
        android:textColor="#000000"
        android:textSize="12sp"/>

    <TextView
        android:id="@+id/lbl_toDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/lbl_fromDate"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/lbl_fromDate"
        android:layout_marginTop="22dp"
        android:text="To Date :"
        android:textColor="#000000"
        android:textSize="20sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/dialog_toDate"
        style="@style/DropDownNav.Custom_ThemeSherlockLight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/lbl_toDate"
        android:layout_alignBottom="@+id/lbl_toDate"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@+id/lbl_toDate"
        android:text="@string/lbl_select_to_date"
        android:textColor="#000000"
        android:textSize="12sp"/>

    <Button
        android:id="@+id/btn_ok"
        android:layout_width="81dip"
        android:layout_height="35dp"
        android:layout_below="@+id/dialog_toDate"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="45dp"
        android:text="OK"
        android:textColor="@color/white" />

</RelativeLayout>

style.xml 与您的代码相同。

我在您的代码中发现您是通过以下语句获取月份 setFromMonth = today.get(Calendar.MONDAY);setToMonth = today.get(Calendar.MONDAY); 。我相信你做错了。我还通过编写一个通用的 mDateSetListener 监听器来最小化您的代码。另请记住,Calendar.MONTH 对 1 月返回 0,因为它的索引以 0 开头。在将月份值设置为相应的 TextView 之前,我在监听器中添加了 +1。

关于android - 从对话框调用 onCreateDialog(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20879488/

相关文章:

java - File.mkdirs() 是否需要 WRITE_EXTERNAL_STORAGE 权限

android - 将 Google places API 插入 Fragment

android - onActivityResult() 未调用

java - 如何在一个应用程序中创建 2 个操作栏?

android - 按下时如何更改主页按钮突出显示颜色

javascript - 样式化 React Native Picker

javascript - Android 中的 KeyPress 函数

android - 如何在 Fragment 内的 WebView 中添加 "Go Back"函数?

android - 如何在android中实现滑动菜单

android - 使用 multidex 和 Dagger 2 时,Application 对象中缺少函数