私はstackoverflowの中に別の質問からこのソースを取得するには...DatePickerFragmentから日付文字列を転送するには?
これは私のfragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
android:background="#e1e1e1">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="55dp"
android:scrollbars="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#fff"
android:padding="10dp"
android:layout_marginBottom="10dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#fff">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Set Reminder"
style="@style/Base.TextAppearance.AppCompat.Medium"
android:layout_marginBottom="5dp"
android:layout_marginTop="10dp"
android:textColor="#009cee"/>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#e1e1e1"
android:layout_marginBottom="25dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Range Hari"
android:textStyle="bold"
android:textSize="16sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:inputType="number"
android:gravity="top"
android:textSize="16sp"
android:id="@+id/range"
android:ems="10" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tanggal Awal"
android:textStyle="bold"
android:textSize="16sp"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal"
android:background="#fff">
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:gravity="top"
android:textSize="16sp"
android:id="@+id/tanggalawaltxt"
android:ems="10" />
<Button
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#EC407A"
android:textColor="#ffffff"
android:text="Set Tanggal"
android:textSize="14sp"
android:id="@+id/tanggal"
android:layout_marginBottom="15dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:background="#fff">
<Button
android:layout_width="match_parent"
android:layout_marginLeft="10dp"
android:layout_height="wrap_content"
android:background="#009cee"
android:textColor="#ffffff"
android:text="Simpan"
android:textSize="14sp"
android:id="@+id/simpan"
android:layout_marginBottom="15dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
であり、これは私のDatePickerFragment.java
package com.example.gandi.symanlub;
/**
* Created by mgd on 10/07/16.
*/
public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(year, month, day);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(c.getTime());
}
}
とこれです私のsetreminder.javaです
package com.example.gandi.symanlub;
public class Setreminder extends Fragment {
Button btnshowtanggal;
TextView tanggalawal;
View rootview;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootview = inflater.inflate(R.layout.fragment_setreminder, container, false);
btnshowtanggal = (Button)rootview.findViewById(R.id.tanggal);
btnshowtanggal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment picker = new DatePickerFragment();
picker.show(getActivity().getFragmentManager(), "datePicker");
tanggalawal = (TextView) rootview.findViewById(R.id.tanggalawaltxt);
}
});
return rootview;
}
}
選択したDatetimepickerの文字列をtanggalawal(TextView)に取得するにはどうすればよいですか?
例コードで回答できますか?申し訳ありませんが、私はアンドロイドまたはjavaの初心者です。 – Megandi
ダイアログの例を編集しましたが、残りは自明です – Submersed