我使用以下方法设置名为GoogleMap
的{{1}}对象的mapType。
mMap
我要做的是禁用其中一个选项,让我们说第一个(Road)。 我怎么能这样做?
P.S.1我读了这篇AlertDialog with single choice list - I need some items nonclickable,但我不明白我怎么能在我的案例中使它成功。
P.S.2我试过,这个解决方案也是:Android: AlertDialog - how to disable certain choices that are not available什么都没发生。所有选项均已启用。
答案 0 :(得分:2)
可以在标准AlertDialog中执行此操作,但使用自定义列表适配器。也许您发布的第一个链接不适合您的原因是因为在填充对话框之前更新列表项很重要。
创建对话框:
final CharSequence[] MAP_TYPE_ITEMS =
{"Road", "Satellite", "Hybrid"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Set map type");
int checkItem = 0;
ArrayList<Integer> list = new ArrayList<Integer>();
//specify index 1 is disabled, 0 and 2 will be enabled as normal
list.add(Integer.valueOf(1));
final MyCustomAdapter adapter = new MyCustomAdapter(MAP_TYPE_ITEMS, list);
builder.setAdapter(adapter, null );
builder.setSingleChoiceItems(
MAP_TYPE_ITEMS,
checkItem,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch (item) {
case 0:
Toast.makeText(InitialActivity.this, "Item 0 clicked ", Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(InitialActivity.this, "Item 1 clicked ", Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(InitialActivity.this, "Item 2 clicked ", Toast.LENGTH_SHORT).show();
break;
}
if( adapter.getItemViewType(item) == 0 ){
dialog.dismiss();
}
}
}
);
AlertDialog fMapTypeDialog = builder.create();
fMapTypeDialog.show();
自定义适配器:
private class MyCustomAdapter extends BaseAdapter {
private ArrayList<String> mData = new ArrayList<String>();
private ArrayList<Integer> mDisabled = new ArrayList<Integer>();
private LayoutInflater mInflater;
public MyCustomAdapter(CharSequence[] items,
ArrayList<Integer> disabledItems) {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mDisabled = disabledItems;
for( int i = 0; i< items.length; ++i ){
addItem( items[i].toString());
}
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
if( mDisabled.contains(position))
return 1; //disabled
return 0; //enabled as normal
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public String getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch(type) {
case 1:
convertView = mInflater.inflate(android.R.layout.simple_list_item_1, null);
holder.textView = (TextView)convertView.findViewById(android.R.id.text1);
convertView.setEnabled(false);
convertView.setClickable(false);
break;
case 0:
convertView = mInflater.inflate(android.R.layout.simple_list_item_1, null);
holder.textView = (TextView)convertView.findViewById(android.R.id.text1);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.textView.setText(mData.get(position));
return convertView;
}
}
答案 1 :(得分:1)
基本上,你不能使用简单的AlertDialog和Builder。你想做什么,它在一些互动中交换你的观点,但那些项目没有这样的行为。
但使用自定义对话框来解决这个问题并不是问题。仅举例......
// create a Dialog component
final Dialog dialog = new Dialog(context);
//Tell the Dialog to use the dialog.xml as it's layout description
// With your own Layouts and CheckBoxs
dialog.setContentView(R.layout.dialog);
dialog.setTitle("Android Custom Dialog Box");
TextView headerTextView = (TextView) dialog.findViewById(R.id.txt);
headerTextView .setText("This is an Android custom Dialog Box Example! Enjoy!");
Button dialogButton1 = (Button) dialog.findViewById(R.id.dialogButton1);
dialogButton1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialogButton1.setEnabled(false);
dialog.dismiss();
}
});
// And so on, with other buttons
dialog.show();
答案 2 :(得分:0)
我回答了一个非常相似的问题here。基本上,您可以在将视图添加到给定的ListView中时设置侦听器并将其禁用。