0

毎回、日付ピッカーでユーザーが選択した日付を取得する方法を教えてください。ユーザーは、ボタンを押してカレンダーを開き、から日付を選択します。これを選択すると、日付がTextViewに追加されます。しかし、どのように変数に格納するたびに日付を取得できますか?各日付を値として使用する必要があります。Datepickerから選択した値を取得し、各選択項目の変数に格納します。

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> 

答えて

0

使用OnDateSetListener。このようなもの

int year,monthofyear,dayofmonth; 
    dpd = new DatePickerDialog.OnDateSetListener() { 

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

      preview.setText(new StringBuilder().append(year+"/") 
        .append(monthOfYear+"/").append(dayOfMonth)); 
     } 
    }; 
+0

これを使用して個々の日付を取得するにはどうすればよいですか?ユーザーが10個の日付を追加したとすると、各日付の間に曜日を計算する変数を追加するにはどうすればよいですか? – Sam

関連する問題