如何添加一个简单的日历,当用户点击“出生日期字段”时,它将直接指向另一个页面或具有日历的弹出框,因此用户可以选择他/她的出生日期?我正在用PhoneGap 1.4.1在eclipse中开发一个android应用程序,很多谢谢你们。BirthDate on Registration Page,Eclipse
0
A
回答
2
这里是我做过什么:
为默认值创建一个日历实例:
//class level variables
private int mYear;
private int mMonth;
private int mDay;
static final int DATE_DIALOG_ID = 0;
private DatePickerDialog.OnDateSetListener mDateSetListener;
//inside onCreate
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR)-20;
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH); //Default DOB is (today - 20) years
//date picker things
mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
更新出生的EditText字段的日期函数:对et_dob
private void updateDisplay() {
et_dob.setText(new StringBuilder().append(mMonth+1).append("/").append(mDay).append("/").append(mYear).append(" "));
//month is 0 based so 1 is added in mMonth;
}
onClickListener
showDialog(DATE_DIALOG_ID); //DATE_DIALOG_ID = 0;
然后在你的活动覆盖onCreateDialog:
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return null;
}
当你点击EditText et_dob时,你会看到类似这样的东西。
0
package com.timesoft;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.wheel.ArrayWheelAdapter;
import com.wheel.NumericWheelAdapter;
import com.wheel.OnWheelChangedListener;
import com.wheel.WheelView;
public class Popup_Date extends Activity {
/** Called when the activity is first created. */
WheelView month;
WheelView year;
WheelView day;
String months[];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popup_date);
Calendar calendar = Calendar.getInstance();
month = (WheelView) findViewById(R.id.month);
year = (WheelView) findViewById(R.id.year);
day = (WheelView) findViewById(R.id.day);
OnWheelChangedListener listener = new OnWheelChangedListener() {
public void onChanged(WheelView wheel, int oldValue, int newValue) {
updateDays(year, month, day);
}
};
// month
int curMonth = calendar.get(Calendar.MONTH);
months= new String[] { "January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December" };
month.setViewAdapter(new DateArrayAdapter(this, months, curMonth));
month.setCurrentItem(curMonth);
month.addChangingListener(listener);
// year
int curYear = calendar.get(Calendar.YEAR);
year.setViewAdapter(new DateNumericAdapter(this, curYear-10, curYear + 10,0));
year.setCurrentItem(curYear);
year.addChangingListener(listener);
// day
updateDays(year, month, day);
day.setCurrentItem(calendar.get(Calendar.DAY_OF_MONTH)-1);
}
public void setclick(View v) {
if(GlobalData.forwhich.equals("1"))
{
GlobalData.leave_date_from = String.valueOf(year.getCurrentItem()+2001) + "-"
+ month.getCurrentItem() + "-"
+ String.valueOf(day.getCurrentItem()+1);
}
else if(GlobalData.forwhich.equals("2"))
{
GlobalData.leave_date_to = String.valueOf(year.getCurrentItem()+2001) + "-"
+ month.getCurrentItem() + "-"
+ String.valueOf(day.getCurrentItem()+1);
}
else
{
GlobalData.selected_date = String.valueOf(year.getCurrentItem()+2001) + "-"
+ months[month.getCurrentItem()] + "-"
+ String.valueOf(day.getCurrentItem()+1);
}
finish();
}
/**
* Updates day wheel. Sets max days according to selected month and year
*/
void updateDays(WheelView year, WheelView month, WheelView day) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR,calendar.get(Calendar.YEAR) + year.getCurrentItem());
calendar.set(Calendar.MONTH, month.getCurrentItem());
int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
day.setViewAdapter(new DateNumericAdapter(this, 1, maxDays, calendar
.get(Calendar.DAY_OF_MONTH) - 1));
int curDay = Math.min(maxDays, day.getCurrentItem() + 1);
day.setCurrentItem(curDay - 1, true);
calendar.set(Calendar.DAY_OF_MONTH, day.getCurrentItem() + 1);
Log.i("date :",
new SimpleDateFormat("dd/MM/yyyy").format(calendar.getTime()));
}
/**
* Adapter for numeric wheels. Highlights the current value.
*/
private class DateNumericAdapter extends NumericWheelAdapter {
// Index of current item
int currentItem;
// Index of item to be highlighted
int currentValue;
/**
* Constructor
*/
public DateNumericAdapter(Context context, int minValue, int maxValue,
int current) {
super(context, minValue, maxValue);
this.currentValue = current;
setTextSize(16);
}
@Override
protected void configureTextView(TextView view) {
super.configureTextView(view);
if (currentItem == currentValue) {
view.setTextColor(0xFF0000F0);
}
view.setTypeface(Typeface.SANS_SERIF);
}
@Override
public View getItem(int index, View cachedView, ViewGroup parent) {
currentItem = index;
return super.getItem(index, cachedView, parent);
}
}
/**
* Adapter for string based wheel. Highlights the current value.
*/
private class DateArrayAdapter extends ArrayWheelAdapter<String> {
// Index of current item
int currentItem;
// Index of item to be highlighted
int currentValue;
/**
* Constructor
*/
public DateArrayAdapter(Context context, String[] items, int current) {
super(context, items);
this.currentValue = current;
setTextSize(16);
}
@Override
protected void configureTextView(TextView view) {
super.configureTextView(view);
if (currentItem == currentValue) {
view.setTextColor(0xFF0000F0);
}
view.setTypeface(Typeface.SANS_SERIF);
}
@Override
public View getItem(int index, View cachedView, ViewGroup parent) {
currentItem = index;
return super.getItem(index, cachedView, parent);
}
}
}
+0
不是真的手机的差距有关的权利? – 2012-02-28 08:51:02
+0
是的,它与phongap – Google 2012-02-28 09:22:53
2
这里是XML文件
<LinearLayout android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_gravity="center_horizontal" android:paddingLeft="12dp"
android:paddingRight="12dp" android:paddingTop="30dp">
<com.wheel.WheelView android:id="@+id/day"
android:layout_height="wrap_content" android:layout_width="46dp" />
<com.wheel.WheelView android:id="@+id/month"
android:layout_height="wrap_content" android:layout_width="100dp" />
<com.wheel.WheelView android:id="@+id/year"
android:layout_height="wrap_content" android:layout_width="wrap_content" />
</LinearLayout>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Set"
android:onClick="setclick">
</Button>
相关问题
- 1. Magento语言翻译For Registration Page only
- 2. FLinq on Page
- 3. Tikzpicture on every page
- 4. Admin Page Ruby on Rails
- 5. Img not rendering on page
- 6. Modal,modeless divs on page
- 7. No fbml tags working on page
- 8. Angular Formly - HideExpression on page load
- 9. MVC Multiple DropDownLists on one page?
- 10. Django Registration Registration_form
- 11. ASP.Net调用Dispose on page/Controls in a page,or I do this?
- 12. .net core multiple forms on one page validation
- 13. Javascript image image cross-fade on page change
- 14. Slide form on/off page and expand div
- 15. django-registration activation_email.txt
- 16. propagating?next =在django-registration
- 17. django-registration电邮自定义
- 18. MOD_REWRITE /foo.html到/ existing/page /?page = foo
- 19. Native。* assemblyIdentity with Registration-free com
- 20. django-registration视图定制
- 21. 用htaccess重写url?page = 2到/ page/2?
- 22. Unix时间戳和mysql日期:birthdate
- 23. 为什么RewriteRule^page /?$ page.php [L] match site/page//
- 24. 在Page Ruby on Rails上输出Externall JSON Api
- 25. Wordpress 404 PAGE
- 26. Page 1
- 27. 与此不同.Page和(Page)HttpContext.Current.Handler
- 28. Page()在Razor Page中做了什么?
- 29. “/ page”和“http://www.domain.com/page”之间的区别
- 30. 将oldDomain.com/folder/page/重定向到newDomain.com/page/
你必须做出适配器,将打开calender..i的弹出有一些代码,我附上PLZ看到 – Google 2012-02-28 05:42:50