1. Trang chủ
  2. » Công Nghệ Thông Tin

Android chapter11 dialogboxes

17 87 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 17
Dung lượng 428,6 KB

Nội dung

10/4/2011 1 Ad id 11 A n d ro id  DialogBoxes AlertDialog &ToastWidgets VictorMatos ClevelandStateUniversity Notesarebasedon: AndroidDevelopers http://developer.android.com/index.html 11.Android–UI–TheDialogBox TheDialogBox Androidprovidestwoprimitiveformsof dialogboxes: 1. AlertDialog boxes,and 2. Toastcontrols 22 10/4/2011 2 11.Android–UI–TheDialogBox TheAlertDialog TheAlertDialog isanalmost modal screen that (1) presentsabriefmessagetotheuser typicallyshownasasmallfloating windowthatpartiallyobscuresthe underlyingview,and (2) collectsasimpleanswer(usuallyby 333 clickinganoptionbutton). Note: Afullymodal viewremainsonthescreenwaitingforuser’sinput.Therestof theapplicationisonhold.Ithastobedismissedbyanexplicituser’saction. 11.Android–UI–TheDialogBox TheAlertDialog Warning!!! AnAlertDialo g isNOT at yp icalin p utBox ( asin.NET ) g yp p ( ) Why? AnAlertDialog boxismodalasitneedsuserinterventionto beterminated 44 However itdoesnotstopthemainthread(codefollowingthecallto showtheDialogAlert boxisexecutedwithoutwaitingforthe user’sinput) 10/4/2011 3 11.Android–UI–TheDialogBox TheAlertDialog Dissectingan AlertDialog Box: Icon Title Negative Button Message 55 Positive Button Neutral Button 11.Android–UI–TheDialogBox TheAlertDialog Example:AsimpleDialogBox <LinearLayout android:id="@+id/LinearLayout01" android:layout width =" fill parent " android:layout_width fill_parent android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal"> <Button android:text="GO" android:id="@+id/btnGo" android:layout_width="wrap_content" android:layout_height="wrap_content"> </Button> <EditText d idhit "likth btt " 66 an d ro id : hi n t = " c li c k th e b u tt on " android:id="@+id/txtMsg" android:layout_width="fill_parent" android:layout_height="wrap_content"> </EditText> </LinearLayout> 10/4/2011 4 11.Android–UI–TheDialogBox TheAlertDialog Example: Asimpledialogbox package cis493.selectionwidgets; it did Atiit i mpor t an d ro id .app. A c ti v it y ; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class AndDemoUI1 extends Activity { 77 Button btnGo; EditText txtMsg; String msg; 11.Android–UI–TheDialogBox TheAlertDialog Example: Asimpledialogbox @Override public void onCreate(Bundle savedInstanceState) { super .onCreate ( savedInstanceState ); super .onCreate ( savedInstanceState ); setContentView(R.layout.main); txtMsg = (EditText)findViewById(R.id.txtMsg); btnGo = (Button) findViewById(R.id.btnGo); btnGo.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { AlertDialog dialBox = createDialogBox(); dialBox.show(); // WARNING: (in general ) // after showing a dialog you should have NO more code. Let the buttons of 88 // the dialog box handle the rest of the logic. For instance, in this // example a modal dialog box is displayed (once shown you can not do // anything to the parent until the child is closed) however the code in // the parent continues to execute after the show() method is // called. txtMsg.setText("I am here! "); } }); }//onCreate 10/4/2011 5 11.Android–UI–TheDialogBox TheAlertDialog Example: Asimpledialogbox private AlertDialog createDialogBox(){ AlertDialo g m yQ uittin g Dialo g Box = g yQ g g new AlertDialog.Builder(this) //set message, title, and icon .setTitle("Terminator") .setMessage("Are you sure that you want to quit?") .setIcon(R.drawable.ic_menu_end_conversation) //set three option buttons .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { //whatever should be done when answering "YES" goes here "YES " It tSti ( hi hB tt ) 99 msg = "YES " + I n t eger. t o St r i ng ( whi c hB u tt on ) ; txtMsg.setText(msg); } })//setPositiveButton 11.Android–UI–TheDialogBox TheAlertDialog Example: Asimpledialogbox .setNeutralButton("Cancel",new DialogInterface.OnClickListener() { p ublic void onClick ( D ialo g Interface d ialo g , i nt w hichButton ) { p g g //whatever should be done when answering "CANCEL" goes here msg = "CANCEL " + Integer.toString(whichButton); txtMsg.setText(msg); }//OnClick })//setNeutralButton .setNegativeButton("NO", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { //whatever should be done when answering "NO" goes here msg = "NO " + Integer.toString(whichButton); txtMsg.setText(msg); } }) // setNegativeButton 1010 }) // setNegativeButton .create(); .return myQuittingDialogBox; }// createDialogBox }// class 10/4/2011 6 11.Android–UI–TheDialogBox TheAlertDialog Example: AsimpleAlertDialog box Thistextisset right after right  after  showingthe dialogbox 1111 11.Android–UI–TheDialogBox TheToastView AToast isatransientview containingaquicklittlemessage fortheuser. Theyappearasafloatingview overtheapplication. Toastsneverreceivefocus! 1212 10/4/2011 7 11.Android–UI–TheDialogBox TheToastView Example: AsimpleToast Toast.makeText ( context, message, duration ).show(); Context:Areferencetotheview’senvironment(whatisaroundme…) Message:Thethingyouwanttosay Duration:SHORT(0)orLONG(1)exposure 1313 11.Android–UI–TheDialogBox TheToastView Example: AsimpleToast package cis493.dialogboxes; it d id A ti it i mpor t an d ro id .app. A c ti v it y ; import android.os.Bundle; import android.widget.Toast; public class ToastDemo1 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView ( R.layout. main ); 1414 setContentView ( R.layout. main ); Toast.makeText( getApplicationContext(), "Saludos amigos \n Hasta la vista", Toast.LENGTH_LONG).show(); } } 10/4/2011 8 11.Android–UI–TheDialogBox TheToastView Asanaside Context: OnAndroidaContextismostlyusedtoloadandaccessresources. AllwidgetsreceiveaContextparameterintheirconstructor. InaregularAndroidapplication,youusuallyhavetwokindsofContext, Activity andApplication.Thefirstoneistypicallypassedtoclasses andmethodsthatneedaContext. 1515 Viewshaveareferencetotheentireactivityandthereforetoanythingyour activityisholdingonto;usuallytheentireViewhierarchyandallitsresources. 11.Android–UI–TheDialogBox TheToastView CustomizingaToastView B y defaultToastviewsaredis p la y edatthecenter‐bott omofthe y py screen. HowevertheusermaychangetheplacementofaToastviewby usingeitherofthefollowingmethods: void setGravity (int gravity, int xOffset, int yOffset) Set the location at which the notification should appear on the screen. 1616 Set  the  location  at  which  the  notification  should  appear  on  the  screen. void setMargin (float horizontalMargin, float verticalMargin) Setthemarginsoftheview. 10/4/2011 9 11.Android–UI–TheDialogBox TheToastView CustomizingaToastView Thefollowin g methodusesoffsetvaluesbasedonthe p ixel g p resolutionoftheactualdevice.Forinstance,theG1phonescreen contains360x480pixels. void setGravity (int gravity, int xOffset, int yOffset) Gravity: Overallplacement.Typicalvaluesinclude:Gravity.CENTER,Gravity.TOP, Gravity.BOTTOM,… 1717 xOffset: AssumeGravity.CENTER placementonaG1phone.The xOffset rangeis‐160,…,0,…160(left,center,right) yOffset:TherangeonaG1is:‐240,…,0,…240(top,center, bottom) 11.Android–UI–TheDialogBox TheToastView CustomizingtheToastView AsecondmethodtoplaceaToastissetMargin.Thescreenisconsideredtohave acenterpointwherehorizontalandverticalcenterlinesmeet.Thereis50%of thescreentoeachsideofthatcenterpoint(top,botton,left,right).Marginsare expressedasavaluebetween:‐50,…,0,…,50. void setMargin (float horizontalMargin, float verticalMargin) 1818 Note: Thepairofmargins(‐50,‐50)representtheupper‐leftcornerofthescreen, (0,0)isthecenter,and(50,50)thelower‐rightcorner. 10/4/2011 10 11.Android–UI–TheDialogBox TheToastView Example: ChangingtheplacementofaToastview. 1919 UsingthesetGravity(…) methodwithGravity.CENTER,andxandyoffsetsof(resp.): 0,0(center) ‐160,‐240 (top‐left) 160,240 (right‐bottom) 11.Android–UI–TheDialogBox TheToastView Example: ChangingtheplacementofaToastview. <?xml version="1.0" encoding="utf-8"?> <TableLayout android:id="@+id/myTableLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ff0000ff" android:orientation="vertical" android:stretchColumns="1,2" xmlns:android="http://schemas.android.com/apk/res/android" > android:inputType="numberSigned" > </EditText> </TableRow> <TableRow android:id="@+id/myRow2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ff0000ff" android:padding = "10px " > <TableRow android:id="@+id/myRow1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/myCaption" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ff009999" android:text="Testing Toast - Gravity.CENTER 320x480 pixels" android:textSize="20sp" android:gravity="center" android:layout_span="2" > </TextView> </TableRow> <TableRow android:id="@+id/myRow1" android:layout _ width = "fill _ parent" android:padding = 10px android:orientation="horizontal" > <TextView android:id="@+id/yLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" Y offset: " android:textSize="18sp" > </TextView> <EditText android:id="@+id/yBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" android:textSize="18sp" android:inputType="numberSigned" > </EditText> </TableRow> < T ableRow 2020 _ _ android:layout_height="wrap_content" android:background="#ff0000ff" android:padding="10px" android:orientation="horizontal" > <TextView android:id="@+id/xLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" X offset: " android:textSize="18sp" > </TextView> <EditText android:id="@+id/xBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" android:textSize="18sp" android:id="@+id/myRow3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ff0000ff" android:padding="10px" android:orientation="horizontal" > <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" Show Toast " android:layout_span="2" > </Button> </TableRow> </TableLayout> [...]... xmlns :android= "http://schemas .android. com/apk/res /android" android: orientation="vertical" android: layout_width="fill_parent" android: layout_height="fill_parent" android: background="#777" > android: width 2dp android: color #ffffff00 26 13 10/4/2011 11. Android – UI – The DialogBox The Toast View... 11. Android – UI – The DialogBox The Toast View Example: Showing Fancy Toast views package cis493 .dialogboxes; import import import import import import import import import import android. app.Activity; android. os.Bundle; android. view.Gravity; android. view.LayoutInflater; android. view.View; android. view.ViewGroup; android. view.View.OnClickListener; android. widget.Button; android. widget.TextView; android. widget.Toast;...10/4/2011 11. Android – UI – The DialogBox The Toast View Example: Changing the placement of a Toast view package cis493 .dialogboxes; import import import import import import import import android. app.Activity; android. os.Bundle; android. view.Gravity; android. view.View; android. view.View.OnClickListener; android. widget.Button; android. widget.EditText; android. widget.Toast; public class... xmlns :android= "http://schemas .android. com/apk/res /android" android: id="@+id/my_toast_layout_root" android: orientation="horizontal" android: layout_width="fill_parent" android: layout_height="fill_parent" android: padding="10dp" > 24 12 10/4/2011 11. Android – UI – The DialogBox The Toast View Example: Showing Fancy Toast views Now we create our custom Toast layout (called:  y_ y ) my toast_layout.xml.   It must contain a TextView called ‘text’)

Ngày đăng: 16/03/2014, 23:37

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN