2013年8月29日星期四

Android

 

Introduction

 

dialog is also essential for the application of a component in Android is no exception, a dialog box prompts for some important information that require additional user interaction or some of the content helpful. This blog will explain the use of the dialog box under Android, In this blog, you will learn some general properties dialog settings dialog boxes and all kinds of use, and will provide small to show all effects Demo .

 

Dialog

 

Dialog , dialog, a dialog box is a small window, and will not fill over the entire screen display mode usually requires the user to take action in order to proceed to the rest of the operation.

 

Android provides a rich dialog support, which provides the following four commonly used dialog:

 
      
  • AlertDialog: Warning dialog box, the most widely used one of the most feature rich dialog.
  •   
  • ProgressDialog: progress bar dialog box, except for the progress bar for a simple package.
  •   
  • DatePickerDialog: Date dialog box.
  •   
  • TimePickerDialog: Time dialog box.
  •  
 

all dialogs are directly or indirectly inherit from the Dialog class, while AlterDialog inherit directly from Dialog, several other classes are inherited from AlterDialog.

 

 

AlertDialog

 

AlertDialog inherited from the Dialog class, built for the Android AlterDialog, it can contain a title , a message or a select list content up to three buttons. Created AlterDialog recommend using one of its internal class AlterDialog.Builder created. Using Builder object, you can set AlterDialog various properties, and finally through Builder.create () you can get AlterDialog object, if only need to show this AlterDialog, can generally be used directly Builder.show () method, which returns an object AlterDialog , and displays it.

 

if only need to prompt a message to the user, it can be used directly to set some properties AlterDialog message that relates to methods are:

 
      
  • AlterDialog create (): according to the set of attributes, creating a AlterDialog.
  •   
  • AlterDialog show (): according to the set of attributes, creating a AlterDialog, and displayed on the screen.
  •   
  • AlterDialog.Builder setTitle (): set the title.
  •   
  • AlterDialog.Builder setIcon (): set the title of the icon.
  •   
  • AlterDialog.Builder setMessage (): set the title of the content.
  •   
  • AlterDialog.Builder setCancelable (): Set whether modal, generally set to false, modal, the user must take action in order to continue with the rest of the operation.
  •  
 

Tips: AlterDialog.Builder many ways to set properties returned are the AlterDialog.Builder object, so you can write code using the chain, which is more convenient.

 

When a dialog box called show () method, the display on the screen, if you need to remove it, you can use DialogInterface interface declares two methods, cancel () and dismiss () to take or remove the dialog box, which The role of the two methods is the same, but the recommended dismiss (). Dialog and AlterDialog have achieved DialogInterface interface, so long as the dialog, can use these methods to eliminate the dialog box.

 

following a simple Demo, take a look at how the message AlterDialog of:

 

Example:

 
  
      
  
 1         btnGeneral.setOnClickListener(new View.OnClickListener() { 
2
3 @Override
4 public void onClick(View v) {
5 // TODO Auto-generated method stub
6 AlertDialog.Builder builder = new AlertDialog.Builder(
7 MainActivity.this);
8 builder.setTitle("提示");
9 builder.setMessage("这是一个普通的对话框!");
10 builder.setIcon(R.drawable.ic_launcher);
11 builder.setCancelable(false);
12 builder.setPositiveButton("知道了!", new OnClickListener() {
13 @Override
14 public void onClick(DialogInterface dialog, int which) {
15 dialog.cancel();
16 }
17 });
18 builder.create().show();
19 }
20 });
  
      
 
 

showing the effect of:

 

 

 

AlterDialog button

 

AlterDialog built three buttons can be used directly setXxxButton () method to set, for the average dialog box with three buttons are basically good enough, the following is the signature of these three methods:

 
      
  • AlterDialog.Builder setPositiveButton (CharSquence text, DialogInterFace.OnClickListener): an active button, generally used for "OK" or "continue," and other operations.
  •   
  • AlterDialog.Builder setNegativeButton (CharSquence text, DialogInterFace.OnClickListener): a negative buttons are generally used for the "Cancel" operation.
  •   
  • AlterDialog.Builder setNeutralButton (CharSquence text, DialogInterFace.OnClickListener): a more neutral button, generally used for "ignore", "Remind Me Later" and other operations.
  •  
 

described above DialogInterface interface also provides a range of incident response, these three buttons are required to pass a DialogInterFace.OnClickListener interface object, to achieve its click event trigger, need to implement this interface in an onClick (DialogInterface dialog , int which), dialog box for the current trigger event object interface, can be directly cast to AlterDialog operate; which button to click on the identifier is an integer data, for these three buttons, each button using a different data type int identified: Positive (-1), Negative (-2), Neutral (-3).

 

In addition to dedicated button clicks achieved DialogInterFace.OnClickListener incidents, DialogInterface also provides some other events for the Dialog object response to these events just Dialog statement cycle response of each state, one can understand, on the not explained in detail, the following is a description of these events:

 
      
  • interface DialogInterface.OnCancelListener: When the dialog box call cancel () method when triggered.
  •   
  • interface DialogInterface.OnDismissListener: When the dialog box calls dismiss () method when triggered.
  •   
  • interface DialogInterface.OnShowListener: When the dialog box call show () method when triggered.
  •   
  • interface DialogInterface.OnMultiChoiceListener: When using the multiple-selection list box, and select when triggered.
  •  
 

Example:

 
  
      
  
 1         btnButtons.setOnClickListener(new View.OnClickListener() { 
2
3 @Override
4 public void onClick(View v) {
5 // TODO Auto-generated method stub
6 AlertDialog.Builder builder = new AlertDialog.Builder(
7 MainActivity.this);
8 builder.setTitle("提示");
9 builder.setMessage("这是一个多按钮普通的对话框!");
10 builder.setIcon(R.drawable.ic_launcher);
11 builder.setPositiveButton("确定", new OnClickListener() {
12
13 @Override
14 public void onClick(DialogInterface dialog, int which) {
15 Toast.makeText(MainActivity.this, "确定被点击",
16 Toast.LENGTH_SHORT).show();
17 dialog.dismiss();
18 }
19 });
20 builder.setNegativeButton("否定", new OnClickListener() {
21
22 @Override
23 public void onClick(DialogInterface dialog, int which) {
24 // TODO Auto-generated method stub
25 Toast.makeText(MainActivity.this, "否定被点击",
26 Toast.LENGTH_SHORT).show();
27 dialog.dismiss();
28 }
29 });
30 builder.setNeutralButton("忽略", new OnClickListener() {
31
32 @Override
33 public void onClick(DialogInterface dialog, int which) {
34 // TODO Auto-generated method stub
35 Toast.makeText(MainActivity.this, "忽略被点击",
36 Toast.LENGTH_SHORT).show();
37 dialog.cancel();
38 }
39 });
40 builder.show();
41 }
42 });
  
      
 
 

showing the effect of:

 

 

 

AlterDialog list form

 

AlterDialog In addition to showcasing some of the tips, you can also demonstrate a form of a list, you need to Builder.setItems (CharSequence [] items, DialogInterface.OnClickListener listener) method to set, it needs to pass an array of type CharSequenece to Data binding list, it also need to pass a DialogInterface.OnClickListener interface in response to clicking the list item, and this interface onClick method which parameters click the trigger for the current item items in subscript.

 

Example:

 
  
      
  
 1         btnListView.setOnClickListener(new View.OnClickListener() { 
2
3 @Override
4 public void onClick(View v) {
5 // TODO Auto-generated method stub
6 AlertDialog.Builder builder = new AlertDialog.Builder(
7 MainActivity.this);
8 builder.setTitle("请选择城市");
9 //items使用全局的finalCharSequenece数组声明
10 builder.setItems(items, new OnClickListener() {
11 @Override
12 public void onClick(DialogInterface dialog, int which) {
13 // TODO Auto-generated method stub
14 String select_item = items[which].toString();
15 Toast.makeText(MainActivity.this,
16 "选择了---》" + select_item, Toast.LENGTH_SHORT)
17 .show();
18 }
19 });
20 builder.show();
21 }
22 });
  
      
 
 

results show:

 

 

AlterDialog radio list

 

AlterDialog radio is also possible to use a list style, using Builder.setSingleChoiceItems (CharSequenece [] items, int checkedItem, DialogInterface.OnClickListener listener), this method has several overloads, mainly in response to different data sources , items for the list item array, checkedItem initial option, listener response to a click event. Sometimes does not necessarily need to close the dialog box after selected, you can set two buttons, used to determine the selection.

 

Example:

 
  
      
  
 1         btnListViewSingle.setOnClickListener(new View.OnClickListener() { 
2 @Override
3 public void onClick(View v) {
4 // TODO Auto-generated method stub
5 AlertDialog.Builder builder = new AlertDialog.Builder(
6 MainActivity.this);
7 builder.setTitle("请选择一下城市");
8 builder.setSingleChoiceItems(items, 1, new OnClickListener() {
9
10 @Override
11 public void onClick(DialogInterface dialog, int which) {
12 // TODO Auto-generated method stub
13 String select_item = items[which].toString();
14 Toast.makeText(MainActivity.this,
15 "选择了--->>" + select_item, Toast.LENGTH_SHORT)
16 .show();
17 }
18 });
19 builder.setPositiveButton("确定", new OnClickListener() {
20 @Override
21 public void onClick(DialogInterface dialog, int which) {
22 dialog.dismiss();
23 }
24 });
25 builder.show();
26 }
27 });
  
      
 
 

results show:

 

 

 

AlterDialog multiple-selection list

 

AlterDialog In addition to single-selection list, as well as multiple-choice list. You can use Builder.setMultiChoiceItems (CharSequence [] items, boolean [] checkedItems, DialogInterface.OnMultiChoiceClickListener listener), this method also has a variety of overloads for this method, items in an array as the data source; checkedItems is the default option, because is a multi-selection list, so if you need to set all settings, if not checked by default, returns Null; listener for multiple options Click triggering event.

 

Example:

 
  
      
  
 1         btnListViewMulti.setOnClickListener(new View.OnClickListener() { 
2
3 @Override
4 public void onClick(View v) {
5 // TODO Auto-generated method stub
6 AlertDialog.Builder builder = new AlertDialog.Builder(
7 MainActivity.this);
8 builder.setTitle("请选择城市");
9 builder.setMultiChoiceItems(items, new boolean[] { true, false,
10 true }, new OnMultiChoiceClickListener() {
11 @Override
12 public void onClick(DialogInterface dialog, int which,
13 boolean isChecked) {
14 // TODO Auto-generated method stub
15 String select_item = items[which].toString();
16 Toast.makeText(MainActivity.this,
17 "选择了--->>" + select_item, Toast.LENGTH_SHORT)
18 .show();
19 }
20 });
21 builder.setPositiveButton("确定", new OnClickListener() {
22 @Override
23 public void onClick(DialogInterface dialog, int which) {
24 dialog.dismiss();
25 }
26 });
27 builder.show();
28 }
29 });
  
      
 
 

results show:

 

 

 

AlertDialog custom style

 

Sometimes, Android comes with some style settings have been unable to meet demand, then you can use custom styles, custom an XML layout file, with the contents of this document as the style of AlertDialog display on the screen, so you can a flexible customization dialog. For custom XML file, you can use LayoutInflater.from (Context). Inflate (int, ViewGroup) way its dynamic loading, and then use Builder.setView (View) to view the loaded object associated with the Builder, the last regular show () can be.

 

layout code:

 
  
      
  
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
2 xmlns:tools="http://schemas.android.com/tools"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical"
6 android:paddingBottom="@dimen/activity_vertical_margin"
7 android:paddingLeft="@dimen/activity_horizontal_margin"
8 android:paddingRight="@dimen/activity_horizontal_margin"
9 android:paddingTop="@dimen/activity_vertical_margin"
10 tools:context=".MainActivity" >
11
12 <Button
13 android:id="@+id/btnGeneral"
14 android:layout_width="wrap_content"
15 android:layout_height="wrap_content"
16 android:text="普通对话框" />
17
18 <Button
19 android:id="@+id/btnButtons"
20 android:layout_width="wrap_content"
21 android:layout_height="wrap_content"
22 android:text="多按钮的普通对话框" />
23
24 <Button
25 android:id="@+id/btnListView"
26 android:layout_width="wrap_content"
27 android:layout_height="wrap_content"
28 android:text="列表选择对话框" />
29
30 <Button
31 android:id="@+id/btnListViewSingle"
32 android:layout_width="wrap_content"
33 android:layout_height="wrap_content"
34 android:text="单选列表选择对话框" />
35
36 <Button
37 android:id="@+id/btnListViewMulti"
38 android:layout_width="wrap_content"
39 android:layout_height="wrap_content"
40 android:text="多选列表选择对话框" />
41
42 <Button
43 android:id="@+id/btnProgressDialog"
44 android:layout_width="wrap_content"
45 android:layout_height="wrap_content"
46 android:text="滚动等待对话框" />
47
48 <Button
49 android:id="@+id/btnProgressDialogH"
50 android:layout_width="wrap_content"
51 android:layout_height="wrap_content"
52 android:text="进度条对话框" />
53 <Button
54 android:id="@+id/btnCustomDialog"
55 android:layout_width="wrap_content"
56 android:layout_height="wrap_content"
57 android:text="自定义对话框" />
58 </LinearLayout>
  
      
 
 

implementation code:

 
  
      
  
 1         btnCustomDialog.setOnClickListener(new View.OnClickListener() { 
2
3 @Override
4 public void onClick(View v) {
5 AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
6 View view=LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_signin,null);
7 Button btn=(Button)view.findViewById(R.id.btnCustom);
8 btn.setOnClickListener(new View.OnClickListener() {
9
10 @Override
11 public void onClick(View v) {
12 // TODO Auto-generated method stub
13 alertDialog.dismiss();
14 Toast.makeText(MainActivity.this, "表单填写完成",
15 Toast.LENGTH_SHORT).show();
16 }
17 });
18 builder.setView(view);
19 alertDialog=builder.show();
20 }
21 });
  
      
 
 

results show:

 

 

 

ProgressDialog

 

Sometimes, just need to prompt the user to wait, for example, in the implementation of the time-consuming operation, etc., can be used to display a progress dialog progress information that prompts the user to wait, this time you can use ProgressDialog. ProgressDialog is used most of them can see ProgressBar, is actually a package of the ProgressBar dialog.

 

ProgressDialog There are two display methods, one is a rolling ring icon , you can display a title and some text content to wait for the dialog box; another one is with a progress bar scale, and general usage of the progress bar. Both styles through ProgressDialog.setProgressStyle (int style) settings, you can set ProgressDialog two constants: STYLE_HORIZONTAL: Scale rolling; STYLE_SPINNER: icon scrolling, the default option.

 

For scrolling icons can be used in two ways, one is the conventional call the constructor, and then set the corresponding property; Another is the direct use ProgressDialog static method show (), returns a ProgressDialog object directly, and call the show () method.

 

Example:

 
  
      
  
 1         btnProgressDialog.setOnClickListener(new View.OnClickListener() { 
2
3 @Override
4 public void onClick(View v) {
5 // 第一种方法,使用ProgressDialog构造函数
6 progressDialog = new ProgressDialog(MainActivity.this);
7 progressDialog.setIcon(R.drawable.ic_launcher);
8 progressDialog.setTitle("等待");
9 progressDialog.setMessage("正在加载....");
10 progressDialog.show();
11 //第二种方法,使用静态的show方法
12 //progressDialog=ProgressDialog.show(MainActivity.this, "等待", "正在加载....", false, false);
13 new Thread(new Runnable() {
14
15 @Override
16 public void run() {
17 try {
18 Thread.sleep(5000);
19 } catch (Exception e) {
20 e.printStackTrace();
21 }
22 finally{
23 progressDialog.dismiss();
24 }
25 }
26 }).start();
27 }
28 });
  
      
 
 

results show:

 

 

For a graduated ProgressDialog, apart from the AlertDialog in inherited property, there are some necessary attributes need to be set, the following method has the getter method:

 
      
  • setMax (int max): maximum scale.
  •   
  • setProgress (int value): First progress.
  •   
  • setSecondaryProgress (int value): Second progress.
  •  
 

Example:

 
  
      
  
 1         btnProgressDialog.setOnClickListener(new View.OnClickListener() { 
2
3 @Override
4 public void onClick(View v) {
5 // 第一种方法,使用ProgressDialog构造函数
6 progressDialog = new ProgressDialog(MainActivity.this);
7 progressDialog.setIcon(R.drawable.ic_launcher);
8 progressDialog.setTitle("等待");
9 progressDialog.setMessage("正在加载....");
10 progressDialog.show();
11 //第二种方法,使用静态的show方法
12 //progressDialog=ProgressDialog.show(MainActivity.this, "等待", "正在加载....", false, false);
13 new Thread(new Runnable() {
14
15 @Override
16 public void run() {
17 try {
18 Thread.sleep(5000);
19 } catch (Exception e) {
20 e.printStackTrace();
21 }
22 finally{
23 progressDialog.dismiss();
24 }
25 }
26 }).start();
27 }
28 });
  
      
 
 

results show:

 

 

source download

 

Summary

 

above describes the common dialog boxes on the content, DatePickerDialog and TimePickerDialog In addition there is an introduction to the blog, you can see: Android - UI's DatePicker, TimePicker ... . The latest official document from the learned, it is recommended to use FragmentDialog to operate Dialog, so easy to manage content on Fragment, no introduction, later introduced Fragment say that after using FragmentDialog how to create a dialog. In the source code there is a FragmentDialog simple example, are interested can download them to see.

 

Please support the original, respect for the original, reproduced please indicate the source. Thank you.

没有评论:

发表评论