我有一个Fragment
,要求用户输入日期。按下按钮后,我使用DatePickerDialog
获取该信息,启动对话框。但我想知道是否可以将DatePickerDialog
嵌套在片段中,因此用户只需与日历进行交互,而不是按下按钮。我已经设法构建了一个示例并使用XML小部件进行显示,但是我没有运气弄清楚如何编写代码来处理数据。这是代码:
这是对话框:
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class DatePickerFragment extends DialogFragment {
private static final String ARG_DATE = "date";
public static final String EXTRA_DATE = "extraDate";
private DatePicker mDatePicker;
public static DatePickerFragment newInstance(Date date){
Bundle args = new Bundle();
args.putSerializable(ARG_DATE, date);
DatePickerFragment fragment = new DatePickerFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public Dialog onCreateDialog(Bundle SavedInstanceState){
Date date = (Date) getArguments().getSerializable(ARG_DATE);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
View view = LayoutInflater.from(getActivity()).inflate(R.layout.dailog_date, null);
mDatePicker = (DatePicker) view.findViewById(R.id.dialog_date_date_picker);
mDatePicker.init(year, month, day, null);
return new AlertDialog.Builder(getActivity()).setView(view).setTitle(R.string.date_picker_title).setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int year = mDatePicker.getYear();
int month = mDatePicker.getMonth();
int day = mDatePicker.getDayOfMonth();
Date date = new GregorianCalendar(year, month, day).getTime();
sendResult(Activity.RESULT_OK, date);
}
}).create();
}
private void sendResult(int resultCode, Date date){
if(getTargetFragment() == null){
return;
}
Intent intent = new Intent();
intent.putExtra(EXTRA_DATE, date);
getTargetFragment().onActivityResult(getTargetRequestCode(), resultCode, intent);
}
}
答案 0 :(得分:0)
创建一个片段,只需通过添加一个Listener来处理DatePicker小部件就像任何其他类型的视图一样。
Dim leftCol as Integer
leftCol = ws1.Cells.Find("*", SearchOrder:=xlByColumns, searchdirection:=xlNext).Column
ws2.Cells(oRow, c).value = ws1.Cells(r, leftCol).value
和相应的xml:
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.Toast;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class DateFragmentTest extends Fragment {
private DatePicker datePicker;
public static DateFragmentTest newInstance(){
return new DateFragmentTest();
}
public DateFragmentTest(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.date_picker_test, container , false);
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
datePicker = (DatePicker) view.findViewById(R.id.date_picker_widget);
datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker datePicker, int i, int i1, int i2) {
Date date = new GregorianCalendar(i, i1, i2).getTime();
// do something with the date here
}
});
return view;
}
}