Thứ Sáu, 25 tháng 4, 2014

Displaying a Dialog Window [Activities, Fragments, And Intents]


T
here are times when you need to display a dialog windo to get a confirmation from the user. In this case, you can override the onCreateDialog() protected metho defined in the Activity base class to display a dialog window Thefollowing Try It Out showyou how.

1.           Using Eclipse, create a new Androi project and name it Dialog.
2.           Add the following statements in bold to the main.xml file:

<?xml   version=”1.0” encoding=”utf-8”?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent”
android:orientation=”vertical” >

<Button android:id=”@+id/btn_dialog” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Click to display  dialog” android:onClick=”onClick”  />

</LinearLayout>

3.           Add the following statements in bold to the DialogActivity.java file:

package  net.learn2develop.Dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle; import android.view.View; import android.widget.Toast;

public class  DialogActivity  extends Activity  {
CharSequence[] items   { “Google”,  “Apple”,  “Microsoft” };
boolean[]  itemsChecked   new  boolean [items.length];

/** Called when  the activity is first  created. */
@Override
public  void onCreate(Bundle savedInstanceState)  super.onCreate(savedInstanceState); setContentView(R.layout.main);
}

public  void onClick(View v) showDialog(0);
}

@Override
protected  Dialog onCreateDialog(int id) {
switch (id) {
case 0:
return new  AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle(“This  is  dialog  with some  simple text...”)
.setPositiveButton(“OK”,
new  DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,  int  whichButton)
{
Toast.makeText(getBaseContext(),
“OK  clicked!”,  Toast.LENGTH_SHORT).show();
}
}
)
.setNegativeButton(“Cancel”,
new  DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,  int  whichButton)
{
Toast.makeText(getBaseContext(),
“Cancel clicked!”,  Toast.LENGTH_SHORT).show();
}
}
)
.setMultiChoiceItems(items,  itemsChecked,
new  DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface  dialog,
int  which, boolean isChecked) Toast.makeText(getBaseContext(),
items[which]  (isChecked  “  checked!”:  unchecked!”), Toast.LENGTH_SHORT).show();
}
}
).create();


}
return null;
}

}
4.           Press F11 to debug the application on the Androi emulator.
Click the button to display the dialog (see Figure 2-5)Checking the various checkboxe will cause the Toast clasto display the text of the item checked/unchecked. To dismisthe dialog,click the OK or Cancel button.


FIGURE 2-5

How It Works

To display a dialog, you first implemen the onCreateDialog()
metho in the Activity class:

@Override
protected Dialog onCreateDialog(int id)  {
//...
}

This metho is called when you call the showDialog() method:

public  void onClick(View v)  {
showDialog(0);
}

The onCreateDialog() method  is a callback for creating dialogs that are managed  by the activity. When you call the showDialog() method,  this callback will be invoked.  The showDialog() method  accepts
an integer argument identifying a particular dialog to display. In this case, we used a switch statement to identify the different types of dialogs to create, although the current  example creates only one type of dialog. Subsequent  Try ItOut exercises will extend this example to create different types of dialogs.

To create a dialog, you use the AlertDialog classs Builder constructor. You set the various properties, such as icon, title, and buttons, as well as checkboxes:

@Override
protected Dialog onCreateDialog(int id)  {
switch (id)  {
case 0:
return new  AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle(“This  is  dialog  with some  simple text...”)
.setPositiveButton(“OK”,
new  DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,  int  whichButton)
{
Toast.makeText(getBaseContext(),
“OK  clicked!”,  Toast.LENGTH_SHORT).show();
}
}
)
.setNegativeButton(“Cancel”,
new  DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,  int  whichButton)
{
Toast.makeText(getBaseContext(),
“Cancel clicked!”,  Toast.LENGTH_SHORT).show();
}
}
)
.setMultiChoiceItems(items,  itemsChecked,
new  DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface  dialog,
int  which, boolean isChecked) Toast.makeText(getBaseContext(),
items[which]  (isChecked  “  checked!”:  unchecked!”), Toast.LENGTH_SHORT).show();
}
}
).create();
 }
return null;
}

The preceding code sets two buttons, OK and Cancel, using the setPositiveButton() and setNegativeButton() methods respectively. You also set a list of checkboxe for users to choose vithe setMultiChoiceItems() method For thesetMultiChoiceItems() method you passed in twarrays: one for the list of items to display and another to contai the value of each item, to indicate ithey are checked. When each item is checked, you use the Toast class to displaya message indicating the item that was checked.

The preceding code for creating the dialog looks complicated, but it could easily be rewritten as follows:

package  net.learn2develop.Dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle; import android.view.View; import android.widget.Toast;

public class  DialogActivity  extends Activity  {
CharSequence[] items   { “Google”,  “Apple”,  “Microsoft” };
boolean[]  itemsChecked   new  boolean [items.length];

/** Called when  the activity is first  created. */
@Override
public  void onCreate(Bundle savedInstanceState)  super.onCreate(savedInstanceState); setContentView(R.layout.main);
}

public  void onClick(View v)  showDialog(0);
}

@Override
protected Dialog onCreateDialog(int id)  {
switch (id)  {
case 0:
Builder builder   new  AlertDialog.Builder(this); builder.setIcon(R.drawable.ic_launcher);
builder.setTitle(“This is   dialog  with some  simple text...”); builder.setPositiveButton(“OK”,
new  DialogInterface.OnClickListener() {
public void onClick(DialogInterface  dialog,        int  whichButton) Toast.makeText(getBaseContext(),
“OK  clicked!”,  Toast.LENGTH_SHORT).show();
}
}
);

builder.setNegativeButton(“Cancel”,
new  DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,  int  whichButton) Toast.makeText(getBaseContext(),
“Cancel clicked!”,  Toast.LENGTH_SHORT).show();
}
}
);

builder.setMultiChoiceItems(items,  itemsChecked,
new  DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface  dialog,
int  which, boolean isChecked) Toast.makeText(getBaseContext(),
items[which]  (isChecked  “  checked!”:  unchecked!”), Toast.LENGTH_SHORT).show();
}
}
);
return builder.create();
}
return null;
}
}
THE CONTEXT OBJECT

In Android,  you often encounter the Context class and its instances.  Instances of the Context class are often used to provide references to your application. For example, in the following code snippet, the firstparameter of the Toast class takes in Context object:

.setPositiveButton(“OK”,
new  DialogInterface.OnClickListener()  {
public void onClick(DialogInterface dialog,  int  whichButton)
{
Toast.makeText(getBaseContext(),
“OK clicked!”,  Toast.LENGTH_SHORT).show();
}
}

However because the Toast() class is not used directly in the activity (it is usewithin the AlertDialog class), you need to retur an instance of the Context class by using the getBaseContext() method.

You also encounter the Context class when creating a view dynamicall in an activity. For example you may want to dynamicall create a TextView from codeTo do so, you instantiate the TextView class, like this:

TextView tv   new  TextView(this);

The constructor for the TextView class takes a Context object; and because the Activity class is a subclass of Context, you can use the this keywor to representhe Context object. 

Không có nhận xét nào:

Đăng nhận xét