我正在尝试在.setOnClickListener中设置一个带有DatePickerDialog的editText。其中,当用户点击editText时,将出现DatePickerDialog,然后在用户选择所需日期后,它将在editText上设置它......如下所示:Android:DatePickerDialog on EditText Click Event
这是我的代码mycode
任何想法如何做到这一点?
我尝试了上面的例子以及更多但似乎没有任何东西在.SetOnClickListener
内部工作答案 0 :(得分:0)
使用此代码:
public class MainActivity extends AppCompatActivity {
EditText text;
private int year,currentYear;
private int month,currentMonth;
private int day,currentDay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText)findViewById(R.id.edit);
// Get current date by calender
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int mYear, int mMonth, int mDay) {
currentDay = mDay;
currentMonth = mMonth+1;
currentYear = mYear;
text.setText(mDay+"-"+(mMonth+1)+"-"+mYear);
}
},year,month,day).show();
}
});
}
}
我已将用户选择的年,月,日分别存储在currentYear,currentMonth,currentDay中。