0

我想在 Android 中使用日期选择器。这是我的 XML 代码。出发日期应从当前日期开始,返回日期应大于我的出发日期

<LinearLayout
    android:id="@+id/dateLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/fromToLayout"
    android:baselineAligned="false"
    android:orientation="horizontal" >
    <LinearLayout
        android:id="@+id/departdateLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:orientation="vertical" >
        <TextView
            android:id="@+id/deparDateTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="7dp"
            android:gravity="center_horizontal"
            android:text="@string/departdate" />
        <TextView
            android:id="@+id/departDate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="dd/mm/yyyy" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/returndateLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/returndata"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="7dp"
            android:gravity="center_horizontal"
            android:text="@string/returndate" />

        <TextView
            android:id="@+id/returnDate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="dd/mm/yyyy"/>
    </LinearLayout>
</LinearLayout>

代替 ID 为 returnDate 和离开日期的 TextView 我想使用 datepicker 以便用户单击日历从那里打开,他可以选择日期并且该日期被填写在这个 Texview

4

3 回答 3

0

试试下面的方法。

TextView departDate = (Button) findViewById(R.id.departDate);
departDate.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {

            Calendar calender = Calendar.getInstance();
            Dialog mDialog = new DatePickerDialog(IncomeActivity.this,
                    mDatesetListener, calender.get(Calendar.YEAR),
                    calender.get(Calendar.MONTH), calender
                            .get(Calendar.DAY_OF_MONTH));

            mDialog.show();             
    }
});

然后将以下代码编写为单独的方法。

    private DatePickerDialog.OnDateSetListener mDatesetListener = new OnDateSetListener() {

    public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
        arg2 = arg2 + 1;

        String my_date = arg1 + "-" + arg2 + "-" + arg3;
        departDate.setText(my_date);            
    }
};

我希望这能帮到您。

于 2013-07-16T07:36:09.527 回答
0

你可以做这样的事情: -

public class mExample extends Activity {

    private TextView returnDate;


    private int year;
    private int month;
    private int day;

    static final int DATE_PICKER_ID = 1111; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        returnDate= (TextView) findViewById(R.id.returnDate);

        // Get current date by calender

        final Calendar c = Calendar.getInstance();
        year  = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day   = c.get(Calendar.DAY_OF_MONTH);

        // Show current date

        returnDate.setText(new StringBuilder()
                // Month is 0 based, just add 1
                .append(month + 1).append("-").append(day).append("-")
                .append(year).append(" "));


        returnDate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                // On click show datepicker dialog 
                showDialog(DATE_PICKER_ID);

            }

        });
   }


    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_PICKER_ID:

            // open datepicker dialog. 
            // set date picker for current date 
            // add pickerListener listner to date picker
            return new DatePickerDialog(this, pickerListener, year, month,day);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.
        @Override
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {

            year  = selectedYear;
            month = selectedMonth;
            day   = selectedDay;

            // Show selected date 
            returnDate.setText(new StringBuilder().append(month + 1)
                    .append("-").append(day).append("-").append(year)
                    .append(" "));

           }
        };
}
于 2015-02-02T04:40:37.217 回答
-1

尝试使用 dis 代码解决你所有的问题

public class SearchFlight extends Activity {

    EditText depart,returnDate;
    Calendar myCalendar;
    int val;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_flight);

        depart=(EditText)findViewById(R.id.departDate);
        returnDate=(EditText)findViewById(R.id.returnDate);
        myCalendar = Calendar.getInstance();


        final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {



            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                // TODO Auto-generated method stub
                 myCalendar.set(Calendar.YEAR, year);
                    myCalendar.set(Calendar.MONTH, monthOfYear);
                    myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                    updateLabel(val);

            }

        };
            depart.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    new DatePickerDialog(SearchFlight.this, date, myCalendar
                            .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                            myCalendar.get(Calendar.DAY_OF_MONTH)).show();
                    val=1;
                }
            });
            returnDate.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    new DatePickerDialog(SearchFlight.this, date, myCalendar
                            .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                            myCalendar.get(Calendar.DAY_OF_MONTH)).show();
                            val=2;
                }
            });





    }

    private void updateLabel(int val) {

        String myFormat = "dd/MM/yy"; //In which you need put here
        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
        if(val==1)
            depart.setText(sdf.format(myCalendar.getTime()));
        else
            returnDate.setText(sdf.format(myCalendar.getTime()));

        }


}
于 2013-07-16T07:03:52.003 回答