Multiple EditText objects in AlertDialog

后端 未结 3 1618
暗喜
暗喜 2020-12-08 04:18

I\'m working on a project for college that will let a user place a point on a map and then set the title and description for the overlay object. The problem is, the second <

3条回答
  •  青春惊慌失措
    2020-12-08 04:46

    A Dialog only contains one root View, that's why setView() overwrites the first EditText. The solution is simple put everything in one ViewGroup, for instance a LinearLayout:

    Context context = mapView.getContext();
    LinearLayout layout = new LinearLayout(context);
    layout.setOrientation(LinearLayout.VERTICAL);
    
    // Add a TextView here for the "Title" label, as noted in the comments
    final EditText titleBox = new EditText(context);
    titleBox.setHint("Title");
    layout.addView(titleBox); // Notice this is an add method
    
    // Add another TextView here for the "Description" label
    final EditText descriptionBox = new EditText(context);
    descriptionBox.setHint("Description");
    layout.addView(descriptionBox); // Another add method
    
    dialog.setView(layout); // Again this is a set method, not add
    

    (This is a basic example, but it should get you started.)

    You should take note of the nomenclature difference between a set and add method. setView() only holds one View, the same is similar for setMessage(). In fact this should be true for every set method, what you're thinking of are add commands. add methods are cumulative, they build a list of everything you push in while set methods are singular, they replace the existing data.

提交回复
热议问题