0

好的,我正在开发这个应用程序......它有一个使用 ABS 和滑动菜单库的左侧菜单滑出。这是我在选择菜单项后启动的片段。这个片段应该有一个文本字段来显示您选择的日期和一个按钮来选择一个日期。关于 SelectDateFragment 出错的任何想法?

这是 XML 布局:

    <EditText
        android:id="@+id/dateselected"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="56dp"
        android:ems="10"
        android:inputType="date" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/pickdate"
               android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/dateselected"
        android:layout_toRightOf="@+id/dateselected"
        android:contentDescription="@string/selectdate"
        android:cropToPadding="true"
        android:onClick="selectDate"
        android:src="@drawable/ic_datepicker" />

</RelativeLayout>

这是java类:

import java.util.Calendar;
import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.Fragment;
import android.support.v4.app.DialogFragment;
import android.widget.EditText;


public class DatePicker extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstaceState){
        return inflater.inflate(R.layout.fragment_charting, container, false);
    }

    EditText mEdit;
    @Override
    public void onStart() {
        super.onStart();
    }


    public void selectDate(View view) {
        DialogFragment newFragment = new SelectDateFragment();
        newFragment.show(getFragmentManager(), "DatePicker");
    }

    public void populateSetDate(int year, int month, int day) {
        mEdit = (EditText)getView().findViewById(R.id.dateselected);
        mEdit.setText(month+"/"+day+"/"+year);
    }

    public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            final Calendar calendar = Calendar.getInstance();
            int yy = calendar.get(Calendar.YEAR);
            int mm = calendar.get(Calendar.MONTH);
            int dd = calendar.get(Calendar.DAY_OF_MONTH);
            return new DatePickerDialog(getActivity(), this, yy, mm, dd);
        }

        @Override
        public void onDateSet(android.widget.DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            // TODO Auto-generated method stub
            populateSetDate(year, monthOfYear, dayOfMonth);

        }
    }

}

编辑:我正在尝试关注http://javapapers.com/android/android-datepicker/

我得到的错误是片段内部类应该是静态的。我对这个话题了解得不够多,从阅读更多我仍然不明白,为什么他的作品和我的不明白。

4

1 回答 1

1

试试这个

shipdate.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            showDialog(999);

        }

    });




@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case 999:
        // set date picker as current date
        return new DatePickerDialog(this, datePickerListener, year, month,
                day);
    }
    return null;
}



private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

    // when dialog box is closed, below method will be called.
    public void onDateSet(DatePicker view, int selectedYear,
            int selectedMonth, int selectedDay) {
        final Calendar cf = Calendar.getInstance();
        year = cf.get(Calendar.YEAR);
        month = cf.get(Calendar.MONTH);
        day = cf.get(Calendar.DAY_OF_MONTH);



            // set selected date into textview
            shipdate.setText(new StringBuilder().append(month + 1)
                    .append("-").append(day).append("-").append(year)
                    .append(" "));
        }


};
于 2013-04-29T14:43:14.950 回答