gpt4 book ai didi

android - 如何在 Android Fragment 中使用日期选择器?

转载 作者:行者123 更新时间:2023-11-29 17:30:46 26 4
gpt4 key购买 nike

我在 Activity 中使用过 Date Picker 但我不知道如何在 Fragment 中使用它。我正在上传我的 Activity Date Picker 代码。任何人都可以帮助我将该代码 fragment 转换吗?

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
context = getApplicationContext();
View.OnClickListener listenerDate = new View.OnClickListener() {
@Override
public void onClick(View arg0) {
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
showDialog(DATE_DIALOG_ID);
}
};
editTextDate.setOnClickListener(listenerDate);
} private void initialize() {
editTextDate = (EditText) findViewById(R.id.editTextDate);
}private void updateDisplay() {
currentDate = new StringBuilder().append(day).append(".")
.append(month + 1).append(".").append(year).toString();
Log.i("DATE", currentDate);
}
DatePickerDialog.OnDateSetListener myDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int i, int j, int k) {
year = i;
month = j;
day = k;
updateDisplay();
editTextDate.setText(currentDate);
}
};`

最佳答案

试试这个示例:

public class YourClass extends Fragment {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.your_layout, container, false);
return view;
}

public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
edittext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDatePicker();
}
});
}

private void showDatePicker() {
DatePickerFragment date = new DatePickerFragment();
/**
* Set Up Current Date Into dialog
*/
Calendar calender = Calendar.getInstance();
Bundle args = new Bundle();
args.putInt("year", calender.get(Calendar.YEAR));
args.putInt("month", calender.get(Calendar.MONTH));
args.putInt("day", calender.get(Calendar.DAY_OF_MONTH));
date.setArguments(args);
/**
* Set Call back to capture selected date
*/
date.setCallBack(ondate);
date.show(getFragmentManager(), "Date Picker");
}

OnDateSetListener ondate = new OnDateSetListener() {

public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {

edittext.setText(String.valueOf(dayOfMonth) + "-" + String.valueOf(monthOfYear+1)
+ "-" + String.valueOf(year));
}
};
}

现在创建 DatePickerFragment 类:

public class DatePickerFragment extends DialogFragment {
OnDateSetListener ondateSet;
private int year, month, day;

public DatePickerFragment() {}

public void setCallBack(OnDateSetListener ondate) {
ondateSet = ondate;
}

@SuppressLint("NewApi")
@Override
public void setArguments(Bundle args) {
super.setArguments(args);
year = args.getInt("year");
month = args.getInt("month");
day = args.getInt("day");
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new DatePickerDialog(getActivity(), ondateSet, year, month, day);
}
}

希望对您有所帮助。

关于android - 如何在 Android Fragment 中使用日期选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33086786/

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