Customize dialog which has single-choice list items

后端 未结 3 823
一整个雨季
一整个雨季 2021-02-05 23:55

I created a Dialog with single-choice list items:

final CharSequence[] items = {\"Red\", \"Green\", \"Blue\"};

AlertDialog.Builder builder = ne         


        
3条回答
  •  [愿得一人]
    2021-02-06 00:14

    Steps for Creating customize dialog:

    1. Create the dialog box layout files, like:

      
      
      
      
          
      
          
      
      
      
      

    2. Because the layout in the ListView custom, so to create a layout file for the ListView:

       
      
       
       
      
      
    3. Create a custom Dialog class PriorityDlg inherited from Dialog

           public class PriorityDlg extends Dialog {
      
          private Context context;
          private ListView dlg_priority_lvw = null;
          public PriorityDlg(Context context) {
              super(context);
              this.context = context;
              // TODO Auto-generated constructor stub
          }
          public PriorityDlg(Context context, int theme) {
              super(context, theme);
              this.context = context;
          }
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              // TODO Auto-generated method stub
              super.onCreate(savedInstanceState);
              this.setContentView(R.layout.dlg_priority);
              dlg_priority_lvw = (ListView) findViewById(R.id.dlg_priority_lvw);
              // ListView
              SimpleAdapter adapter = new SimpleAdapter(context, getPriorityList(),
                      R.layout.lvw_priority, new String[] { "list_priority_img",
                              "list_priority_value" }, new int[] {
                              R.id.list_priority_img, R.id.list_priority_value });
              dlg_priority_lvw.setAdapter(adapter);
              //ListView
              dlg_priority_lvw
                      .setOnItemClickListener(new AdapterView.OnItemClickListener() {
      
                          @Override
                          public void onItemClick(AdapterView arg0, View arg1,
                                  int arg2, long arg3) {
      
                          }
                      });
          }
          private List> getPriorityList() {
              List> priorityList = new ArrayList>();
              HashMap map1 = new HashMap();
              map1.put("list_priority_img", R.drawable.priority_not_important);
              map1.put("list_priority_value", context.getResources().getString(
                      R.string.dlg_priority_not_important));
              priorityList.add(map1);
              HashMap map2 = new HashMap();
              map2.put("list_priority_img", R.drawable.priority_general);
              map2.put("list_priority_value", context.getResources().getString(
                      R.string.dlg_priority_general));
              priorityList.add(map2);
              HashMap map3 = new HashMap();
              map3.put("list_priority_img", R.drawable.priority_important);
              map3.put("list_priority_value", context.getResources().getString(
                      R.string.dlg_priority_important));
              priorityList.add(map3);
              HashMap map4 = new HashMap();
              map4.put("list_priority_img", R.drawable.priority_very_important);
              map4.put("list_priority_value", context.getResources().getString(
                      R.string.dlg_priority_very_important));
              priorityList.add(map4);
      
              return priorityList;
          }
      
      }
      
    4. To create a custom dialog box

      PriorityDlg dlg = new PriorityDlg (SimpleTaskActivity.this, R.style.dlg_priority); 
      dlg.show();
      

    Where R.style.dlg_priority set the dialog box uses the style file, just let the dialog box to remove the title bar, and of course you can code to complete this effect: