在Android的切换的复选框,在MultiChoice AlertDialog [英] Toggling check boxes in MultiChoice AlertDialog in android

查看:105
本文介绍了在Android的切换的复选框,在MultiChoice AlertDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我已经创建了 MultiChoice   AlertDialog 的AlertDialog有五个   列表项与复选框。在这里,当我   检查第一个复选框,w.r.t这个   如果在列表中的其他复选框   检查他们shud被选中   自动反之亦然。

I have created the MultiChoice AlertDialog The AlertDialog has five list items with checkboxes. Here when I check First checkbox, w.r.t this the if the other checkboxes in the list are checked they shud be unchecked automatically and vice versa.

我检查了状态器isChecked   在的onClick 的方法    OnMultiChoiceClickListener()并调用   的ShowDialog(DIALOG_MULTIPLE_CHOICE); 通过更新的布尔[]   checkedItems; 以重建   对话,但我无法去实现它。   如果您有什么建议,请直接   我到正确的方式。

I am checking the isChecked status in the onClick method of OnMultiChoiceClickListener() and calling the showDialog(DIALOG_MULTIPLE_CHOICE); by updating boolean[] checkedItems; to recreate the Dialog, But I am unable to achieve it. If you any suggestions please direct me to right way.

请问有什么办法可以重新创建的单选按钮,点击AleartDialog onclick事件。

下面的一些示例code:

Some Sample Code below:

case DIALOG_MULTIPLE_CHOICE:
final String[] lJobTypes = { "Item1", "Item2", "Item3","Item4", "Item5" };
    return new AlertDialog.Builder(JoblistPage.this)
    // .setIcon(R.drawable.logo)
    .setTitle("Title Here")
    // .setCustomTitle(m_Title)
    .setMultiChoiceItems(lTypes, m_Selections,
        new DialogInterface.OnMultiChoiceClickListener() {

            public void onClick(DialogInterface dialog,int whichButton, boolean isChecked) {
                /* User clicked on a check box do some stuff */
                if (isChecked) {
                    m_CheckCount++;
                    //Toggle the Radio button Check status
                } else {
                    m_CheckCount--;
                }
                                                }
        }).setPositiveButton("Ok",
        new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog,
                    int whichButton) {
                    }
                }).create();

问候 维纳亚克

Regards Vinayak

推荐答案

不要重新创建对话框,只需拨动当前对话框中的复选框。您onMultiChoiceClickListener可以保持当前活动的复选框的轨道(如果有的话),并取消它,当另一个选择。这里有一个完整的测试,工作的例子:

Don't recreate the dialog, just toggle the checkboxes within the current dialog. Your onMultiChoiceClickListener can keep track of the currently active checkbox (if any) and uncheck it when another is selected. Here's a complete tested, working example:

package com.stackoverflow.beekeeper;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

public class StackOverflowTest extends Activity {
  /** Called when the activity is first created. */
  @Override public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  }

  private int mSelected = -1;

  @Override protected void onResume() {
    super.onResume();
    final Builder build = new Builder(this);
    build.setTitle("List selection");
    build.setCancelable(true);
    final String[] strings = new String[]{"Cow", "Horse", "Goat"};
    final OnMultiChoiceClickListener onClick =
      new OnMultiChoiceClickListener() {
        @Override public void onClick(final DialogInterface dialog,
                                      final int which, final boolean isChecked) {

          if (isChecked) {
            if ((mSelected != -1) && (mSelected != which)) {
              final int oldVal = mSelected;
              final AlertDialog alert = (AlertDialog)dialog;
              final ListView list = alert.getListView();
              list.setItemChecked(oldVal, false);
            }
            mSelected = which;
          } else
            mSelected = -1;
        }
      };
    build.setMultiChoiceItems(strings, null, onClick);
    build.setPositiveButton("Done", new OnClickListener() {
      @Override public void onClick(final DialogInterface dialog,
                                    final int which) {
        String message = null;
        if (mSelected == -1)
          message = "You didn't select anything.";
        else
          message = "You selected '" + strings[mSelected] + "'";
        Toast.makeText(StackOverflowTest.this, message, Toast.LENGTH_LONG).show();
      }
    });
    build.show();
  }
}

有一点需要注意的:你必须指定空为你setMultiChoiceItems呼叫checkedItems参数 - 否则setItemChecked电话将无法正常工作。它最终将使用数组存储的选中状态,而setItemCheckedwould'nt正确地更新它,所以一切都感到困惑。奇怪,但事实。

One thing to watch for: you must specify "null" for the "checkedItems" parameter in your "setMultiChoiceItems" call -- otherwise the "setItemChecked" calls won't work as expected. It would end up using that array to store the checked state, and "setItemChecked" would'nt update it correctly, so everything would get confused. Odd, but true.

这篇关于在Android的切换的复选框,在MultiChoice AlertDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆