我是Android开发新手,几天来我一直坚持这个问题。 我有一个用 AlertDialog 实现的填写表单。在对话框中我创建了一个 DatePickerDialog ,当点击并聚焦特定的 EditText 时,它会打开。我似乎无法弄清楚如何从 DatePicker 获取日期,然后将其显示给可点击的 EditText 。以下是我的代码,如果有人有想法或两个请分享!!
public void AddDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater=this.getLayoutInflater();
View view=inflater.inflate(R.layout.add_dialog_layout, spinner);
builder.setView(view);
Spinner spinner = (Spinner)view.findViewById(R.id.spinner1);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.list_of_categories, android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
builder.setInverseBackgroundForced(true);
EditText editText1 = (EditText)view.findViewById(R.id.editText4);
editText1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SetUpDateDialog();
}
});
editText1.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
{
SetUpDateDialog();
}
}
});
TextView title = new TextView(this);
title.setText("Προσθέστε πληροφορίες");
title.setBackgroundColor(Color.DKGRAY);
title.setPadding(20, 20, 20, 20);
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.WHITE);
title.setTextSize(20);
title.setCompoundDrawablesWithIntrinsicBounds(R.drawable.add_icon, 0, 0, 0);
builder.setCustomTitle(title);
builder.show();
}
public void SetUpDateDialog(){
new DatePickerDialog(MapActivityPrivilleged.this,d,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH))
.show();
}
private DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month,
int day) {
LayoutInflater li = LayoutInflater.from(MapActivityPrivilleged.this);
View view1=li.inflate(R.layout.add_dialog_layout, null);
EditText editText1 = (EditText)view1.findViewById(R.id.editText4);
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
editText1.setText(day + "/" + (month+1)+ "/" + year);
}
};
答案 0 :(得分:0)
实际上我找到了问题的解决方案。我创建了一个单独的 DialogFragment ,它使用当前日期作为选择器中的默认日期。 我还在可点击的 EditText 上设置了一个监听器,并在 onClick()方法中创建了片段。
出现对话框后,用户可以将日期修改为他/她的选择。 最后,使用 onDateSet(),我将用户选择的日期设置为文本。
Fragment类:
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
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) {
DateText.setText(String.valueOf(day) + "/"
+ String.valueOf(month + 1) + "/" + String.valueOf(year));
// set selected date into datepicker also
}
}
可点击 EditText ,并在 onClick()方法中创建 DatePickerFragment 。
DateText = (EditText)view.findViewById(R.id.editText);
DateText.setOnClickListener( new OnClickListener () {
@Override
public void onClick(View v) {
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
DialogFragment DateFragment = new DatePickerFragment();
DateFragment.show(getFragmentManager(), "datePicker");
}
});