Android 日期选择器未在AlertDialog中显示编辑文本的一次单击
我需要显示Android 日期选择器未在AlertDialog中显示编辑文本的一次单击,android,datepicker,android-alertdialog,Android,Datepicker,Android Alertdialog,我需要显示datePicker来选择EditText的点击日期事件,我的EditText在AlertDialog中。我已经为onClickListener编写了一段代码,但它没有显示日期选择器 public class TourActivity extends AppCompatActivity { DatabaseHelper db; private LayoutInflater inflater; private FloatingActionButton fab; @Override p
datePicker
来选择EditText
的点击日期事件,我的EditText
在AlertDialog
中。我已经为onClickListener
编写了一段代码,但它没有显示日期选择器
public class TourActivity extends AppCompatActivity {
DatabaseHelper db;
private LayoutInflater inflater;
private FloatingActionButton fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tour);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
fab = (FloatingActionButton) findViewById(R.id.fab);
db = new DatabaseHelper(getApplicationContext());
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inflater = TourActivity.this.getLayoutInflater();
View content = inflater.inflate(R.layout.activity_add_new_trip, null);
final EditText editEvent = (EditText) content.findViewById(R.id.edTxt_EventName);
final EditText editStartDate = (EditText) content.findViewById(R.id.edTxt_EventSDate);
editStartDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar mcurrentDate = Calendar.getInstance();
int mYear = mcurrentDate.get(Calendar.YEAR);
int mMonth = mcurrentDate.get(Calendar.MONTH);
int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);
DatePickerDialog mDatePicker = new DatePickerDialog(TourActivity.this, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
// TODO Auto-generated method stub
/* Your code to get date and time */
}
}, mYear, mMonth, mDay);
mDatePicker.setTitle("Select date");
mDatePicker.show();
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(TourActivity.this);
builder.setView(content)
.setTitle("Add Event")
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Tour tour = new Tour();
tour.setEventId((int) System.currentTimeMillis());
tour.setEventName(editEvent.getText().toString().toUpperCase());
long t = db.createTour(tour);
Snackbar.make(findViewById(android.R.id.content),
"Event Added Successfully.", Snackbar.LENGTH_LONG)
.setActionTextColor(Color.RED)
.show();
displayList();
}
})
.setNegativeButton(cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
}
}public class TourActivity extends AppCompatActivity {
DatabaseHelper db;
private LayoutInflater inflater;
private FloatingActionButton fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tour);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
fab = (FloatingActionButton) findViewById(R.id.fab);
db = new DatabaseHelper(getApplicationContext());
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inflater = TourActivity.this.getLayoutInflater();
View content = inflater.inflate(R.layout.activity_add_new_trip, null);
final EditText editEvent = (EditText) content.findViewById(R.id.edTxt_EventName);
final EditText editStartDate = (EditText) content.findViewById(R.id.edTxt_EventSDate);
editStartDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar mcurrentDate = Calendar.getInstance();
int mYear = mcurrentDate.get(Calendar.YEAR);
int mMonth = mcurrentDate.get(Calendar.MONTH);
int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);
DatePickerDialog mDatePicker = new DatePickerDialog(TourActivity.this, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
// TODO Auto-generated method stub
/* Your code to get date and time */
}
}, mYear, mMonth, mDay);
mDatePicker.setTitle("Select date");
mDatePicker.show();
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(TourActivity.this);
builder.setView(content)
.setTitle("Add Event")
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Tour tour = new Tour();
tour.setEventId((int) System.currentTimeMillis());
tour.setEventName(editEvent.getText().toString().toUpperCase());
long t = db.createTour(tour);
Snackbar.make(findViewById(android.R.id.content),
"Event Added Successfully.", Snackbar.LENGTH_LONG)
.setActionTextColor(Color.RED)
.show();
displayList();
}
})
.setNegativeButton(cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
}
}
如果您需要任何其他代码,请告诉我。这里有一个解决方案供您回答@MubinSayed感谢您的帮助!!!但是我的editText没有直接在AlertDialog中的活动中,这就是为什么创建另一个对话框是一个问题,。