片段 - 带有 DatePickerDialog 片段的 EditText

Posted

技术标签:

【中文标题】片段 - 带有 DatePickerDialog 片段的 EditText【英文标题】:Fragment - EditText with DatePickerDialog Fragment 【发布时间】:2021-12-09 19:59:30 【问题描述】:

我有一个片段 (FragmentAggiungiPaziente),其中包含一个 EditText,用于选择日期。当我选择日期时,edittext 不会随所选日期更新。你能帮我解决这个问题吗?谢谢

片段AggiungiPaziente

EditText nomePaz, cognomePaz, dataPaz, mailPaz, telefonoPaz;
Button aggiungiPaz;
DBHelper dbPaz;
View view;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) 
    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.fragment_aggiungi_paziente, container, false);

    nomePaz = view.findViewById(R.id.nomePazNew);
    cognomePaz = view.findViewById(R.id.cognomePazNew);

    final FragmentManager fm = ((AppCompatActivity)getActivity()).getSupportFragmentManager();
    dataPaz = view.findViewById(R.id.dataPazNew);

    dataPaz.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 

            FragmentData newFragment = new FragmentData();
            newFragment.show(fm, "data");
         
            getChildFragmentManager().setFragmentResultListener("chiaveRichiesta", getViewLifecycleOwner(), new FragmentResultListener() 
                @Override
                public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle result) 
                    String risultato = result.getString("chiaveRisultato");
                    dataPaz.setText(risultato);
                
            );


        

    );

    mailPaz = view.findViewById(R.id.mailPazNew);
    telefonoPaz = view.findViewById(R.id.telefonoPazNew);
    dbPaz = new DBHelper(getActivity());
    //
    aggiungiPaz = view.findViewById(R.id.aggiungiPazBtn);
    aggiungiPaz.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            String nome = nomePaz.getText().toString();
            String cognome = cognomePaz.getText().toString();
            String data = dataPaz.getText().toString();
            String mail = mailPaz.getText().toString();
            String telefono = telefonoPaz.getText().toString();
            Sessione sessione = new Sessione(getActivity());
            String mail_medico = sessione.getSessione();

            if (nome.equals("") || cognome.equals("") || data.equals("") || mail.equals("") || telefono.equals(""))
                Toast.makeText(getActivity(), "Inserisci tutti i campi", Toast.LENGTH_SHORT).show();
             else 
                boolean controlloPaz = dbPaz.controlloPaziente(mail_medico,nome,cognome,data,mail,telefono);
                if (controlloPaz==true)
                    boolean inserisci = dbPaz.inserisciPaziente(mail_medico, nome,cognome, data,mail,telefono);
                    if(inserisci==true)
                        Toast.makeText(getActivity(), "Paziente inserito con successo", Toast.LENGTH_SHORT).show();
                        getParentFragmentManager().beginTransaction().replace(R.id.frameLayoutMain,new FragmentListaPazienti()).addToBackStack(null).commit();
                     else 
                        Toast.makeText(getActivity(), "Inserimento fallito", Toast.LENGTH_SHORT).show();
                    
                 else 
                        Toast.makeText(getActivity(), "Paziente già inserito", Toast.LENGTH_SHORT).show();
                
            
        
    );
    return view;

下面这个是包含日期选择器对话框的片段。我使用了 setFragmentResultListener 监听器

片段数据

public class FragmentData extends DialogFragment implements 
DatePickerDialog.OnDateSetListener 

final Calendar c = Calendar.getInstance();
@Override
 public void onDateSet(DatePicker datePicker, int i, int i1, int i2) 
    // i:anno, i1:mese, i2:giorno
    c.set(Calendar.YEAR, i);
    c.set(Calendar.MONTH, i1);
    c.set(Calendar.DAY_OF_MONTH, i2);
    String selectedDate = new SimpleDateFormat("dd/MM/yyyy", Locale.ITALIAN).format(c.getTime());

   
    Bundle risultato = new Bundle();
    risultato.putString("chiaveRisultato",selectedDate);
    getParentFragmentManager().setFragmentResult("chiaveRichiesta", risultato);





@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) 
    int anno = c.get(Calendar.YEAR);
    int mese = c.get(Calendar.MONTH);
    int giorno = c.get(Calendar.DAY_OF_MONTH);

    return new DatePickerDialog(getActivity(), FragmentData.this, anno, mese, giorno);

【问题讨论】:

别忘了将我的答案标记为已接受。 【参考方案1】:

使用 FragmentManager 代替您的 getChildFragmentManager() 检查以下代码:

     fm.setFragmentResultListener("chiaveRichiesta", getViewLifecycleOwner(), new FragmentResultListener() 
            @Override
            public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle result) 
                String risultato = result.getString("chiaveRisultato");
                dataPaz.setText(risultato);
            
        );

【讨论】:

谢谢你。现在,代码可以工作了。

以上是关于片段 - 带有 DatePickerDialog 片段的 EditText的主要内容,如果未能解决你的问题,请参考以下文章

在片段中显示 DatePickerDialog

Android:带有取消按钮的DatePickerdialog [重复]

如何在按钮单击时显示 DatePickerDialog? [关闭]

Android 日期选择器片段更改为微调器

自定义对话框片段

导航组件的默认空值:参数过多错误