在我的应用程序中,用户应该通过单击“编辑文本”来选择日期,因为目前我正在使用日期选择器对话框并且其工作正常,但它就像打开另一个对话框一样。所以我想把它显示为弹出菜单,结帐图片。请帮我实现这个目标!
答案 0 :(得分:4)
您只需创建一个新弹出窗口并将日历视图扩展到该窗口:按照以下步骤操作:
edit_text.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showPopup(MainActivity.this);
}
});
现在showPopup()方法:
// The method that displays the popup.
private void showPopup(Activity context) {
// Inflate the popup_layout.xml
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = layoutInflater.inflate(R.layout.main3, null,false);
// Creating the PopupWindow
final PopupWindow popupWindow = new PopupWindow(
layout,400,400);
popupWindow.setContentView(layout);
popupWindow.setHeight(500);
popupWindow.setOutsideTouchable(false);
// Clear the default translucent background
popupWindow.setBackgroundDrawable(new BitmapDrawable());
CalendarView cv = (CalendarView) layout.findViewById(R.id.calendarView);
cv.setBackgroundColor(Color.BLUE);
cv.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month,
int dayOfMonth) {
// TODO Auto-generated method stub
popupWindow.dismiss();
Log.d("date selected", "date selected " + year + " " + month + " " + dayOfMonth);
}
});
popupWindow.showAtLocation(layout, Gravity.TOP,5,170);
}
现在,main3.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CalendarView
android:id="@+id/calendarView"
android:layout_width="match_parent"
android:layout_height="400dp"
android:clickable="true"
android:showWeekNumber="false" />
</LinearLayout>
然后在快照下方看看,你可以自己定制这个弹出窗口。
答案 1 :(得分:3)
试试@Jarvis
public class MainActivity extends Activity {
EditText date;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
date=(EditText)findViewById(R.id.date);
date.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();
}
});
}
Calendar myCalendar = Calendar.getInstance();
DatePickerDialog.OnDateSetListener dateD = new DatePickerDialog.OnDateSetListener(){
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
private void updateLabel() {
String myFormat = "MM/dd/yy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.ENGLISH);
date.setText(sdf.format(myCalendar.getTime()));
}
}