如何获取两个datepicker值并传递给两个字符串

时间:2016-03-04 07:56:38

标签: java android datepicker

我正在开发一个应用程序,我可以从datepicker获取开始和结束日期并将其传递给服务器。我通过从互联网复制代码成功开发了两个日期选择器,但现在我不知道如何将这些日期选择器的值放在两个不同的字符串中。这样我就可以将它传递给服务器了。 这是我的xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ggreenb"
tools:context="com.example.viren.cable.unpaid_cust">

<TextView android:text="Set a Range"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textview1"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:textSize="20dp"
    android:textAppearance="?android:attr/textAppearanceMedium"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/startbt"
    android:layout_below="@+id/textview1"
    android:layout_toLeftOf="@+id/textview1"
    android:layout_marginTop="30dp"
    android:text="Set Date"


    android:textAppearance="?android:textAppearanceMedium"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/endbt"
    android:layout_below="@+id/textview1"
    android:layout_toRightOf="@+id/textview1"
    android:layout_marginTop="30dp"
    android:text="Set Date"
    android:textAppearance="?android:textAppearanceMedium"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/startdateview"
    android:layout_below="@+id/startbt"
    android:layout_toLeftOf="@+id/textview1"
    android:text="DD/MM/YYYY"
    android:layout_marginTop="15dp"
    android:textSize="20dp"
    android:textAppearance="?android:textAppearanceMedium"/>\

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/enddateview"
    android:layout_below="@+id/endbt"
    android:layout_toRightOf="@+id/textview1"
    android:text="DD/MM/YYYY"
    android:layout_marginTop="15dp"
    android:textSize="20dp"
    android:textAppearance="?android:textAppearanceMedium"/>
<Spinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/area_spinner"
    android:layout_marginTop="35dp"
    android:layout_below="@+id/startdateview"
   android:layout_centerHorizontal="true"

    >

</Spinner>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="submit"
android:textSize="20dp"
android:id="@+id/submitdatebt"
android:layout_below="@+id/startdateview"
android:layout_centerHorizontal="true"
 android:layout_marginTop="200dp"/>
 </RelativeLayout>

这是我的java代码..

package com.example.viren.cable;

import android.app.DatePickerDialog;  
import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Calendar;


public class unpaid_cust extends ActionBarActivity {
String startdate, enddate;

Button startbt, endbt, subbt;
TextView starttv, endtv;
static final int DATE_DIALOG_ID = 0;
private DatePicker datePicker;
private Calendar startDate, endDate;
DatePickerDialog.OnDateSetListener from_dateListener, to_dateListener;

int from_year, from_month, from_day, to_year, to_month, to_day;
private final int DATE_PICKER_TO = 0;
private final int DATE_PICKER_FROM = 1;
int cur = 0;
String[] area_list;
private TextView activeDateDisplay;
private Calendar activeDate;
Spinner sp;
//private int year1, month1, day1, year2, month2, day2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_unpaid_cust);
    startbt = (Button) findViewById(R.id.startbt);
    endbt = (Button) findViewById(R.id.endbt);
    subbt = (Button) findViewById(R.id.submitdatebt);
    starttv = (TextView) findViewById(R.id.startdateview);
    endtv = (TextView) findViewById(R.id.enddateview);
    sp = (Spinner) findViewById(R.id.area_spinner);
    area_list = getResources().getStringArray(R.array.area_list);
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, area_list);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    sp.setAdapter(dataAdapter);
    sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            String area = (String) parent.getItemAtPosition(position);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
    subbt = (Button) findViewById(R.id.submitdatebt);
    subbt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //new Postdate().execute();
            Toast.makeText(unpaid_cust.this, "from " + startdate + " to " + enddate, Toast.LENGTH_SHORT).show();

        }
    });


    startDate = Calendar.getInstance();


    startbt.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDateDialog(starttv, startDate);
                }
    });


    endDate = Calendar.getInstance();

            endbt.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDateDialog(endtv, endDate);

 }        });


    updateDisplay(starttv, startDate);
    updateDisplay(endtv, endDate);

}

private void updateDisplay(TextView dateDisplay, Calendar date) {
    dateDisplay.setText(
            new StringBuilder()
                    // Month is 0 based so add 1
                    .append(date.get(Calendar.MONTH) + 1).append("-")
                    .append(date.get(Calendar.DAY_OF_MONTH)).append("-")
                    .append(date.get(Calendar.YEAR)).append(" "));

}

public void showDateDialog(TextView dateDisplay, Calendar date) {
    activeDateDisplay = dateDisplay;
    activeDate = date;
    showDialog(DATE_DIALOG_ID);
}

private DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        activeDate.set(Calendar.YEAR, year);
        activeDate.set(Calendar.MONTH, monthOfYear);
        activeDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        startdate=year+"-"+monthOfYear+"-"+dayOfMonth;
        updateDisplay(activeDateDisplay, activeDate);
        unregisterDateDisplay();
    }
};


private void unregisterDateDisplay() {
    activeDateDisplay = null;
    activeDate = null;
}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this, dateSetListener, activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
    }
    return null;
}

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    super.onPrepareDialog(id, dialog);
    switch (id) {
        case DATE_DIALOG_ID:
            ((DatePickerDialog) dialog).updateDate(activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
            break;
    }
}

}

在上面的java代码中,我有两个字符串 startdate enddate ,我想将startdateview值放在enddate中的startdate和enddateview值中。 帮帮我......

3 个答案:

答案 0 :(得分:0)

我只是给你一个想法,..请不要与你的代码比较。您可以根据您的要求进行修改

datePicker.setButton(DatePickerDialog.BUTTON_POSITIVE, "Ok",
                new DialogInterface.OnClickListener() {

                    @SuppressWarnings("deprecation")
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        // selectdate = monthOfYear + "/" + dayOfMonth + "/"
                        // + year;


                        selectdate = datePicker.getSelectedMonth() + "/"
                                + datePicker.getSelectedDate() + "/"
                                + datePicker.getSelectedYear();
                        String date_current = txt_datedisplay.getText().toString();

创建一个扩展DatePickerDialog的类。完整代码在这里

import android.app.DatePickerDialog;

import android.content.Context; import android.widget.DatePicker;

public class CustomDatePickerDialog扩展了DatePickerDialog {

public static int mYear, mMonth, mDate, selectedYear, selectedMonth,
        selectedDate;
public static int dateflag = 0;
public static int dateflag2 = 0;

public CustomDatePickerDialog(Context context, OnDateSetListener callBack,
        int year, int monthOfYear, int dayOfMonth) {
    super(context, callBack, year, monthOfYear, dayOfMonth);
    // TODO Auto-generated constructor stub

    mYear = year;
    mMonth = monthOfYear;
    mDate = dayOfMonth;

    selectedYear = year;
    selectedDate = dayOfMonth;
    selectedMonth = monthOfYear;

}

@Override
public void onDateChanged(DatePicker view, int year, int month, int day) {
    // TODO Auto-generated method stub
    selectedYear = year;
    selectedDate = day;
    selectedMonth = month;
    super.onDateChanged(view, year, month, day);
}

// @Override
// public void onDateChanged(DatePicker view, int year, int month, int day)
// {
// // TODO Auto-generated method stub
// super.onDateChanged(view, year, month, day);
//
// view.updateDate(year, month, day);
// }

public int getSelectedYear() {
    return selectedYear;
}

public int getSelectedMonth() {
    return selectedMonth + 1;
}

public int getSelectedDate() {
    return selectedDate;
}

}

答案 1 :(得分:0)

最好你重构你的代码以便于阅读...使用像类名这样的java约定! 使用初始字符串设置两个臭名昭着的String variable以及如何处理对话框导入 使用代码周围的日志

updateDisplay(starttv, startDate); updateDisplay(endtv, endDate);

答案 2 :(得分:0)

我只是修改了我的代码..之前我复制了datepicker代码,但现在我自己一步一步地写了我的想法。

这是修改过的Java代码..

public class unpaid_cust extends ActionBarActivity {
String startdate, enddate;
Button startbt, endbt, subbt;
TextView starttv, endtv;
  private DatePicker datePicker;
private Calendar startDate, endDate;


int from_year, from_month, from_day, to_year, to_month, to_day;
static final int DATE_PICKER_TO = 0;
static final int DATE_PICKER_FROM = 1;

String[] area_list;
private TextView activeDateDisplay;
private Calendar activeDate;
Spinner sp;
//private int year1, month1, day1, year2, month2, day2;

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

            public void onDateSet(DatePicker view, int year,
                                  int monthOfYear, int dayOfMonth) {
                from_year = year;
                from_month = monthOfYear;
                from_day = dayOfMonth;
                updateStartDisplay();
                //displayToast();
            }
        };

private void updateStartDisplay() {
    starttv.setText(
            new StringBuilder()
                    // Month is 0 based so add 1
                    .append(from_month + 1).append("/")
                    .append(from_day).append("/")
                    .append(from_year).append(" "));
startdate=starttv.getText().toString();
}

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

            public void onDateSet(DatePicker view, int year,
                                  int monthOfYear, int dayOfMonth) {
                to_year = year;
                to_month = monthOfYear;
                to_day = dayOfMonth;
                updateEndDisplay();
                //displayToast();
            }
        };

private void updateEndDisplay() {
    endtv.setText(
            new StringBuilder()
                    // Month is 0 based so add 1
                    .append(to_month + 1).append("/")
                    .append(to_day).append("/")
                    .append(to_year).append(" "));
enddate=endtv.getText().toString();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_unpaid_cust);
    startbt = (Button) findViewById(R.id.startbt);
    endbt = (Button) findViewById(R.id.endbt);
    subbt = (Button) findViewById(R.id.submitdatebt);
    starttv = (TextView) findViewById(R.id.startdateview);
    endtv = (TextView) findViewById(R.id.enddateview);
    sp = (Spinner) findViewById(R.id.area_spinner);
    area_list = getResources().getStringArray(R.array.area_list);
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, area_list);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    sp.setAdapter(dataAdapter);
    sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            String area = (String) parent.getItemAtPosition(position);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
    subbt = (Button) findViewById(R.id.submitdatebt);

    startbt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showDialog(DATE_PICKER_FROM);
        }
    });

    endbt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showDialog(DATE_PICKER_TO);
        }
    });

    final Calendar cal = Calendar.getInstance();
    from_year = cal.get(Calendar.YEAR);
    from_month = cal.get(Calendar.MONTH);
    from_day = cal.get(Calendar.DAY_OF_MONTH);

    to_year = cal.get(Calendar.YEAR);
    to_month = cal.get(Calendar.MONTH);
    to_day = cal.get(Calendar.DAY_OF_MONTH);

    /** Display the current date in the TextView */
    updateStartDisplay();
    updateEndDisplay();

    subbt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //new Postdate().execute();
            Toast.makeText(unpaid_cust.this, "from " + startdate + " to " + enddate, Toast.LENGTH_SHORT).show();

        }
    });

}
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_PICKER_FROM:
                return new DatePickerDialog(this,
                        startDateSetListener,
                        from_year, from_month, from_day);

            case DATE_PICKER_TO:
                return new DatePickerDialog(this,
                        endDateSetListener,
                        to_year, to_month, to_day);

        }
        return null;
     }}

在上面的代码中,我在 startdate 字符串中获得了第一个datepicker值,在 enddate 字符串中获得了第二个datepicker值