android中警报对话框自定义标题内的复选框处理

android中警报对话框自定义标题内的复选框处理,android,checkbox,android-alertdialog,Android,Checkbox,Android Alertdialog,我的应用程序中有一个带有自定义标题的警报对话框,标题字段中有复选框,如下所示 选择All复选框时,默认情况下应选中下面的所有其他项目(多选项列表),如下所示: 此外,如果未选中All复选框,则应选中先前的选择状态(即,在选择All之前),并取消选择其他项目。我希望这一切在对话聚焦时发生 我尝试的是下面的代码 @Override public Dialog onCreateDialog(Bundle savedInstanceState) { int ti

我的应用程序中有一个带有自定义标题的警报对话框,标题字段中有复选框,如下所示

选择All复选框时,默认情况下应选中下面的所有其他项目(多选项列表),如下所示:

此外,如果未选中All复选框,则应选中先前的选择状态(即,在选择All之前),并取消选择其他项目。我希望这一切在对话聚焦时发生

我尝试的是下面的代码

@Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            int title = getArguments().getInt("title");


            View view = getActivity().getLayoutInflater().inflate(
                    R.layout.dialog_categories_title, null);

            cb = (CheckBox) view.findViewById(R.id.checkBox1);
            cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    if (isChecked) {
                        for (int i = 0; i < _selections.length; i++) {
                            _selections[i] = isChecked;

                        }
                    }

                }
            });

            return new AlertDialog.Builder(getActivity())
                    .setIcon(R.drawable.alert_dialog_icon)
                    .setTitle(title)
                    .setCustomTitle(view)

                    .setMultiChoiceItems(_categories, _selections,
                            new DialogSelectionClickHandler())

                    .setPositiveButton(R.string.alert_dialog_ok,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int whichButton) {
                                    ((MainActivity) getActivity())
                                            .doPositiveClick();
                                }

                            }).create();
}
@覆盖
创建对话框上的公共对话框(Bundle savedInstanceState){
int title=getArguments().getInt(“title”);
视图=getActivity().GetLayoutFlater().充气(
R.layout.dialog\u categories\u title,空);
cb=(复选框)view.findviewbyd(R.id.checkBox1);
cb.setOnCheckedChangeListener(新的OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮视图,
布尔值(已检查){
如果(已检查){
对于(int i=0;i<\u selections.length;i++){
_选择[i]=已检查;
}
}
}
});
返回新的AlertDialog.Builder(getActivity())
.setIcon(R.drawable.alert_对话框_图标)
.setTitle(标题)
.setCustomTitle(视图)
.setMultiChoiceItems(_类别,_选择,
新建对话框SelectionClickHandler())
.setPositiveButton(R.string.alert_对话框_ok,
新建DialogInterface.OnClickListener(){
公共void onClick(对话框接口对话框,
int(按钮){
((MainActivity)getActivity())
.doPositiveClick();
}
}).create();
}

如果我运行此操作,当选中All时,仅当我再次关闭并打开对话框时,才会检查其他项目,但我希望在当前对话框处于焦点时发生这种情况。我该怎么做呢?

我已经完成了您在图像中显示的内容。我使用的是自定义对话。我在xml文件中使用了Listview

    CheckBox repeatChkBx = (CheckBox) findViewById(R.id.repeat_checkbox);
    repeatChk2 = (CheckBox) findViewById(R.id.repeat_checkbox2);
            repeatChkBx.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    if (isChecked) {
                        repeatChk2.setChecked(true);
                    }

                }
            });
首先定义并初始化下面的arraylist

// Catagory Selection
    public static ArrayList<String> acceptpositionwhoesNearMe = new ArrayList<String>();
    public static String AcceptCatagotyIDWhoesNearMe = "";
//类别选择
public static ArrayList acceptpositionwhoesNearMe=new ArrayList();
公共静态字符串AcceptCatagotyIDWhoesNearMe=“”;
下面是我使用的代码showDialog()

public void showDialog() {
        Log.i(TAG, "Inside Show Dialog");
        final Dialog warning = new Dialog(logout_dialogue.this);
        warning.requestWindowFeature(Window.FEATURE_NO_TITLE);
        warning.setContentView(R.layout.YOUR_XML);
        warning.setCancelable(false);
        warning.getWindow().setGravity(Gravity.CENTER);

        WindowManager mWinMgr;
        mWinMgr = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        int displayWidth = mWinMgr.getDefaultDisplay().getWidth();

        warning.getWindow().setLayout(displayWidth - 75,
                LayoutParams.WRAP_CONTENT);
        warning.setOnDismissListener(new OnDismissListener() {
            public void onDismiss(DialogInterface dialog) {
                Log.i(TAG, "Inside Dialog interface");
                // test = true;
                warning.dismiss();
            }
        });
        ListView listinterest = (ListView) warning
                .findViewById(R.id.list_catagory);
        /*
         * ArrayList<String> count = new ArrayList<String>(); count.clear();
         * count.add("Hotels"); count.add("Restaurants"); count.add("Gardens");
         * count.add("Theater");
         */
        CatagorySummaryAdapter adapter;
        adapter = new CatagorySummaryAdapter(YOUR_ACTIVITY.this,
                YOUR_ARRAYLIST_OF_CATAGORY, true);

        listinterest.setAdapter(adapter);

        Button btnOk = (Button) warning.findViewById(R.id.btn_close);
        btnOk.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                // TODO Auto-generated method stub

                String Catagory = "";
                int count = HomeActivity.acceptpositionwhoesNearMe.size();

                if (count > 0) {
                    for (int i = 0; i < HomeActivity.acceptpositionwhoesNearMe
                            .size(); i++) {

                        int pos = Integer
                                .parseInt(HomeActivity.acceptpositionwhoesNearMe
                                        .get(i));
                        if (Catagory.equals("")) {
                            Catagory = GetUserDetailsJsonParser.CategoryName
                                    .get(pos);
                            HomeActivity.AcceptCatagotyIDWhoesNearMe = GetUserDetailsJsonParser.CategoryID
                                    .get(pos);
                        } else {

                            Catagory = Catagory
                                    + ","
                                    + GetUserDetailsJsonParser.CategoryName
                                            .get(pos);
                            HomeActivity.AcceptCatagotyIDWhoesNearMe = HomeActivity.AcceptCatagotyIDWhoesNearMe
                                    + ","
                                    + GetUserDetailsJsonParser.CategoryID
                                            .get(pos);
                        }

                    }

                    Log.i(TAG, "Accept Catagory IDs WhoseNear Me"
                            + HomeActivity.AcceptCatagotyIDWhoesNearMe);

                    GetUserDetailsJsonParser.InterestedIn = HomeActivity.AcceptCatagotyIDWhoesNearMe;
                    UpdateMap = true;
                    /*
                     * startActivity(new Intent(WhosNearMe.this,
                     * BuildInukshk_4.class));
                     */
                    new GetUsersInRadiusAsyncTask().execute();

                    warning.dismiss();

                } else {
                    Toast.makeText(WhosNearMe.this,
                            "Please Select One or More Catagory", 3).show();
                }
                // test = true;

            }
        });

        warning.show();
    }
public void showDialog(){
Log.i(标记“内部显示对话框”);
最终对话框警告=新建对话框(注销对话。此对话框);
警告.请求窗口功能(窗口.功能\u无\u标题);
warning.setContentView(R.layout.YOUR_XML);
警告。可设置可取消(错误);
warning.getWindow().setGravity(Gravity.CENTER);
WindowManager mWinMgr;
mWinMgr=(WindowManager)this.getSystemService(Context.WINDOW\u服务);
int displayWidth=mWinMgr.getDefaultDisplay().getWidth();
warning.getWindow().setLayout(displayWidth-75,
LayoutParams.WRAP_内容);
警告:setOnDismissListener(新的OnDismissListener(){
公共void onDismiss(对话框接口对话框){
Log.i(标签,“内部对话框界面”);
//测试=真;
警告。解除();
}
});
ListView listinterest=(ListView)警告
.findviewbyd(R.id.list\u分类);
/*
*ArrayList count=新建ArrayList();count.clear();
*计数。添加(“酒店”);计数。添加(“餐厅”);计数。添加(“花园”);
*加上(“剧院”);
*/
分类汇总适配器;
adapter=新分类摘要adapter(您的_活动。此,
你的数组列表(类别列表,对);
setAdapter(适配器);
按钮btnOk=(按钮)警告。findViewById(R.id.btn\u关闭);
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
字符串类别=”;
int count=HomeActivity.acceptpositionwhoesNearMe.size();
如果(计数>0){
对于(int i=0;i<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/edittext_back_final"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <com.inukshk.CustomTextViewBold
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:text="Interested in"
                android:textColor="#3C3C3C"
                android:textSize="18sp"
                android:textStyle="bold" />
        </LinearLayout>

        <ListView
            android:id="@+id/list_catagory"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:cacheColorHint="#00000000"
            android:divider="@android:color/transparent" >
        </ListView>

        <Button
            android:id="@+id/btn_close"
            android:layout_width="99dp"
            android:layout_height="40dp"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="10dp"
            android:background="@drawable/btn_back_final"
            android:text="CLOSE"
            android:textColor="#ffffff"
            android:textSize="16dp"
            android:textStyle="bold" />
    </LinearLayout>

</LinearLayout>
package com.inukshk.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.inukshk.HomeActivity;
import com.inukshk.R;
import com.inukshk.CreateInukshk.BuildInukshk_3;
import com.inukshk.WhosNearMe.WhosNearMe;

public class CatagorySummaryAdapter extends BaseAdapter {

    public Activity context;
    String TAG = "CatagorySummaryAdapter";
    public LayoutInflater inflater;
    public ArrayList<String> Count;
    boolean Dialogue;

    public CatagorySummaryAdapter(Activity context, ArrayList<String> Count,
            boolean Dialogue) {
        super();
        this.context = context;
        this.inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        this.Count = Count;

        this.Dialogue = Dialogue;

        // TODO Auto-generated constructor stub
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return Count.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return Count.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public class ViewHolder {
        RelativeLayout lsummary_row;
        TextView txtinterestname;
        CheckBox chkinterest;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        int pos = position;
        ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder();

            if (Dialogue) {
                convertView = inflater.inflate(
                        R.layout.listview_summary_dialogue_row, null);
            } else {
                convertView = inflater.inflate(R.layout.listview_summary_row,
                        null);
            }

            holder.txtinterestname = (TextView) convertView
                    .findViewById(R.id.txtinterestname);
            holder.lsummary_row = (RelativeLayout) convertView
                    .findViewById(R.id.lsummary_row);
            holder.chkinterest = (CheckBox) convertView
                    .findViewById(R.id.chkinterest);
            holder.chkinterest.setEnabled(true);

            holder.chkinterest.setTag(position);

            if (Dialogue) {

                for (int i = 0; i < HomeActivity.acceptpositionwhoesNearMe.size(); i++) {
                    int index = Integer
                            .parseInt(HomeActivity.acceptpositionwhoesNearMe
                                    .get(i));
                    // Log.i(TAG, "Inside for Loop of Accept Positions");
                    if (index == position) {
                        // Log.i(TAG, "Matched for index" + index);
                        holder.chkinterest.setChecked(true);
                        holder.chkinterest
                                .setButtonDrawable(R.drawable.checkbox_checked);
                    }

                }

            } else {
                for (int i = 0; i < BuildInukshk_3.acceptposition.size(); i++) {
                    int index = Integer.parseInt(BuildInukshk_3.acceptposition
                            .get(i));
                    // Log.i(TAG, "Inside for Loop of Accept Positions");
                    if (index == position) {
                        // Log.i(TAG, "Matched for index" + index);
                        holder.chkinterest.setChecked(true);
                        holder.chkinterest
                                .setButtonDrawable(R.drawable.checkbox_checked);
                    }

                }
            }

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.chkinterest
                .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked) {
                        // TODO Auto-generated method stub
                        // Log.i(TAG, "ISChecked is " + isChecked);
                        if (isChecked) {
                            // Log.i(TAG, "ISChecked is true");
                            buttonView
                                    .setButtonDrawable(R.drawable.checkbox_checked);
                            int position = Integer.parseInt(buttonView.getTag()
                                    .toString());
                            if (Dialogue) {
                                HomeActivity.acceptpositionwhoesNearMe.add(String
                                        .valueOf(position));

                                Log.i(TAG, "Accept ID of Dialogue"
                                        + HomeActivity.acceptpositionwhoesNearMe);
                            } else {
                                BuildInukshk_3.acceptposition.add(String
                                        .valueOf(position));

                                Log.i(TAG, "Accept ID"
                                        + BuildInukshk_3.acceptposition);
                            }

                        } else {
                            // Log.i(TAG, "ISChecked is false");
                            buttonView
                                    .setButtonDrawable(R.drawable.checkbox_unchecked);
                            int position = Integer.parseInt(buttonView.getTag()
                                    .toString());

                            if (Dialogue) {
                                if (HomeActivity.acceptpositionwhoesNearMe
                                        .contains(String.valueOf(position))) {
                                    // Log.i(TAG,
                                    // "Inside Already present position");
                                    HomeActivity.acceptpositionwhoesNearMe
                                            .remove(String.valueOf(position));
                                    Log.i(TAG,
                                            "Accept ID Dialogue***"
                                                    + HomeActivity.acceptpositionwhoesNearMe);
                                }
                            } else {
                                if (BuildInukshk_3.acceptposition
                                        .contains(String.valueOf(position))) {
                                    // Log.i(TAG,
                                    // "Inside Already present position");
                                    BuildInukshk_3.acceptposition.remove(String
                                            .valueOf(position));
                                    Log.i(TAG, "Accept ID ***"
                                            + BuildInukshk_3.acceptposition);
                                }
                            }
                            //
                        }

                    }
                });

        holder.txtinterestname.setText(Count.get(pos));

        return convertView;
    }

}
 ArrayList<Boolean> checked = new ArrayList<Boolean>();
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    if (isChecked) {
                        for (int i = 0; i < _selections.length; i++) {
checked.add(yourCheckBoxArray[i].isChecked)
                            _selections[i] = isChecked;

                        }
                    }
else{
for (int i = 0; i < _selections.length; i++) {
                        yourCheckBoxesArray[i].setChecked(checked.get)

                    }
                }
            });