gpt4 book ai didi

android - AlertDialog 与 builder.setSingleChoiceItems。禁用项目

转载 作者:行者123 更新时间:2023-12-02 15:23:27 32 4
gpt4 key购买 nike

我使用以下方法设置名为 mMapGoogleMap 对象的 mapType。

private void setMapType() {
final CharSequence[] MAP_TYPE_ITEMS =
{"Road", "Satellite", "Hybrid"};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Set map type");

int checkItem = 0;

builder.setSingleChoiceItems(
MAP_TYPE_ITEMS,
checkItem,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch (item) {
case 0:
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
break;
case 1:
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
break;
case 3:
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
break;
}

dialog.dismiss();
}
}
);

AlertDialog fMapTypeDialog = builder.create();
fMapTypeDialog.show();

}

我想做的是禁用其中一个选项,比如说第一个(道路)。我怎样才能做到这一点?

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什么都没发生。所有选项均已启用。

最佳答案

可以在标准 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;
}

}

关于android - AlertDialog 与 builder.setSingleChoiceItems。禁用项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44655216/

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