2012-02-28 13 views
0

ユーザーが「生年月日フィールド」をクリックしたときにカレンダーを持つ別のページまたはポップアップボックスに移動するため、ユーザーが「生年月日」フィールドを選択できるようにする簡単なカレンダーを追加するにはどうすればよいですか?彼/彼女の誕生日?私はPhoneGap 1.4.1でEclipseでAndroidアプリケーションを開発しています。多くの皆さんありがとうございました。ここで生年月日登録ページ、Eclipse

+0

は、私はplzは – Google

答えて

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フィールドの日付を更新する:

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 et_dob

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をクリックすると、このような画面が表示されます。あなたがcalender..iのポップアップが開きますアダプターをしなければならない

DatePicker

+0

には関係ありません。あなたは、DatePickerを直接使用できます。月と日の配列を作成する必要はありません。 et_dobがフォーカスすると、この日付ピッカーダイアログボックスが表示されます。ユーザーは日付を選択し、設定をクリックしてダイアログを閉じ、EditTextに表示することができます。 – drulabs

+1

私はphonegapの部分を見ませんでした。それでも私はこれがうまくいくと思います。 – drulabs

+0

とにかくこのコードを置くべきですか? :D、申し訳ありません、これはアプリを初めて作った時です。私はここであなたの答えをありがとう。 :D、 –

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

が実際に携帯電話のギャップ権利関係していないことがわかり付属のいくつかのコードを持っていますか? –

+0

はいphongap – Google

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>