先给一个数组:String[] items = new String[]{"全部", "热门动态", "旅游景点", "美食推荐", "酒店住宿",
"本地特产", "农家玩乐", "娱乐中心", "美容美发"};
private String[] food = new String[]{"特色餐饮", "中西餐厅", "奶茶店", "小吃店"};
private String[] xiuxian = new String[]{"休闲会所", "休闲茶楼"};
private String[] meirong = new String[]{"美容院", "发型屋"};
首先创建一个AlertDialog.Builder builder = new AlertDialog.Builder(SearchActivity.this);
遍历出数组的元素:for (int i = 0; i < items.length; i++) {
if (searchall.getText().toString().equals(items[i])) {
location = i;
}
通过dialog中的一个方法:
builder.setSingleChoiceItems(items, location, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
searchall.setText(items[which]);
//判断是否有两个选项
switch (which) {
//执行到这几种情况时显示第二个弹框
case 3:
search_img2.setVisibility(View.VISIBLE);
searchall2.setVisibility(View.VISIBLE);
searchall2.setText(food[0]);
break;
case 7:
search_img2.setVisibility(View.VISIBLE);
searchall2.setVisibility(View.VISIBLE);
searchall2.setText(xiuxian[0]);
break;
case 8:
search_img2.setVisibility(View.VISIBLE);
searchall2.setVisibility(View.VISIBLE);
searchall2.setText(meirong[0]);
break;
default:
search_img2.setVisibility(View.GONE);
searchall2.setVisibility(View.GONE);
editext.setText(items[which]);
break;
}
dialog.dismiss();
secondDialog(which);
}
});
builder.create().show();
//调用第二次弹框
private void secondDialog(final int position) {
searchall2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (position) {
case 3:
showD(food);
break;
case 7:
showD(xiuxian);
break;
case 8:
showD(meirong);
break;
}
}
});
}
//第二次弹框
private void showD(final String[] food) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
for (int i = 0; i < food.length; i++) {
if (searchall2.getText().toString().equals(food[i])) {
location = i;
}
}
builder.setSingleChoiceItems(food, location, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
searchall2.setText(food[which]);
editext.setText(food[which]);
dialog.dismiss();
}
});
builder.create().show();
}
Diaglog弹出框
最新推荐文章于 2020-11-03 13:36:51 发布