如何添加一個簡單的日曆,當用戶點擊「出生日期字段」時,它將直接指向另一個頁面或具有日曆的彈出框,因此用戶可以選擇他/她的出生日期?我正在用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. Tikzpicture on every page
- 3. FLinq on Page
- 4. model undefined on page reload
- 5. woocommerce hook on page-new.php
- 6. Img not rendering on page
- 7. Modal,modeless divs on page
- 8. Admin Page Ruby on Rails
- 9. jquery show overlay div on page load
- 10. Angular Formly - HideExpression on page load
- 11. jQuery Tabs on ASP.Net Master Page
- 12. Center visualize.js div content on page
- 13. MVC Multiple DropDownLists on one page?
- 14. No fbml tags working on page
- 15. Move Fabric JS Canvas on page
- 16. ModelForm For Registration HttpResponseError
- 17. java spring bean registration
- 18. Django Registration Registration_form
- 19. ASP.Net調用Dispose on page/Controls in a page,or I do this?
- 20. MVC Beginform action = goto anchor/id on page
- 21. Multiple Raphael Clocks on one page,not passing hours/minutes
- 22. .net core multiple forms on one page validation
- 23. Slide form on/off page and expand div
- 24. Coffeescript JQuery on click only working when page is refreshd
- 25. Javascript image image cross-fade on page change
- 26. django-registration activation_email.txt
- 27. canonical registration id gcm
- 28. jUnit on eclipse Indigo
- 29. .htaccess RewriteRule domain.com/page/page to domain.com?p = page/page
- 30. django 1.9 and registration/login.html
你必須做出適配器,將打開calender..i的彈出有一些代碼,我附上PLZ看到 – Google 2012-02-28 05:42:50