Simple Android example-IpHone style AlertDialog and iphonealertdialog
This example originated from the Internet. After downloading it, I added comments to it as a summary and sent them to my blog. Thank you for choosing the original author.
What I learned through this example
1. Use of the custom dialog box
2. How to store colors in the program to increase reusability
3. Deepen the use of linear layout and common controls
1. Implementation results
2. color value File
<?xml version="1.0" encoding="utf-8"?><resources> <drawable name="white">#FFFFFF</drawable> <color name="White">#FFFFFF</color> <color name="Black">#000000</color> <color name="grey">#D7D4D4</color> <color name="red">#FF0000</color> </resources>
3. The first interface layout File
<?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="wrap_content" android:layout_gravity="center_horizontal|center_vertical" android:orientation="vertical"> <LinearLayout android:orientation="vertical" android:background="@drawable/alert" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:layout_marginLeft="20dip" android:layout_marginRight="20dip"> <TextView android:id="@+id/dialog_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dip" android:textColor="#ffffff" android:textStyle="bold" android:textSize="17sp" android:text="About to call 323"/> <TextView android:id="@+id/dialog_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dip" android:gravity="center_horizontal" android:textSize="15sp" android:textColor="#ffffff" android:text="Are you sure you want to proceed?" /> <Button android:id="@+id/ok" android:layout_width="fill_parent" android:layout_height="40dip" android:layout_marginBottom="10dip" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" android:background="@drawable/custom_button" android:textColor="@color/White" android:textSize="17sp" android:textStyle="bold" android:text="OK"/> </LinearLayout></LinearLayout>
4. layout file of the Second Interface
<? 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 = "wrap_content" android: layout_gravity = "center_horizontal | center_vertical" android: orientation = "vertical"> <LinearLayout android: orientation = "vertical" android: background = "@ drawable/alert" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: g Ravity = "center_horizontal" android: layout_marginLeft = "20dip" android: layout_marginRight = "20dip"> <TextView android: id = "@ + id/dialog_title_2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginTop = "15dip" android: textColor = "# ffffff" android: textStyle = "bold" android: textSize = "17sp" android: text = "About to call 323"/> <TextView android: id = "@ + id/dialog_message _ 2 "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: layout_margin =" 10dip "android: gravity =" center_horizontal "android: textSize =" 15sp "android: textColor = "# ffffff" android: text = "Are you sure you want to proceed? "/> <LinearLayout android: layout_width =" fill_parent "android: layout_height =" wrap_content "android: layout_marginBottom =" 10dip "android: layout_marginTop =" 10dip "android: gravity = "center_horizontal" android: orientation = "horizontal"> <Button android: id = "@ + id/cancel_2" android: layout_width = "0dip" android: layout_height = "40dip" android: layout_gravity = "left" android: layout_marginLeft = "10dip" android: layout_weight = "1" android: background = "@ drawable/custom_button" android: text = "cancel" android: textColor = "@ color/White" android: textStyle = "bold"/> <Button android: id = "@ + id/OK _2" android: layout_width = "0dip" android: layout_height = "40dip" android: layout_marginBottom = "10dip" android: layout_marginRight = "10dip" android: layout_weight = "1" android: background = "@ drawable/custom_button" android: text = "OK" android: textColor = "@ color/White" android: textStyle = "bold"/> </LinearLayout>
5. Core code file
Public class MainActivity extends Activity {private Button btn; private Button btn2; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); btn = (Button) findViewById (R. id. btn1);/** add listener events **/btn. setOnClickListener (new OnClickListener () {@ Override public void onClick (View view) {// TODO Auto-generated method stub showCustomMessageOK ("prompt message ", "This operation cannot be performed") ;}}); btn2 = (Button) findViewById (R. id. btn2); btn2.setOnClickListener (new OnClickListener () {@ Override public void onClick (View view) {// TODO Auto-generated method stub showCustomMessageOKAndCancle ("tip ", "Are you sure you want to exit ");}});} /*** implement a dialog box with the OK and cancel buttons * @ param title * @ param message */protected void showCustomMessageOKAndCancle (String title, String message) {// TODO Auto-generated method stub/*** creates a Dialog object. The Dialog has two constructor Methods * 1. * ***/final Dialog dialog = new Dialog (MainActivity. this, android. r. style. theme_Translucent_NoTitleBar);/** load layout file for Dialog **/dialog. setContentView (R. layout. OK _cancle_dialog_view);/** set the corresponding attribute value **/(TextView) dialog. findViewById (R. id. dialog_title_2 )). setText (title); (TextView) dialog. findViewById (R. id. dialog_message_2 )). setText (message); (Button) dialog. findViewById (R. id. cancel_2 )). setOnClickListener (new OnClickListener () {@ Override public void onClick (View view) {// TODO Auto-generated method stub dialog. dismiss () ;}}); (Button) dialog. findViewById (R. id. OK _2 )). setOnClickListener (new OnClickListener () {@ Override public void onClick (View view) {// TODO Auto-generated method stub MainActivity. this. finish (); System. exit (0) ;}}); dialog. show ();}/*** create a dialog box with only the OK button * @ param title * @ param message */private void showCustomMessageOK (String title, String message) {// TODO Auto-generated method stub final Dialog dialog = new Dialog (MainActivity. this, android. r. style. theme_Translucent_NoTitleBar); dialog. setContentView (R. layout. OK _dialog_view); (TextView) dialog. findViewById (R. id. dialog_title )). setText (title); (TextView) dialog. findViewById (R. id. dialog_message )). setText (message); (Button) dialog. findViewById (R. id. OK )). setText ("OK"); (Button) dialog. findViewById (R. id. OK )). setOnClickListener (new OnClickListener () {@ Override public void onClick (View view) {// TODO Auto-generated method stub dialog. dismiss () ;}}); dialog. show ();}}
Code