gpt4 book ai didi

android - 在android中的MultiChoice AlertDialog中切换复选框

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:33:31 25 4
gpt4 key购买 nike

Hi,

I have created the MultiChoice AlertDialog The AlertDialog has five list items with checkboxes. 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.

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 事件。

下面是一些示例代码:

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();

问候维纳亚克语

最佳答案

不要重新创建对话框,只需切换当前对话框中的复选框即可。您的 onMultiChoiceClickListener 可以跟踪当前 Activity 的复选框(如果有)并在选择另一个复选框时取消选中它。这是一个经过完整测试的工作示例:

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”参数指定“null”——否则“setItemChecked”调用将无法按预期工作。它最终会使用该数组来存储选中的状态,并且“setItemChecked”不会正确更新它,所以一切都会变得困惑。奇怪,但真实。

关于android - 在android中的MultiChoice AlertDialog中切换复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3608018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com