قرار دادن Checkbox در Alertdialog در برنامه نویسی اندروید

سلام دوستان در این سری از آموزش برنامه نویسی اندروید به آموزش قرار دادن Checkbox در Alertdialog در برنامه نویسی اندروید می پردازیم قبلا آموزش های شبیه به این مانند قرار دادن radio button در Alertdialog پرداخته بودیم و امروز در صدد برآمدیم تا آموزش CheckBox آن را نیز قرار دهیم در ادامه با ما همراه باشید.
ابتدای کار وارد فایل layout خود شده در این جا نام layout برابر با activity_main.xml است سپس کد زیر را در آن قرار دهید.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".MainActivity" >
 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Show Alert" />
 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_above="@+id/button1"
 android:layout_centerHorizontal="true"
 android:layout_marginBottom="52dp"
 android:text="Result :"
 android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

در کد بالا یک دکمه قرار دارد که با کلیک بروی آن Alert ما به نمایش در می آید در Alert همانطور که گفتیم یکسری checkbox قرار دارد و یک TextView برای نمایش نتیجه checkbox های انتخاب شده در نظر گرفته ایم.

package ir.programchi;
import java.util.Arrays;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
 Button button;
 TextView textview;
 AlertDialog.Builder alertdialogbuilder;
 String[] AlertDialogItems = new String[]{
 "Android",
 "PHP",
 "WordPress",
 "Swift"
 };
 List<String> ItemsIntoList;
 boolean[] Selectedtruefalse = new boolean[]{
 false,
 false,
 false,
 false,
 false
 };
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 button = (Button)findViewById(R.id.button1);
 textview = (TextView)findViewById(R.id.textView1);
 button.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 textview.setText("");
 alertdialogbuilder = new AlertDialog.Builder(MainActivity.this);
 ItemsIntoList = Arrays.asList(AlertDialogItems);
 alertdialogbuilder.setMultiChoiceItems(AlertDialogItems, Selectedtruefalse, new DialogInterface.OnMultiChoiceClickListener() {
 @Override
 public void onClick(DialogInterface dialog, int which, boolean isChecked) {
 }
 });
 alertdialogbuilder.setCancelable(false);
 alertdialogbuilder.setTitle("Select Subjects Here");
 alertdialogbuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
 @Override
 public void onClick(DialogInterface dialog, int which) {
 int a = 0;
 while(a < Selectedtruefalse.length)
 {
 boolean value = Selectedtruefalse[a];
 if(value){
 textview.setText(textview.getText() + ItemsIntoList.get(a) + "\n");
 }
 a++;
 }
 }
 });
 alertdialogbuilder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
 @Override
 public void onClick(DialogInterface dialog, int which) {
 }
 });
 AlertDialog dialog = alertdialogbuilder.create();
 dialog.show();
 }
 });
 }
}

در ابتدا یک آرایه داریم که در آن متن مربوط به هر آیتم در یک اندیس قرار میگیرد. یک ارایه از نوع boolean که تعیین می کند که هر checkbox تیک خورده باشد یا خیر و برای اینکه نوع checkbox ی آن نمایش پیدا کند از setMultiChoiceItems استفاده کردیم برای اینکه اگر کاربر بیرون محوطه alert کلیک کرد alert ما از بین نره از ویژگی setCancelable استفاده شده است.
اون لیستی که در ابتدا ایجاد کردیم رو در Alert استفاده کردیم دقت کنید متوجه می شوید کدام بخش رو دارم عرض می کنم.
 
محاصل کار ما همانند زیر می شود.

 
این آموزش هم به پایان رسید.
 
موفق باشید.
 

مطالعه بیشتر