我正在尝试创建searchdate。我在这里做的是RijekSearch.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_rijek_search, container, false);
EditText start = (EditText) v.findViewById(R.id.startdateval);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fm = getFragmentManager();
DatePickerFragment myDialogFragment = new DatePickerFragment();
myDialogFragment.show(fm, "dialog_fragment");
}
});
return v;
}
这里是DatePickerFragment
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
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(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(year, month, day);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(c.getTime());
Fragment fr = new RijekSearch();
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
Bundle args = new Bundle();
args.putString("startval", formattedDate);
fr.setArguments(args);
ft.replace(R.id.content_frame, fr);
ft.commit();
}
}
因此。首先,我打开RijekSearch
这是一个片段。然后在我点击start
(EditText)后,它将显示dialogfragment
(DatePickerFragment)。显示没有问题,没有错误。
我的问题是,如何在从start
选择日期后设置DatePickerFragment
值?
答案 0 :(得分:0)
有两种方法: 1.创建一个公共静态editText(不推荐,内存泄漏)。 2.创建侦听器并在对话框片段中传递实例。
答案 1 :(得分:0)
创建界面DateCallback
interface DateCallback{
void onDateSet(String fDate);
}
在DialogFragment
参数
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
private static DataCallback dataCallback;
public static DatePickerFragment newInstance(DataCallback dataCallback){
this.dataCallback = dataCallback;
return new DatePickerFragment();
}
@Override
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(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(year, month, day);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(c.getTime());
FragmentManager fm=getFragmentManager();
dataCallback.onDateSet(formattedDate);
fm.popBackStack();
}
}
现在进入你的RijekSearch
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fm = getFragmentManager();
DatePickerFragment myDialogFragment = DatePickerFragment.newInstance(new DateCallback(){
@Override
public void onDateSet(String fDate){
start.setText(fDate);
}
});
myDialogFragment.show(fm, "dialog_fragment");
}
});
答案 2 :(得分:0)
您需要将EditText
个数据Fragment
传递给DialogFragment
首先删除此start.setOnClickListener()
您可以使用这些
3 - Search and handle its click
on Action传递EditText
数据
Bundle args = new Bundle();
args.putString("key", "value");
DialogFragment newFragment = new YourDialogFragment();
newFragment.setArguments(args);
newFragment.show(getSupportFragmentManager(), "TAG");
并在DialogFragment
Bundle mArgs = getArguments();
String myValue = mArgs.getString("key");