限价选择在MultiChoice AlertDialog,机器人 [英] Limit choices in MultiChoice AlertDialog, Android
本文介绍了限价选择在MultiChoice AlertDialog,机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个MultiChoice AlertDialog在那里我有25个选择。
I have a MultiChoice AlertDialog where I have 25 choices.
我希望让用户选择只有25中任意5个了。
I want to let the user select only any 5 out of 25.
当她选择的第6选择,我想unckeck并显示吐司消息,该消息称,只有5个选项,她可以做到的。
When she selects the 6th choice I want to unckeck it and show a Toast message which says that only 5 choices she can make.
与MultiChoice AlertDialog这可能吗?请帮帮忙!
Is it possible with MultiChoice AlertDialog? Please help!
推荐答案
创建一个静态变量计数,并增加它所选的选项,递减当其取消对复选框的onclick事件。具体如下:
Create a static variable "count" and increment it on the option selected, and decrement when its deselected on the onclick event of the checkbox. As follows :
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class AlertWithCheckBoxActivity extends Activity {
/** Called when the activity is first created. */
static int count = 0;
final CharSequence[] items={".NET","J2EE","PHP"};
boolean[] itemsChecked = new boolean[items.length];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void showDialog(View v)
{
count = 0;
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("Pick a Choice");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String selectedTech="Selected Tech - ";
for (int i = 0; i < items.length; i++) {
if (itemsChecked[i]) {
selectedTech=selectedTech+items[i]+" ";
itemsChecked[i]=false;
}
}
}
});
builder.setMultiChoiceItems(items, new boolean[]{false,false,false}, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if(isChecked) {
if(count < 5) {
itemsChecked[which] = isChecked;
count++;
}else{
//Display your toast here
}
}else{
count--;
}
}
});
builder.show();
}
}
这篇关于限价选择在MultiChoice AlertDialog,机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文