我想创建一个Date对话框选择器来自动填充我的EditText。 有两个EditText,我不知道如何在我的听众中使用editText lanch dialod。
public class DatePickerFragment extends DialogFragment{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
super.onCreateDialog(savedInstanceState);
Calendar c = Calendar.getInstance();
int year= c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day_of_month = c.get(Calendar.DAY_OF_MONTH);
Log.i(Acceuil.APP_TAG, "DialogFrag method Id: " +this.getId());
DatePickerDialog datePicker = new DatePickerDialog(getActivity(), STYLE_NORMAL, (OnDateSetListener)getActivity(), year,month,day_of_month);
return datePicker;
}
在我的活动中:
public class AddCar extends FragmentActivity implements OnItemSelectedListener,OnDateSetListener{
EditText registration_ET=null;
EditText control_ET=null;
DialogFragment newFrag= null;
DialogFragment newFrag2= null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addcar_layout);
registration_ET = (EditText) this.findViewById(R.id.firstRegistration_editText);
registration_ET.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i(Acceuil.APP_TAG,"Registration ET clicked!");
newFrag = new DatePickerFragment();
newFrag.show(getSupportFragmentManager(), "DateRegisterId");
}
});
control_ET = (EditText) this.findViewById(R.id.control_editText);
control_ET.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i(Acceuil.APP_TAG,"Registration ET clicked!");
newFrag2 = new DatePickerFragment();
newFrag2.show(getSupportFragmentManager(), "controlId");
}
});
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
Log.i(Acceuil.APP_TAG, "onDateSet AddCar method");
//switch (view.getId())
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();
c.set(year, monthOfYear, dayOfMonth);
String date = sdf.format(c.getTime());
}
我想要的是更新正确的editText registration_ET或control_ET dependnt whitch片段设置日期。 感谢。
答案 0 :(得分:0)
只需更改您的DatePickerFragment
即可。
public class DatePickerFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
super.onCreateDialog(savedInstanceState);
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day_of_month = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), STYLE_NORMAL, (DatePickerDialog.OnDateSetListener) getActivity(), year, month, day_of_month);
dialog.getDatePicker().setTag(getTag());
return dialog;
}
}
注意:
我在您的代码中添加了以下两行。
DatePickerDialog dialog = new DatePickerDialog(getActivity(), STYLE_NORMAL, (DatePickerDialog.OnDateSetListener) getActivity(), year, month, day_of_month);
dialog.getDatePicker().setTag(getTag());
现在,您可以从onDateSet
方法获取标记,如下所示。
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
if (view.getTag().equals("DateRegisterId")) {
registration_ET.setText(monthOfYear + "/" + dayOfMonth + "/" + year);
} else if (view.getTag().equals("controlId")) {
control_ET.setText(monthOfYear + "/" + dayOfMonth + "/" + year);
}
}
<强>更新强>
<强> 1。 DatePickerFragment.java 强>
package com.example.androidtester;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import java.util.Calendar;
public class DatePickerFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
super.onCreateDialog(savedInstanceState);
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day_of_month = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), STYLE_NORMAL, (DatePickerDialog.OnDateSetListener) getActivity(), year, month, day_of_month);
dialog.getDatePicker().setTag(getTag());
return dialog;
}
}
<强> 2。 MainActivity.java 强>
package brightspot.com.androidtester;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {
private EditText editText;
private EditText editText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
editText2 = (EditText) findViewById(R.id.editText2);
final DatePickerFragment newFrag = new DatePickerFragment();
editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
newFrag.show(getSupportFragmentManager(), "editText");
}
});
editText2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
newFrag.show(getSupportFragmentManager(), "editText2");
}
});
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
if (view.getTag().equals("editText")) {
editText.setText(monthOfYear + "/" + dayOfMonth + "/" + year);
} else if (view.getTag().equals("editText2")) {
editText2.setText(monthOfYear + "/" + dayOfMonth + "/" + year);
}
}
}
我希望这会对你有所帮助。