2012-02-28 47 views
0

如何添加一个简单的日历,当用户点击“出生日期字段”时,它将直接指向另一个页面或具有日历的弹出框,因此用户可以选择他/她的出生日期?我正在用PhoneGap 1.4.1在eclipse中开发一个android应用程序,很多谢谢你们。BirthDate on Registration Page,Eclipse

+0

你必须做出适配器,将打开calender..i的弹出有一些代码,我附上PLZ看到 – Google 2012-02-28 05:42:50

回答

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时,你会看到类似这样的东西。

DatePicker

+0

无关,你可以直接使用DatePicker,不需要创建一个数月和数天的数组。 et_dob得到焦点的时刻,你会看到这个日期选择器对话框。用户可以选择日期并单击设置来关闭对话框并在EditText上显示它。 – drulabs 2012-02-28 06:12:12

+1

哎呀,我没有看到phonegap部分。不过我认为这会奏效。 – drulabs 2012-02-28 06:21:19

+0

无论如何我应该把这段代码放在哪里? :D,抱歉,这是我第一次构建应用程序。但我很欣赏你的答案。 :D, – 2012-02-28 22:32:03

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>