Lập trình Android tiếng Việt - Chapter 11: Android Dialog Boxes

Customizing a Toast View

 

By default Toast views are displayed at the center-bottom of the 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.

 

void setMargin (float horizontalMargin, float verticalMargin)

Set the margins of the view.

 

pptx31 trang | Chuyên mục: Android | Chia sẻ: dkS00TYs | Lượt xem: 1957 | Lượt tải: 5download
Tóm tắt nội dung Lập trình Android tiếng Việt - Chapter 11: Android Dialog Boxes, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
t(msg); } })//setPositiveButton 10 11. Android – UI – The DialogBox The AlertDialog 10 Example: A simple dialog box .setNeutralButton("Cancel",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { 	//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 .create(); .return myQuittingDialogBox; }// createDialogBox }// class 11 11. Android – UI – The DialogBox The AlertDialog 11 Example: A simple AlertDialog box This text is set right after showing the dialog box 12 11. Android – UI – The DialogBox The Toast View 12 A Toast is a transient view containing a quick little message for the user. They appear as a floating view over the application. They never receive focus. 13 11. Android – UI – The DialogBox The Toast View 13 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 or LONG exposure 14 11. Android – UI – The DialogBox The Toast View 14 Example: A simple Toast package cis493.dialogboxes; import android.app.Activity; 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); Toast.makeText( 	getApplicationContext(), 	"Saludos amigos \n Hasta la vista", 	Toast.LENGTH_LONG).show(); } } 15 11. Android – UI – The DialogBox The Toast View 15 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. 	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. 16 11. Android – UI – The DialogBox The Toast View 16 Customizing a Toast View By default Toast views are displayed at the center-bottom of the 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. void setMargin (float horizontalMargin, float verticalMargin) Set the margins of the view.	 17 11. Android – UI – The DialogBox The Toast View 17 Customizing a Toast View The following method uses offset values based on the pixel 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, … 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) 18 11. Android – UI – The DialogBox The Toast View 18 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) 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. 19 11. Android – UI – The DialogBox The Toast View 19 Example: Changing the placement of a Toast view. 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) 20 11. Android – UI – The DialogBox The Toast View 20 Example: Changing the placement of a Toast view. 21 11. Android – UI – The DialogBox The Toast View 21 Example: Changing the placement of a Toast view. package cis493.dialogboxes; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class ToastDemo1 extends Activity { EditText xBox; EditText yBox; Button btn1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main2); xBox = (EditText)findViewById(R.id.xBox); yBox = (EditText)findViewById(R.id.yBox); 22 11. Android – UI – The DialogBox The Toast View 22 Example: Changing the placement of a Toast view. btn1 = (Button)findViewById(R.id.btn1); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { Toast myToast = Toast.makeText( getApplicationContext(), "Saludos amigos \n Hasta la vista", Toast.LENGTH_LONG); myToast.setGravity(Gravity.CENTER, Integer.valueOf(xBox.getText().toString()), Integer.valueOf(yBox.getText().toString()) ); myToast.show(); } catch (NumberFormatException e) { Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show(); 	 } }// onClick }); // listener }// onCreate }// class 23 11. Android – UI – The DialogBox The Toast View 23 Example: Showing Fancy Toast views. Toasts could be modified to display a custom combination of color/shape/text/background. You need to follow the next steps: Define the XML layout of the new custom view Make sure there is a TextView named: text Additionally you could attach an android: background to the TextView. The background could be a figure (such as a .png file) or an XML defined shape (see next example). Example taken from:  24 11. Android – UI – The DialogBox The Toast View 24 Example: Showing Fancy Toast views. Let’s begin with the application’s main layout. 25 11. Android – UI – The DialogBox The Toast View 25 Example: Showing Fancy Toast views. Now we create our custom Toast layout (called: my_toast_layout.xml. It must contain a TextView called ‘text’) Optional background Required TextView 26 11. Android – UI – The DialogBox The Toast View 26 Example: Showing Fancy Toast views. Finally we take care of the optional background element (my_border.xml). In this example we define a (but it could be any .png image). This XML (or image) is saved in the folder: /res/drawable 27 11. Android – UI – The DialogBox The Toast View 27 Example: Showing Fancy Toast views. Testing the application A Toast displayed with our custom layout A Toast displayed using standard formatting 28 11. Android – UI – The DialogBox The Toast View 28 Example: Showing Fancy Toast views. package cis493.dialogboxes; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class ToastDemo2 extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); 29 11. Android – UI – The DialogBox The Toast View 29 Example: Showing Fancy Toast views. Button btnShowToast = (Button) findViewById(R.id.btnShowToast); btnShowToast.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { 	//custom made TOAST 	LayoutInflater inflater = getLayoutInflater(); 	View layout = inflater.inflate( 	R.layout.my_toast_layout, 	(ViewGroup) findViewById(R.id.my_toast_layout_root)); 	TextView text = (TextView) layout.findViewById(R.id.text); 	Toast toast = new Toast(getApplicationContext()); text.setText("Hola mundo \nI'm a fancy Toast"); toast.setGravity(Gravity.CENTER, 0, 0); toast.setDuration(Toast.LENGTH_SHORT); toast.setView(layout); toast.show(); // normal TOAST Toast.makeText(getApplicationContext(), 	"Hola mundo \nnow I am quite normal", 	Toast.LENGTH_SHORT).show(); 	} }); } } 30 11. Android – UI – The DialogBox The Toast View 30 Example: Showing Fancy Toast views. As an aside: Inflating a View You may want occasionally to modify the way Android renders a particular view (perhaps a different color, style, or shape). Once the Hierarchy View has been displayed, you can take any terminal node and extend it by inflating a custom ‘view sub-tree’. Also, by using layout inflation we may draw a new Hierarchy on top of the existing screen. In our example, our customized rendition of a Toast box (including a colorful background) is defined in an XML file. Depicting the image of the custom Toast is accomplished by inflating the XML layout spec. 31 11. Android – UI – The DialogBox The Toast View 31 Example: Showing Fancy Toast views. As an aside: Inflating a View Syntaxt public View inflate (int resource, ViewGroup root) Inflate a new view hierarchy from the specified xml resource. Parameters resource ID for an XML layout resource to load, root: optional view to be the parent of the generated hierarchy. Returns The root View of the inflated hierarchy. If root was supplied, this is the root View; otherwise it is the root of the inflated XML file. LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate( 	R.layout.my_toast_layout, 	(ViewGroup) findViewById(R.id.my_toast_layout_root)); TextView text = (TextView) layout.findViewById(R.id.text); 32 32 	 11. Android – UI – The DialogBox Dialog Boxes 32 

File đính kèm:

  • pptxLập trình Android tiếng Việt - Chapter 11 Android Dialog Boxes.pptx
Tài liệu liên quan