مترجم: حبیب الله علیخانی
منبع:راسخون




 

نمایش پنجره ی محاوره یا دیالوگ(dialog)

مواقعی وجود دارد که شما نیاز به نمایش یک پنجره برای دریافت تایید از کاربر دارید. در این موارد، برای نمایش پنجره ی دیالوگ می توانید متد محافظت شده ی(protected) onCreateDialog() را در کلاس اکتیویتی پایه بازنویسی(override) کنید. در زیر نشان می دهد که چه کاری انجام دهید.
نمایش یک پنجره ی دیالوگ با استفاده از یک اکتیویتی
1- یک پروژه ی اندرویدی در ایکلیپس به نام Dialog ایجاد کنید.
2- دستورات زیر که بولد شده(خط 12 تا 17)، را در فایل main.xml آن اضافه کنید:
1- <?xml version="1.0" encoding="utf-8"?>
2- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3- android:layout_width="fill_parent"
4- android:layout_height="fill_parent"
5- android:orientation="vertical" >
6-
7- <TextView
8- android:layout_width="fill_parent"
9- android:layout_height="wrap_content"
10- android:text="@string/hello" />
11-
12- <Button
13- android:id=”@+id/btn_dialog”
14- android:layout_width=”fill_parent”
15- android:layout_height=”wrap_content”
16- android:text=”Click to display a dialog”
17- android:onClick=”onClick” />
18-
19- </LinearLayout>
3- دستورات زیر را در فایل DialogActivity.java اضافه کنید:
1- package net.rasekhoon.raeesiyan;
2-
3- import android.app.Activity;
4- import android.app.AlertDialog;
5- import android.app.Dialog;
6- import android.content.DialogInterface;
7- import android.os.Bundle;
8- import android.view.View;
9- import android.widget.Toast;
10-
11-
12- public class DialogActivity extends Activity {
13-
14- CharSequence[] items = { "Rasekhoon", "Raeesiyan", "Alikhani" };
15- boolean[] itemsChecked = new boolean[items.length];
16-
17-
18- /** Called when the activity is first created. */
19- @Override
20- public void onCreate(Bundle savedInstanceState) {
21- super.onCreate(savedInstanceState);
22- setContentView(R.layout.main);
23- }
24-
25-
26- public void onClick(View v) {
27- showDialog(0);
28- }
29-
30-
31- @Override
32- protected Dialog onCreateDialog(int id) {
33- switch (id) {
34- case 0:
35- return new AlertDialog.Builder(this)
36- .setIcon(R.drawable.ic_launcher)
37- .setTitle("This is a dialog with some simple text...")
38- .setPositiveButton("OK",
39- new DialogInterface.OnClickListener() {
40-
41- @Override
42- public void onClick(DialogInterface dialog, int whichButton)
43- {
44- Toast.makeText(getBaseContext(),
45- "OK clicked!", Toast.LENGTH_SHORT).show();
46- }
47- }
48- )
49- .setNegativeButton("Cancel",
50- new DialogInterface.OnClickListener() {
51-
52- @Override
53- public void onClick(DialogInterface dialog, int whichButton)
54- {
55- Toast.makeText(getBaseContext(),
56- "Cancel clicked!", Toast.LENGTH_SHORT).show();
57- }
58- }
59- )
60- .setMultiChoiceItems(items, itemsChecked,
61- new DialogInterface.OnMultiChoiceClickListener() {
62-
63- @Override
64- public void onClick(DialogInterface dialog,
65- int which, boolean isChecked) {
66- Toast.makeText(getBaseContext(),
67- items[which] + (isChecked ? " checked!" : " unchecked!"),
68- Toast.LENGTH_SHORT).show();
69- }
70- }
71- ).create();
72- }
73- return null;
74- }
75- }
4- سپس آن را اجرا کنید :
دکمه ی click to display a dialog را کلیک کنید:
سپس می بینید:
انتخاب چک باکس های مختلف از کلاس Toast استفاده می کند تا آیتم را تیک بزند تا تیک آن را بردارد. برای خروج از این دیالوگ باکس، دکمه ی ok یا cancel را کلیک کنید.
چگونه کار می کند:
برای نمایش این دیالوگ، متد onCreateDialog() را در کلاس Activity پیاده سازی می کنیم:
@Override
protected Dialog onCreateDialog(int id) {
//...
}
زمانیکه شما متد showDialog() را فراخوانی می کنید، این متد فراخوانی می شود:
public void onClick(View v) {
showDialog(0);
}
متد onCreateDialog() برای ایجاد دیالوگها که توسط اکتیویتی اداره می شود، یک callback است. زمانیکه که شما متد showDialog() را فراخوانی می کنید، این callback احظار می شود. متد showDialog() یک آرگومان عدد صحیح شناسایی دیالوگ خاص برای نمایش پذیرد. در این مورد، ما ازدستور switch برای شناسایی انواع مختلف پنجره در ایجاد استفاده می کنیم، اگر چه این مثال فقط یک نوع از دیالوگ را ایجاد می کند. در جلوتر از این مثال برای ایجاد انواع مختلف دیالوگ استفاده می کنید.
برای ایجاد یک محاوره، شما Builder سازنده کلاس AlertDialog استفاده کنید. شما خواص مختلف، مانند آیکون، عنوان، و دکمه ها، و همچنین چک باکس قرار دهید:
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
return new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("This is a dialog with some simple text...")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int whichButton)
{
Toast.makeText(getBaseContext(),
"OK clicked!", Toast.LENGTH_SHORT).show();
}
}
)
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton)
{
Toast.makeText(getBaseContext(),
"Cancel clicked!", Toast.LENGTH_SHORT).show();
}
}
)
.setMultiChoiceItems(items, itemsChecked,
new DialogInterface.OnMultiChoiceClickListener() { @Override
public void onClick(DialogInterface dialog,
int which, boolean isChecked) {
Toast.makeText(getBaseContext(),
items[which] + (isChecked ? " checked!" : " unchecked!"),
Toast.LENGTH_SHORT).show();
}
}
).create();
}
return null;
}
}
در جلوتر شما دو دکمه ی OK و Cancel قرار دادید که به ترتیب از متد های setPositiveButton() و setNegativeButton() استفاده می کند. همچنین شما لیستی از چک باکس ها را برای کاربر با استفاده از متد setMultiChoiceItems() قرار می دهید. برای متد setMultiChoiceItems() ، شما دو آرایه به آن پاس مس دهید: یکی از آنها برای لیست آیتم برای نمایش استفاده می شود و دیگری مقدار هرکدام از آنها را شامل می شود که اگر تیک خورده شود نمایش داده شود. وقتی هر کدام از آیتم ها تیک خورده شود، شما از کلاس Toast برای نمایش پیام آیتمی که تیک خورده استفاده می کنید:
1- package net.rasekhoon.raeesiyan;
2-
3- import android.app.Activity;
4- import android.app.AlertDialog;
5- import android.app.AlertDialog.Builder;
6- import android.app.Dialog;
7- import android.content.DialogInterface;
8- import android.os.Bundle;
9- import android.view.View;
10- import android.widget.Toast;
11-
12-
13- public class DialogActivity extends Activity {
14-
15- CharSequence[] items = { "Rasekhoon", "Raeesiyan", "Alikhani" };
16- boolean[] itemsChecked = new boolean[items.length];
17-
18-
19- /** Called when the activity is first created. */
20- @Override
21- public void onCreate(Bundle savedInstanceState) {
22- super.onCreate(savedInstanceState);
23- setContentView(R.layout.main);
24- }
25-
26-
27- public void onClick(View v) {
28- showDialog(0);
29- }
30-
31-
32- @Override
33- protected Dialog onCreateDialog(int id) {
34- switch (id) {
35- case 0:
36- Builder builder = new AlertDialog.Builder(this);
37- builder.setIcon(R.drawable.ic_launcher);
38- builder.setTitle("This is a dialog with some simple text...");
39- builder.setPositiveButton("OK",
40- new DialogInterface.OnClickListener() {
41-
42- @Override
43- public void onClick(DialogInterface dialog, int whichButton) {
44- Toast.makeText(getBaseContext(),
45- "OK clicked!", Toast.LENGTH_SHORT).show();
46- }
47- }
48- );
49- builder.setNegativeButton("Cancel",
50- new DialogInterface.OnClickListener() {
51-
52- @Override
53- public void onClick(DialogInterface dialog, int whichButton) {
54- Toast.makeText(getBaseContext(),
55- "Cancel clicked!", Toast.LENGTH_SHORT).show();
56- }
57- }
58- );
59- builder.setMultiChoiceItems(items, itemsChecked,
60- new DialogInterface.OnMultiChoiceClickListener() {
61-
62- @Override
63- public void onClick(DialogInterface dialog,
64- int which, boolean isChecked) {
65- Toast.makeText(getBaseContext(),
66- items[which] + (isChecked ? " checked!" : " unchecked!"),
67- Toast.LENGTH_SHORT).show();
68- }
69- }
70- );
71- return builder.create();
72- }
73- return null;
74- }
75- }

مفهوم آبجکت

در اندروید، شما معمولا با کلاس Context و نمونه های(Instance) آن برخورد می کنید. نمونه های کلاس Context اغلب برای ارئه کردن مرجع استفاده می شود. برای مثال، در نمونه کد زیر، اولین پارامتر کلاس Toast در یک آبجکت Context می گیرد:
.setPositiveButton(“OK”,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
Toast.makeText(getBaseContext(),
“OK clicked!”, Toast.LENGTH_SHORT).show();
}
}
با این وجود، به دلیل اینکه کلاس Toast() به طور مستقیم در اکتیویتی() استفاده نشده، باید یک نمونه از کلاس Context با استفاده از متد getBasecontext() برگرداند.
همچنین زمانیکه در یک اکتیویتی یک view به طور دینامیک ایجاد می کنید، با کلاس Context برخورد می کنید. به عنوان مثال، زمانیکه می خواهید به طور دینامیک از طریق کد یک TextView بسازید. به این صورت انجام دهید، که یک نمونه از کلاس TextView بسازید. مانند زیر:
TextView tv = new TextView(this);
سازنده ی برای کلاس TextView یک آبجکت Context می گیرد و به دلیل اینکه کلاس Activity یک زیر کلاس Context است، می توانید از کلمه ی کلیدی this برای ارائه دادن آبجکت Context استفاده کنید.
نمایش یک دیالوگ پردازش(progress dialog)
یکی از مشخصه های وسایل اندرویدی دیالوگ "please wait" است که به طور معمول شما آن را زمانی که یک وظیفه ی زمانبری را اجرا می کند، می بینید. برای مثال، قبل از اینکه یک اپلیکیشن به یک سروری login شود تا کاربر از آن استفاده کند، یا زمانی که می خواهد یک محاسبه ای انجام دهد قبل از نمایش نتیجه ی آن، این را می بینید. در یک چنین مواردی، نمایش یک دیالوگ مفید است که آن را به عنوان دیالوگ پردازش می شناسند، که کاربر را در حلقه ای نگه می دارد.
در زیر سعی می می کنیم تا کار یک چنین دیالوگ را تشریح کنیم.

نمایش دیالوگ پردازش(please wait)

1- از همان پروژه ی بالا استفاده می کنیم و دستورات زیر که بولد شده(bold) از خط 17 تا 23، را در فایل main.xml اضافه می کنیم:
1- <?xml version="1.0" encoding="utf-8"?>
2- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3- android:layout_width="fill_parent"
4- android:layout_height="fill_parent"
5- android:orientation="vertical" >
6- <TextView
7- android:layout_width="fill_parent"
8- android:layout_height="wrap_content"
9- android:text="@string/hello" />
10- <Button
11- android:id="@+id/btn_dialog"
12- android:layout_width="fill_parent"
13- android:layout_height="wrap_content"
14- android:text="Click to display a dialog"
15- android:onClick="onClick" />
16-
17- <Button
18- android:id="@+id/btn_dialog2"
19- android:layout_width="fill_parent"
20- android:layout_height="wrap_content"
21- android:text="Click to display a progress dialog"
22- android:onClick="onClick2" />
23-
24- </LinearLayout>
در اجرای این اپلیکیشن، این UI به شکل زیر است:
2- دستورات خط 29 تا 48 را در فایل Dialog.java اضافه می کنیم:
1- package net.rasekhoon.raeesiyan;
2-
3- import android.app.Activity;
4- import android.app.Dialog;
5- import android.app.ProgressDialog;
6- import android.os.Bundle;
7- import android.view.View;
8-
9-
10- public class DialogActivity extends Activity {
11-
12- CharSequence[] items = { "Google", "Apple", "Microsoft" };
13- boolean[] itemsChecked = new boolean[items.length];
14-
15-
16- /** Called when the activity is first created. */
17- @Override
18- public void onCreate(Bundle savedInstanceState) {
19- super.onCreate(savedInstanceState);
20- setContentView(R.layout.main);
21- }
22-
23-
24- public void onClick(View v) {
25- showDialog(0);
26- }
27-
28-
29- public void onClick2(View v) {
30- //---show the dialog---
31- final ProgressDialog dialog = ProgressDialog.show(
32- this, "Doing something", "Please wait...", true);
33- new Thread(new Runnable() {
34-
35- @Override
36- public void run() {
37- try {
38- //---simulate doing something lengthy---
39- Thread.sleep(5000);
40- //---dismiss the dialog---
41- dialog.dismiss();
42- }
43- catch (InterruptedException e) {
44- e.printStackTrace();
45- }
46- }
47- }).start();
48- }
49-
50-
51- @Override
52- protected Dialog onCreateDialog(int id) {
53- return null;
54- }
55- }
3- سپس آن را اجرا می کنیم. پس از اجرا، دکمه ی دومی را کلیک می کنیم تا دیالوگ پردازش را اجرا کند:
چگونه کار می کند:
به طور کلی، برای ایجاد یک دیالوگ پردازش، شما یک نمونه از کلاس progressDialog ایجاد کنید و متد show() را فراخوانی کنید:
//---show the dialog---
final ProgressDialog dialog = ProgressDialog.show(
this, “Doing something”, “Please wait...”, true);
این دیالوگ پردازش را نمایش می دهد که شما آن را می بینید. این یک دیالوگ مقید است، چون تا زمانیکه از آن خارج نشوید، UI بلوکه می شود. برای اجرای یک پردازش زمانبر در پس زمینه، شما با استفاده از یک بلوک Runnable یک نخ ایجاد می کنید(در بخش های آینده اطلاعات بیشتر درباره ی نخ ها را ارائه خواهیم داد). کدی که شما درون متد run() استفاده می کنید، در یک نخ جداگانه اجرا خواهد شد و در این مورد شما با استفاده از متد sleep() به مدت پنج ثانیه تاخیر ایجاد کردید:
new Thread(new Runnable(){
public void run(){
try {
//---simulate doing something lengthy---
Thread.sleep(5000);
//---dismiss the dialog---
dialog.dismiss();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
بعد از سپری شدن پنج ثانیه، متد dismiss() را فراخوانی کنید.
نمایش یک دیالوگ پردازش پیچیده تر
در کنار دیالوگ باکسی که شما در بالا ایجاد کرده اید، می توانید یک دیالوگ که پردازش یک عملیات را نشان می دهد را ایجاد کنید. مانند وضعیت دانلود.
در زیر نشان می دهیم که چگونه یک دیالوگ پردازش مخصوص کار می کند.
نمایش پردازش یک عملیات
1- از همان پروژه ی قبلی استفاده می کنیم و در کد های فایل main.xml آن کد های خط 20 تا 25 زیر را به آن اضافه می کنیم و کل کدهای این فایل باید به صورت زیر باشد:
1- <?xml version="1.0" encoding="utf-8"?>
2- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3- android:layout_width="fill_parent"
4- android:layout_height="fill_parent"
5- android:orientation="vertical" >
6-
7- <Button
8- android:id="@+id/btn_dialog"
9- android:layout_width="fill_parent"
10- android:layout_height="wrap_content"
11- android:text="Click to display a dialog"
12- android:onClick="onClick" />
13- <Button
14- android:id="@+id/btn_dialog2"
15- android:layout_width="fill_parent"
16- android:layout_height="wrap_content"
17- android:text="Click to display a progress dialog"
18- android:onClick="onClick2" />
19-
20- <Button
21- android:id="@+id/btn_dialog3"
22- android:layout_width="fill_parent"
23- android:layout_height="wrap_content"
24- android:text="Click to display a detailed progress dialog"
25- android:onClick="onClick3" />
26-
27-
28- </LinearLayout>
که این کد ها پس از اجرای این پروژه، UI زیر را ایجاد می کند:
2- در همان پروژه وارد فایل DialogActiviyi.java می شویم و دستورات بولد شده ی زیر (یعنی دستور خط 19 و دستورات خط 57 تا 78 و دستورات خط 122 تا 149) را به دستورات آن اضافه می کنیم و کل دستورات این فایل باید به صورت زیر باشد:
1- package net.rasekhoon.raeesiyan;
2-
3- import android.app.Activity;
4- import android.app.AlertDialog;
5- import android.app.AlertDialog.Builder;
6- import android.app.Dialog;
7- import android.app.ProgressDialog;
8- import android.content.DialogInterface;
9- import android.os.Bundle;
10- import android.view.View;
11- import android.widget.Toast;
12-
13-
14- public class DialogActivity extends Activity {
15-
16- CharSequence[] items = { "Rasekhoon", "Raeesiyan", "Alikhani" };
17- boolean[] itemsChecked = new boolean[items.length];
18-
19- ProgressDialog progressDialog;
20-
21-
22- /** Called when the activity is first created. */
23- @Override
24- public void onCreate(Bundle savedInstanceState) {
25- super.onCreate(savedInstanceState);
26- setContentView(R.layout.main);
27- }
28-
29-
30- public void onClick(View v) {
31- showDialog(0);
32- }
33-
34-
35- public void onClick2(View v) {
36- //---show the dialog---
37- final ProgressDialog dialog = ProgressDialog.show(
38- this, "Doing something", "Please wait...", true);
39- new Thread(new Runnable() {
40-
41- @Override
42- public void run() {
43- try {
44- //---simulate doing something lengthy---
45- Thread.sleep(5000);
46- //---dismiss the dialog---
47- dialog.dismiss();
48- }
49- catch (InterruptedException e) {
50- e.printStackTrace();
51- }
52- }
53- }).start();
54- }
55-
56-
57- public void onClick3(View v) {
58- showDialog(1);
59- progressDialog.setProgress(0);
60- new Thread(new Runnable() {
61-
62- @Override
63- public void run() {
64- for (int i = 1; i <= 15; i++) {
65- try {
66- //---simulate doing something lengthy---
67- Thread.sleep(1000);
68- //---update the dialog---
69- progressDialog.incrementProgressBy((int) (100 / 15));
70- }
71- catch (InterruptedException e) {
72- e.printStackTrace();
73- }
74- }
75- progressDialog.dismiss();
76- }
77- }).start();
78- }
79-
80-
81- @Override
82- protected Dialog onCreateDialog(int id) {
83- switch (id) {
84- case 0:
85- Builder builder = new AlertDialog.Builder(this);
86- builder.setIcon(R.drawable.ic_launcher);
87- builder.setTitle("This is a dialog with some simple text...");
88- builder.setPositiveButton("OK",
89- new DialogInterface.OnClickListener() {
90-
91- @Override
92- public void onClick(DialogInterface dialog, int whichButton) {
93- Toast.makeText(getBaseContext(),
94- "OK clicked!", Toast.LENGTH_SHORT).show();
95- }
96- }
97- );
98- builder.setNegativeButton("Cancel",
99- new DialogInterface.OnClickListener() {
100-
101- @Override
102- public void onClick(DialogInterface dialog, int whichButton) {
103- Toast.makeText(getBaseContext(),
104- "Cancel clicked!", Toast.LENGTH_SHORT).show();
105- }
106- }
107- );
108- builder.setMultiChoiceItems(items, itemsChecked,
109- new DialogInterface.OnMultiChoiceClickListener() {
110-
111- @Override
112- public void onClick(DialogInterface dialog,
113- int which, boolean isChecked) {
114- Toast.makeText(getBaseContext(),
115- items[which] + (isChecked ? " checked!" : " unchecked!"),
116- Toast.LENGTH_SHORT).show();
117- }
118- }
119- );
120- return builder.create();
121-
122- case 1:
123- progressDialog = new ProgressDialog(this);
124- progressDialog.setIcon(R.drawable.ic_launcher);
125- progressDialog.setTitle("Downloading files...");
126- progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
127- progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
128- new DialogInterface.OnClickListener() {
129-
130- @Override
131- public void onClick(DialogInterface dialog,
132- int whichButton)
133- {
134- Toast.makeText(getBaseContext(),
135- "OK clicked!", Toast.LENGTH_SHORT).show();
136- }
137- });
138- progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
139- new DialogInterface.OnClickListener() {
140-
141- @Override
142- public void onClick(DialogInterface dialog,
143- int whichButton)
144- {
145- Toast.makeText(getBaseContext(),
146- "Cancel clicked!", Toast.LENGTH_SHORT).show();
147- }
148- });
149- return progressDialog;
150- }
151- return null;
152- }
153- }
سپس آن را اجرا می کنیم. دکمه ی سوم را کلیک می کنیم تا دیالوگ پردازش نمایش داده شود:
چگونه کار می کند:
برای ایجاد یک دیالوگ که پردازش یک عملیات را نشان دهد، ابتدا باید یک نمونه از کلاس Progressdialog ایجاد کنید و مشخصه های مختلف آن را مقدار دهی کنید مانند آیکن، عنوان و استایل. این دستورات در زیر آمده است:
progressDialog = new ProgressDialog(this);
progressDialog.setIcon(R.drawable.ic_launcher);
progressDialog.setTitle("Downloading files...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
شما می توانید با دستورات زیر دو دکمه را که می خواهید در دیالوگ پردازش نشان داده شود، قرار دهید:
progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int whichButton)
{
Toast.makeText(getBaseContext(),
"OK clicked!", Toast.LENGTH_SHORT).show();
}
});
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {

 

@Override
public void onClick(DialogInterface dialog,
int whichButton)
{
Toast.makeText(getBaseContext(),
"Cancel clicked!", Toast.LENGTH_SHORT).show();
}
}
برای نمایش وضعیت پردازش در دیالوگ پردازش ، شما می توانید از یک آبجکت Thread برای اجرای یک بلاک Runnable از کد استفاده کنید:
progressDialog.setProgress(0);
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 15; i++) {
try {
//---simulate doing something lengthy---
Thread.sleep(1000);
//---update the dialog---
progressDialog.incrementProgressBy((int) (100 / 15));
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
progressDialog.dismiss();
}
}).start();
در این مورد، شما می خواهید از 1 تا 15 با تاخیر یک ثانیه بین هر شماره بشمارید. متد incrementProgressBy() شماره را در دیالوگ پردازش افزایش می دهد. زمانی که دیالوگ پردازش به 100% می رسد، خارج می شود.