قرار دادن یک دکمه (Button) درون پیام نمایش داده شده توسط Dialog و اجرای یک سری کد دلخواه پس از اشاره بر روی دکمه (Button)، در برنامه نویسی اندروید
در مبحثی دیگر، چگونگی نمایش یک پیام به کاربر، در یک پنجره جدید، توسط Dialog را شرح دادیم ( آموزش شماره 227 )، اکنون قصد داریم که شرح بدهیم که چگونه می توان درون پنجره مربوط به پیام، یک دکمه (Button) قرار داد که با اشاره کاربر بر روی آن دکمه، یک سری کدهای دلخواه اجرا شود.
نتیجه به صورت زیر خواهد بود :

مثلا در این مثال، تعیین می کنیم که با اشاره کاربر بر روی دکمه (Button)، پنجره مربوط به پیام بسته شود (شما می توانید کدهای دلخواه دیگری را تعیین کنید).
فایل xml متناظر برای لایه گرافیکی Dialog را به صورت فایل با نام custom.xml می سازیم :

کدهای فایل custom.xml را به صورت زیر می نویسیم :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp" />
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/dialogButtonOK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Ok "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp" />
</LinearLayout>
همان طور که مشاهده می کنید، در آن، یک ImageView برای نمایش عکس، یک TextView برای نمایش متن و یک دکمه (Button) تعریف کرده ایم.
اکنون در فایل java مربوط به Activity که در آن می خواهیم با استفاده از Dialog ، پیام نمایش داده شود، کدهای زیر را می نویسیم (اینکه کجا نوشته شود، بستگی به انتخاب شما دارد، مثلا می توانید یک Button بسازید که پس از اشاره کاربر بر روی آن، این کدها اجرا شده و پیام به کاربر نمایش داده شود) :
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
بخش زیر، تعیین کرده است که پس از اشاره بر روی دکمه (Button) موجود در پنجره پیام، چه کدهایی اجرا شود :
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
کدهایی که برای اجرا تعیین شده است، کدهای زیر می باشند :
کد بالا برای بستن پنجره پیام می باشد، اما شما می توانید کدهای دلخواه مورد نظر خود را به جای آن بنویسید.


سلام. نمیدونم چرا توفایل mainactivity در اواخر کدها توی R.menu.main ... کلمهی menu همش ارور میده. اصلا نمیدونم چرا. چون من همه مراحلو درست رفتم.