我是使用片段的新手, 我想在用户触摸片段中的Edittext时显示日期选择器 这是我的“选择日期选择器”类(该类通常在我使用活动时起作用)
public class MyEditTextDatePicker implements View.OnClickListener, DatePickerDialog.OnDateSetListener {
EditText _editText;
private int _day;
private int _month;
private int _birthYear;
private Context _context;
public MyEditTextDatePicker(Context context, int editTextViewID){
Activity act =(Activity) context;
this._editText = (EditText)act.findViewById(editTextViewID);
this._editText.setOnClickListener(this);
this._context = context;
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
_birthYear = year;
_month = monthOfYear;
_day = dayOfMonth;
updateDisplay();
}
@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
DatePickerDialog dialog = new DatePickerDialog(_context, this, calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
dialog.show();
}
private void updateDisplay() {
_editText.setText(new StringBuilder().append(_day).append("/").append(_month + 1)
.append("/").append(_birthYear).append(" "));
}
}
这就是我在Fragment中使用类的方式
public class HomeFragment extends Fragment {
MyEditTextDatePicker myEditDatePicker;
EditText editTanggal;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home, container, false);
editTanggal = (EditText) v.findViewById(R.id.editTanggal);
myEditDatePicker = new MyEditTextDatePicker(getActivity(),R.id.editTanggal);
Button btnMulai = (Button)v.findViewById(R.id.btnMulai);
btnMulai.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent bukaOpname = new Intent(getActivity(), OpnameActivity.class);
startActivity(bukaOpname);
}
}); return v; }
}