gpt4 book ai didi

java - Android 的简单日期选择器

转载 作者:行者123 更新时间:2023-12-01 18:38:44 26 4
gpt4 key购买 nike

我正在尝试通过单击编辑文本来启动 DataPicker

<小时/>

我想做什么::

  1. 有两个编辑文本
  2. 我想从编辑文本启动日期选择器

我做了什么::

  1. 我已经尝试过这样做

  2. 我已将其中一个编辑文本作为示例

问题::

  1. 当我点击编辑文本时,日期选择器未启动

  2. logcat 中没有错误

MainActivity.java

public class MainActivity extends FragmentActivity {

EditText edt;
EditText edt2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt=(EditText)findViewById(R.id.editText1);
edt2=(EditText)findViewById(R.id.editText2);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();
Calendar c1=Calendar.getInstance();
c1.setTime(new Date());
c.setTime(new Date()); // Now use today date.
c.add(Calendar.DATE, 7); // Adding 7 days
String ToDate = sdf.format(c.getTime());
String FromDate = sdf.format(c1.getTime());
edt2.setText(FromDate);
edt.setText(ToDate);
}



public void selectDate(View view) {
DialogFragment newFragment = new SelectDateFragment();
newFragment.show(getSupportFragmentManager(), "DatePicker");
}
public void populateSetDate(int year, int month, int day) {
edt2 = (EditText)findViewById(R.id.editText1);
edt2.setText(day+"/"+month+"/"+year);
}
public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, yy, mm, dd);
}

public void onDateSet(DatePicker view, int yy, int mm, int dd) {
populateSetDate(yy, dd, mm+1);
}
}
}

最佳答案

您只需实现 Edittext onClickListner onClick() 事件:

edt.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// new DatePickerDialog(ProximityActivity.this, dateD, myCalendar
//.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
//myCalendar.get(Calendar.DAY_OF_MONTH)).show();

//Load your DiaglogFragment Over here
}
});

关于java - Android 的简单日期选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20782488/

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