Hello. I'm Wataku, a server-side programmer who is studying programming at a certain school. : relaxed: Let's develop Android this time as well.
--A person who can write Java somehow. --Someone who is ambiguous in Android development but can do it a little.
public class SimpleDialogFragment extends DialogFragment {
** 2) Describe the dialog body generation process in the onCreateDialog () method. ** **
public Dialog onCreateDialog( ... ) {
Dialog generation process(See below)
}
** 3) Write the following code when displaying a dialog for activities etc. **
--New the above class.
SimpleDialogFragment dialog = new SimpleDialogFragment();
--Get Fragment Manager.
FragmentManager manager = getSupportedFragmentManager();
--Display the dialog with the show () method.
<The following two arguments.>
1st:Fragment manager
No. 2:Tag string that identifies the dialog(Arbitrary string)
dialog . show ( manager , “ ... ” );
(note)
Both DialogFragment.FragmentManager import the v4 package.
○ import android.support.v4.app.FragmentManager
× import android.app.FragmentManager
Follow the procedure below to generate the error dialog body described in the onCreateDialog () method of the dialog fragment class you created.
** 1) Create an alert dialog builder object. ** **
AlertDialog.Builder builder = new AlertDialog.Builder(context);
** 2) Set the display contents. ** **
・ Content area
builder.setMessage( ... );
·title
builder.setTitle( ... );
** 3) Set the action button. ** **
builder . setPositiveButton(“Button label”,Listener object when the button is tapped(See below));
There are also * setNegativeButton () * and * setNeutralButton () *.
** 4) Create a dialog object. ** **
AlertDialog dialog = builder.create( ) ;
(note)
ʻThe AlertDialog class imports from the v7 package
`
The process when the action button is tapped in the alert dialog is described in the listener class. The procedure is as follows. ** 1) Create a nested class that implements the DialogInterface.OnClickListener interface. ** **
** 2) Describe the process in the onClick () method. ** **
private class DialogButtonClickListener implements DialogInterface.OnClickListener {
@Override
public void onClick(DialogInterface dialog (dialog body),int which (constant value of tapped button)) {
//Click processing
}
}
(note)
If there are multiple action buttons, a listener class may be prepared for each button, but it may be branched using the second argument (int which).
** (1) Get activity **
To get the activity that the dialog body is displayed in the dialog fragment, use the ** getActivity () ** method.
Activity parent = getActivity();
** (2) Data I **
To pass data from an activity to a dialog fragment, use the following method. ** 1) Create a Bundle object. ** **
Bundle extras = new Bundle();
** 2) Describe the process in the onClick () method. ** **
extends.put data type("name",value);
** 3) Set the Bundle object of 2) in the dialog fragment object. ** **
dialog.setArgments(extends);
** 4) In the dialog fragment, get the above Bundle object with the getArgments () method and retrieve the data. → The extraction method is the same as for Intent (null check in advance) **
FullDialogFragment.java
public class FullDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Normal dialog");
builder.setMessage("Is it OK?");
builder.setPositiveButton("ok", new DialogButtonClickListener());
builder.setNeutralButton("??", new DialogButtonClickListener());
builder.setNegativeButton("NG", new DialogButtonClickListener());
AlertDialog dialog = builder.create();
return dialog;
}
/**
*A member class that describes what happens when a dialog button is pressed.
*/
private class DialogButtonClickListener implements DialogInterface.OnClickListener {
@Override
public void onClick(DialogInterface dialog, int which) {
Activity parent = getActivity();
String msg = "";
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
msg = "Normal dialog:OK is selected.";
break;
case DialogInterface.BUTTON_NEUTRAL:
msg = "Normal dialog: ??Was selected.";
break;
case DialogInterface.BUTTON_NEGATIVE:
msg = "Normal dialog:NG has been selected.";
break;
}
Toast.makeText(parent, msg, Toast.LENGTH_SHORT).show();
}
}
}
Activity class (excerpt)
FullDialogFragment dialog = new FullDialogFragment();
FragmentManager manager = getSupportFragmentManager();
dialog.show(manager,"FullDialogFragment");
TIPS
For the date selection dialog, "new" ** DatePickerDialog ** and ** show () **. The arguments are as follows.
-① Context -② Listener object -③ Initial value "year" -④ Initial value "month" → Java "month" starts * 0 * internally. -⑤ Initial value "day"
The listener object * implements * the ** DatePickerDialog.OnDateSetListener ** interface and describes the process in the ** onDateSet () ** method. The arguments are as follows.
-① Date specification screen parts -② Selected "year" -③ Selected "month" -④ Selected "day"
public void showDatePickerDialog(View view){
Calendar cal = Calendar.getInstance();
int nowYear = cal.get(Calendar.YEAR);
int nowMonth = cal.get(Calendar.MONTH);
int nowDayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(DialogSampleActivity.this, new DatePickerDialogDateSetListener(),nowYear,nowMonth,nowDayOfMonth);
dialog.show();
}
private class DatePickerDialogDateSetListener implements DatePickerDialog.OnDateSetListener {
@Override
public void onDateSet(DatePicker view,int year,int month,int dayOfMonth) {
String msg = "Date selection dialog:" + year + "Year" + (month + 1) + "Month" + dayOfMonth + "Day";
Toast.makeText(DialogSampleActivity.this, msg, Toast.LENGTH_SHORT).show();
}
}
For the time selection dialog, "new" ** TimePickerDialog ** and ** show () **. The arguments are as follows.
-① Context -② Listener object -③ Initial value "hour" (24-hour unit) -④ Initial value "minute" -⑤ Whether to display 24 hours (true is 24 hours notation)
The listener object * implements * the ** TimePickerDialog.OnTimeSetListener ** interface and describes the process in the ** onTimeSet () ** method. The arguments are as follows.
-① Time specification screen parts -② Selected "time" --③ Selected "minutes"
public void showTimePickerDialog(View view){
TimePickerDialog dialog = new TimePickerDialog(DialogSampleActivity.this, new TimePickerDialogTimeSetListener(),0,0,true );
dialog.show();
}
private class TimePickerDialogTimeSetListener implements TimePickerDialog.OnTimeSetListener {
@Override
public void onTimeSet(TimePicker view,int hourOfDay,int minute) {
String msg = "Time selection dialog:" + hourOfDay + "Time" + minute + "Minutes";
Toast.makeText(DialogSampleActivity.this, msg, Toast.LENGTH_SHORT).show();
}
}
that's all. It was how to display the dialog on Android. If you have any suggestions such as something wrong, please contact us. Thank you for reading to the end.
Recommended Posts