gpt4 book ai didi

android - AlertDialog 内的 DatePickerDialog(在特定的 EditText 上设置日期)

转载 作者:行者123 更新时间:2023-12-05 07:54:19 24 4
gpt4 key购买 nike

我是 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);

}

};

最佳答案

其实我已经找到了解决问题的方法。我创建了一个单独的 DialogFragment,它使用当前日期作为 picker 中的默认日期。我还在可点击的 EditText 上设置了一个监听器, fragment 是在 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");
}

});

关于android - AlertDialog 内的 DatePickerDialog(在特定的 EditText 上设置日期),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31550049/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com