我已设法创建日期选择器类但现在我希望将日期选择器对话框中的值返回到主活动类。以下是日期选择器对话框的代码。
public class DateChooser extends DialogFragment implements DatePickerDialog.OnDateSetListener{
public int y,m,d;
public Dialog onCreateDialog(Bundle savedInstanceState)
{
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);
return new DatePickerDialog(getContext(),this,year,month,day);
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Toast.makeText(getContext(), "Toast!", Toast.LENGTH_SHORT).show();
setDay(dayOfMonth);
setMonth(month);
setYear(year);
}
我从mainactivity调用了这个类。
public void dateSelect(View view)
{
DateChooser dateC = new DateChooser();
dateC.show(getSupportFragmentManager(),"tag");
}
答案 0 :(得分:0)
您可以创建一个界面,例如像这样
var city_array = selectedText.split(',');
并在您的活动中实施
$('#previewButton').click(function () {
for (i = 0; i < city_array.length; i++) {
'<button class="btn btn-primary waves-effect waves-light btn-xs">' + city_array[i] + '</button>;';
}
});
然后你必须在你的活动中实现这个方法......
在public interface DateListener {
void onDateSelected(int day, int month, int year);
}
添加以下字段:
public class YourActivity extends Activity implements DateListener {...}
并将此代码添加到您的片段Fragment
:
private DateListener dateListener;
现在您已将活动设置为DateListener,并可在onAttach
答案 1 :(得分:0)
DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker1);
int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth() + 1;
int year = datePicker.getYear();
String dateInString=year+"-"+month+"-"+day;
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = formatter.parse(dateInString);
System.out.println(date);
答案 2 :(得分:0)
这样做:
将此添加到DateChooser类:
private DateSelectedListener mListener;
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
mListener.onDateSelected(view, year, month, day);
}
public void display(FragmentManager manager, String tag, DateSelectedListener listener) {
super.show(manager, tag);
mListener = listener;
}
public interface DateSelectedListener {
void onDateSelected(DatePicker view, int year, int month, int day);
}
在您的活动中执行此操作:
public class MyActivity extends AppCompatActivity implements DateChooser.DateSelectedListener {
//onCreate or whatever other method:
dateChooser.display(
getActivity().getSupportFragmentManager(),
"datePicker", this);
}
祝你好运!!!