Note that there are some explanatory texts on larger screens.

plurals
  1. POandroid - how can I know which editText was clicked in dialog fragment class
    text
    copied!<p>I have two editText views, and when a user clicks the first editText, a dialogfragment window opens and the user selects the date. The same happens with the second editText. The date selected should be "written" in the editText.This works like a charm when I have one editText. But how do I know which ediText was clicked, as they are in the same activity and I have to edit the editText in the dialogFragment class?</p> <p>I have seen some other answers, but some of them are really outdated-deprecated, and some other didn 't help me.</p> <p>Here is my code:</p> <p><strong>Start class:</strong></p> <pre><code>public class Start extends FragmentActivity { private EditText ddEdit; private EditText adEdit; int year; int month; int day; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_start); } public void showDatePickerDialog(View v) { switch (v.getId()) { //i know this is not correct case R.id.etDepDate: DialogFragment depFragment = new DatePick(); depFragment.show(getFragmentManager(), "datepicker1"); break; case R.id.etArrDate: DialogFragment arrFragment = new DatePick(); arrFragment.show(getFragmentManager(), "datepicker2"); break; } } </code></pre> <p><strong>DatePick class:</strong></p> <pre><code> public class DatePick extends DialogFragment implements DatePickerDialog.OnDateSetListener { private EditText ddEdit; private EditText adEdit; @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) { String date = new String(day + "/" + (month + 1) + "/" + year); //???? //how I know here which view was clicked so I can call the editTextById method? //?????? } public void editTextById(View v, String date) { switch (v.getId()) { case R.id.etDepDate: setDate(date, ddEdit); setDate(date, adEdit); break; case R.id.etArrDate: setDate(date, adEdit); break; } } public void setDate(String date, EditText editText) { editText.setText(date); } @Override public void show(FragmentManager manager, String tag) { // TODO Auto-generated method stub super.show(manager, tag); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload