Gon*_*Cao 52 android datepicker android-datepicker android-dialog
我正在使用谷歌示例使用对话框片段在我的应用程序中插入一个日期选择器
http://developer.android.com/guide/topics/ui/controls/pickers.html
但是我不确定如何在设置之后获得日期(不是java专家).对话框和日期选择器运行正常,我可以记录该日期是否已正确设置但是,如何才能在父活动上执行回调?
这是我的Dialog片段
public class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
**Log.w("DatePicker","Date = " + year);**
}
}
Run Code Online (Sandbox Code Playgroud)
......我用我的活动打电话给对话...
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
Run Code Online (Sandbox Code Playgroud)
在我的父活动中调用方法而不是Log.w的正确方法是什么?我想这与将回调作为参数或其他东西相关联,但我在互联网上找到的大多数参考文献都是关于没有对话碎片的先前版本
编辑:不确定它是否重要,但父活动被声明为:
public class EditSessionActivity extends FragmentActivity {
Run Code Online (Sandbox Code Playgroud)
解决方案:感谢Lecho用户,这是一种方法
DatePickerFragmennt.class
public class DatePickerFragment extends DialogFragment{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), (EditSessionActivity)getActivity(), year, month, day);
}
}
Run Code Online (Sandbox Code Playgroud)
...和父活动EditSessionActivity.class ...
public class EditSessionActivity extends FragmentActivity implements OnDateSetListener {
...
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
//do some stuff for example write on log and update TextField on activity
Log.w("DatePicker","Date = " + year);
((EditText) findViewById(R.id.tf_date)).setText("Date = " + year);
}
Run Code Online (Sandbox Code Playgroud)
Les*_*zek 64
构造函数FO DatePickerDialog需要DatePickerDialog.OnDateSetListener作为第二个参数,那么也许你应该实现这个接口在父活动EditSessionActivity(不DatePickerFragment),并改变这一行:
return new DatePickerDialog(getActivity(), this, year, month, day);
Run Code Online (Sandbox Code Playgroud)
进入这个:
return new DatePickerDialog(getActivity(), (EditSessionActivity)getActivity(), year, month, day);
Run Code Online (Sandbox Code Playgroud)
然后你的活动应该是这样的:
public class EditSessionActivity extends FragmentActivity implements
DatePickerDialog.OnDateSetListener{
public void onDateSet(DatePicker view, int year, int month, int day) {
//use date in your activity
}
...
}
Run Code Online (Sandbox Code Playgroud)
小智 25
我在我的类中创建日期选择器时简单地覆盖onDateSet,显示日期选择器.见下面的代码:
private OnClickListener onDateClicked() {
return new OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = new DatePickerFragment() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
salePaymentCustomView.setBtDateText("" + day + "/" + month+1 + "/" + year);
}
};
newFragment.show(getActivity().getSupportFragmentManager(), "datePicker");
}
};
}
Run Code Online (Sandbox Code Playgroud)
ste*_*eps 14
也可以在不将onDateSet方法移动到父活动的情况下解决问题.您只需要告诉DatePickerFragment搜索视图的位置:
public class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// do some stuff for example write on log and update TextField on activity
Log.w("DatePicker","Date = " + year);
((TextView) getActivity().findViewById(R.id.tf_date)).setText("Date = " + year);
}
}
Run Code Online (Sandbox Code Playgroud)
我也改变了转换的目标从EditText到TextView.这样,您可以将您的实现重用于不同类型的视图,而不仅仅是用于EditText.
我对上述答案进行了一次修正.而不是像这样返回DatePickerDialog
返回新的DatePickerDialog(getActivity(),(EditSessionActivity)getActivity(),年,月,日);
你应该以更通用的方式返回它
返回新的DatePickerDialog(getActivity(),(OnDateSetListener)getActivity(),年,月,日);
只需确保您的活动实现OnDateSetListener接口并覆盖函数onDateSet
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
71444 次 |
| 最近记录: |