The Dialog class is the base class for creating dialogs. However, you typically should not instantiate a Dialog directly. Instead, you should use one of the following subclasses:
- AlertDialog
- A dialog that can manage zero, one, two, or three buttons, and/or a list of selectable items that can include checkboxes or radio buttons. The AlertDialog is capable of constructing most dialog user interfaces and is the suggested dialog type.
- ProgressDialog
- A dialog that displays a progress wheel or progress bar. Because it's an extension of the AlertDialog, it also supports buttons.
- DatePickerDialog
- A dialog that allows the user to select a date.
- TimePickerDialog
- A dialog that allows the user to select a time.
Source for AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <? xml version = "1.0" encoding = "utf-8" ?> package = "com.as400samplecode" android:versionCode = "1" android:versionName = "1.0" > < uses-sdk android:minSdkVersion = "12" /> < application android:icon = "@drawable/icon" android:label = "@string/app_name" > < activity android:name = ".AndroidDialogs" android:label = "@string/app_name" android:theme = "@android:style/Theme.Holo.Light" > > < intent-filter > < action android:name = "android.intent.action.MAIN" /> < category android:name = "android.intent.category.LAUNCHER" /> </ intent-filter > </ activity > < activity android:name = ".MyCalendar" android:theme = "@style/MyTheme" /> </ application > </ manifest > |
Source for styles.xml
1 2 3 4 5 6 7 | <? xml version = "1.0" encoding = "utf-8" ?> < resources > < style name = "MyTheme" parent = "@android:style/Theme.Holo.Light" > < item name = "android:windowActionBar" >false</ item > < item name = "android:windowNoTitle" >true</ item > </ style > </ resources > |
Source for main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | <? xml version = "1.0" encoding = "utf-8" ?> android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:orientation = "horizontal" android:gravity = "center" android:id = "@+id/layout1" > < TableLayout android:layout_height = "match_parent" android:layout_width = "wrap_content" android:id = "@+id/tableLayout1" > < TableRow android:id = "@+id/tableRow1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" > < TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:textSize = "25sp" android:id = "@+id/dateText" android:text = "Android Dialog Date:" /> < EditText android:text = "EditText" android:layout_height = "wrap_content" android:id = "@+id/dateDisplay" android:layout_width = "wrap_content" android:inputType = "date" android:textStyle = "bold" android:enabled = "false" /> < Button android:layout_width = "wrap_content" android:id = "@+id/pickDate" android:textSize = "12sp" android:text = "Change Date" android:layout_height = "wrap_content" /> </ TableRow > < TableRow android:id = "@+id/tableRow2" android:layout_width = "wrap_content" android:layout_height = "wrap_content" > < TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:textSize = "25sp" android:id = "@+id/dateText" android:text = "Calendar View Date:" /> < EditText android:text = "EditText" android:layout_height = "wrap_content" android:id = "@+id/dateDisplay2" android:layout_width = "wrap_content" android:inputType = "date" android:textStyle = "bold" android:enabled = "false" /> < Button android:layout_width = "wrap_content" android:id = "@+id/pickDate2" android:textSize = "12sp" android:text = "Change Date" android:layout_height = "wrap_content" /> </ TableRow > < TableRow android:id = "@+id/tableRow2" android:layout_width = "wrap_content" android:layout_height = "wrap_content" > < TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:textSize = "25sp" android:id = "@+id/dateText" android:text = "Android Dialog Time:" /> < EditText android:text = "EditText" android:layout_height = "wrap_content" android:id = "@+id/timeDisplay" android:layout_width = "wrap_content" android:inputType = "date" android:textStyle = "bold" android:enabled = "false" /> < Button android:layout_width = "wrap_content" android:id = "@+id/pickTime" android:textSize = "12sp" android:text = "Change Time" android:layout_height = "wrap_content" /> </ TableRow > </ TableLayout > </ LinearLayout > |
Source for mycalendar.xml
1 2 3 4 5 6 7 | <? xml version = "1.0" encoding = "utf-8" ?> < FrameLayout android:layout_width = "wrap_content" android:layout_height = "match_parent" android:id = "@+id/frameLayout1" < CalendarView android:id = "@+id/calendarView1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" /> </ FrameLayout > |
Source for MyCalendar.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | package com.as400samplecode; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.CalendarView; public class MyCalendar extends Activity { private int mYear; private int mMonth; private int mDay; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); CalendarView calendar = new CalendarView( this ); calendar.setOnDateChangeListener(mDateSetListener); setContentView(calendar); } // the callback received when the user "sets" the date in the dialog private CalendarView.OnDateChangeListener mDateSetListener = new CalendarView.OnDateChangeListener() { public void onSelectedDayChange(CalendarView view, int year, int monthOfYear, int dayOfMonth) { mYear = year; mMonth = monthOfYear; mDay = dayOfMonth; String selectedDate = new StringBuilder().append(mMonth + 1 ).append( "/" ).append(mDay).append( "/" ) .append(mYear).append( " " ).toString(); Bundle b = new Bundle(); b.putString( "dateSelected" , selectedDate); //Add the set of extended data to the intent and start it Intent intent = new Intent(); intent.putExtras(b); setResult(RESULT_OK,intent); finish(); } }; } |
Source for AndroidDialogs.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | package com.as400samplecode; import java.util.Calendar; import android.app.Activity; import android.app.DatePickerDialog; import android.app.Dialog; import android.app.TimePickerDialog; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.View; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.TimePicker; public class AndroidDialogs extends Activity { private EditText mDateDisplay; private Button mPickDate; private EditText mDateDisplay2; private Button mPickDate2; private int mYear; private int mMonth; private int mDay; private EditText mTimeDisplay; private Button mPickTime; private int mHour; private int mMinute; static final int DATE_DIALOG_ID = 0 ; static final int CALENDAR_VIEW_ID = 1 ; static final int TIME_DIALOG_ID = 2 ; @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.optionsmenu, menu); return true ; } @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); // capture our View elements mDateDisplay = (EditText) findViewById(R.id.dateDisplay); mPickDate = (Button) findViewById(R.id.pickDate); mDateDisplay2 = (EditText) findViewById(R.id.dateDisplay2); mPickDate2 = (Button) findViewById(R.id.pickDate2); mTimeDisplay = (EditText) findViewById(R.id.timeDisplay); mPickTime = (Button) findViewById(R.id.pickTime); // add a click listener to the select a date button mPickDate.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { showDialog(DATE_DIALOG_ID); } }); // add a click listener to the select a calendar date button mPickDate2.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(AndroidDialogs. this , MyCalendar. class ); startActivityForResult(intent,CALENDAR_VIEW_ID); } }); // add a click listener to the button mPickTime.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { showDialog(TIME_DIALOG_ID); } }); // get the current date and time final Calendar c = Calendar.getInstance(); mYear = c.get(Calendar.YEAR); mMonth = c.get(Calendar.MONTH); mDay = c.get(Calendar.DAY_OF_MONTH); mHour = c.get(Calendar.HOUR_OF_DAY); mMinute = c.get(Calendar.MINUTE); // display the current date displayDate(); displayCalendarViewDate(); // display the current time displayTime(); } protected void onActivityResult( int requestCode, int resultCode, Intent data) { switch (requestCode) { case CALENDAR_VIEW_ID: if (resultCode == RESULT_OK) { Bundle bundle = data.getExtras(); mDateDisplay2 = (EditText) findViewById(R.id.dateDisplay2); mDateDisplay2.setText(bundle.getString( "dateSelected" )); break ; } } } // updates the date in the EditText private void displayDate() { mDateDisplay.setText( new StringBuilder() // Month is 0 based so add 1 .append(mMonth + 1 ).append( "/" ) .append(mDay).append( "/" ) .append(mYear).append( " " )); } // updates the date in the EditText private void displayCalendarViewDate() { mDateDisplay2.setText( new StringBuilder() // Month is 0 based so add 1 .append(mMonth + 1 ).append( "/" ) .append(mDay).append( "/" ) .append(mYear).append( " " )); } // updates the time we display in the EditText private void displayTime() { mTimeDisplay.setText( new StringBuilder() .append(pad(mHour)).append( ":" ) .append(pad(mMinute))); } private static String pad( int c) { if (c >= 10 ) return String.valueOf(c); else return "0" + String.valueOf(c); } // the callback received when the user "sets" the date in the dialog private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { mYear = year; mMonth = monthOfYear; mDay = dayOfMonth; displayDate(); } }; // the callback received when the user "sets" the time in the dialog private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() { public void onTimeSet(TimePicker view, int hourOfDay, int minute) { mHour = hourOfDay; mMinute = minute; displayTime(); } }; @Override protected Dialog onCreateDialog( int id) { switch (id) { case DATE_DIALOG_ID: return new DatePickerDialog( this , mDateSetListener, mYear, mMonth, mDay); case TIME_DIALOG_ID: return new TimePickerDialog( this , mTimeSetListener, mHour, mMinute, false ); } return null ; } } |
No comments:
Post a Comment
NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.