我找不到如何从主Activity中弹出一个弹出窗口中的日期选择器。当我调用findIdByView()来获取datepicker时,程序会在main的布局中查找。但是datepicker是在popuplayout中定义的。我怎么去那里?
当弹出窗口再次关闭时,我必须能够获得datepicker的值。
这是制作和关闭弹出窗口的方法:
public void onSelectDayClick(View view)
{
if(popup.isShowing())
{
//DatePicker datepicker = (DatePicker) this.findViewById(R.id.datePicker); (doesn't work)
int year = datepicker.getYear();
int month = datepicker.getMonth();
int day = datepicker.getDayOfMonth();
;displayday = new DateTime(year, month, day, 12, 00);
popup.dismiss();
;newDay();
LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
popup = new PopupWindow(inflater.inflate(R.layout.dateselect, null, false),LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
}
else
{
popup.showAtLocation(this.findViewById(R.id.scrollView), Gravity.BOTTOM, 0, 150);
}
}
我的xml文件:(dateselect.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<DatePicker
android:id="@+id/datePicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" />
</LinearLayout>
Thankx
答案 0 :(得分:1)
我认为这会对你有所帮助: http://developer.android.com/resources/tutorials/views/hello-datepicker.html
另外,尝试使用意图,这里有一些提示:How to pass the values from one activity to previous activity
答案 1 :(得分:1)
只需使用layout.findViewById而不是this.findViewById,也可以使用popup 需要在检查它是否显示之前定义,我建议将其放入onCreate
private Object datepicker;
private View layout;
protected void onCreate(Bundle savedInstanceState) {
...
LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.dateselect, null, false);
popup = new PopupWindow(layout,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
}
public void onSelectDayClick(View view)
{
if(popup.isShowing())
{
DatePicker datepicker = (DatePicker) layout.findViewById(R.id.datePicker);
int year = datepicker.getYear();
int month = datepicker.getMonth();
int day = datepicker.getDayOfMonth();
// ;displayday = new DateTime(year, month, day, 12, 00);*/
popup.dismiss();
// ;newDay();
}
else
{
popup.showAtLocation(this.findViewById(R.id.myTextView), Gravity.BOTTOM, 0, 150);
}
}
答案 2 :(得分:0)
adax2000的答案为我完成了这项工作。 Popup工作得很好。
不确定Object datepicker;
声明的目的是什么在顶部,因为这从未使用过,并且将其保留为正常。