如何每次在我的日期选择器中获取用户选择的日期。用户将按下按钮打开日历,然后选择从和到的日期。选择此选项后,日期将附加到 TextView。但是我怎样才能每次都将日期存储到一个变量中,因为我需要将每个日期用作一个值。?
Tab2类:
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;
import com.borax12.materialdaterangepicker.date.DatePickerDialog;
import java.util.Calendar;
/**
* Created by hp1 on 21-01-2015.
*/
public class Tab2 extends Fragment implements DatePickerDialog.OnDateSetListener{
DatePicker pickerDate;
TextView dateTextView;
private boolean mAutoHighlight;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.tab2,container,false);
Button dateButton = (Button)v.findViewById(R.id.button2);
dateTextView = (TextView)v.findViewById(R.id.textView5);
CheckBox ahl = (CheckBox) v.findViewById(R.id.autohighlight_checkbox);
ahl.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
mAutoHighlight = b;
}
});
// Show a datepicker when the dateButton is clicked
dateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar now = Calendar.getInstance();
DatePickerDialog dpd = com.borax12.materialdaterangepicker.date.DatePickerDialog.newInstance(
Tab2.this,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
);
dpd.setAutoHighlight(mAutoHighlight);
dpd.show(getActivity().getFragmentManager(), "Datepickerdialog");
}
});
return v;
}
@Override
public void onResume() {
super.onResume();
DatePickerDialog dpd = (DatePickerDialog) getActivity().getFragmentManager().findFragmentByTag("Datepickerdialog");
if(dpd != null) dpd.setOnDateSetListener(this);
}
@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth,int yearEnd, int monthOfYearEnd, int dayOfMonthEnd) {
String date = "You picked the following date: From- "+dayOfMonth+"/"+(++monthOfYear)+"/"+year+" To "+dayOfMonthEnd+"/"+(++monthOfYearEnd)+"/"+yearEnd;
dateTextView.append(date + "\n");
}
布局:
<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<CheckBox
android:id="@+id/autohighlight_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"/>
</LinearLayout>