2017-05-29 49 views
-4

我想创建一个包含单选列表项的警报对话框,该选项将帮助用户选择应用程序的主题。我已阅读开发者网站(Developers Website)如何制作多项选择项目,但我不太明白如何制作单选列表项目 在此先感谢。如何制作单项选项

//MY CODE 
 

 
public class ThemeDialog extends DialogFragment { 
 

 
ArrayList mSelectedItems = new ArrayList(); // Track the selected items 
 

 
    public static ThemeDialog newInstance(){ 
 
     ThemeDialog f = new ThemeDialog(); 
 
     return f; 
 
    } 
 
    
 
    //Single-choice Item code 
 
    
 

 

 
}

+0

对不起。但你想添加复选框/单选按钮?并为他们的听众? –

+0

单选按钮@aa_oo –

+0

@JamesOla检查我的更新和 –

回答

-1

您需要添加RadioGroup中标签里面单选按钮在乌拉圭回合对话xml文件。

custom_dialog.xml

<RadioGroup 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/RGroup"> 

      <RadioButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Blue Theme" 
       android:id="@+id/bluetheme"/> 
      <RadioButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Black Theme" 
       android:id="@+id/blacktheme"/> 
      <RadioButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Brown Theme" 
       android:id="@+id/browntheme" />           

      <RadioButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Orange Theme" 
       android:id="@+id/orangetheme"/> 


     </RadioGroup> 

只是重视这个XML与对话片段......这仅仅是简单的example..u可以在乌拉圭回合的方式也

public class DFragment extends DialogFragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.custom_dialog, container, 
       false); 
     getDialog().setTitle("DialogFragment Tutorial");   
     // Do something else 
     return rootView; 
    } 
} 
+0

可以解释为什么投入? –

0
@Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 

     AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity()); 

     dialog.setTitle("Please Select"); 
     dialog.setPositiveButton("Cancel", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       //Action when cancel is clicked 
       dialog.dismiss(); 
      } 
     }); 

     CharSequence[] cs = new CharSequence[]{"Item-1", "Item-2","Item-3"}; 
     dialog.setSingleChoiceItems(cs, defaultSelectedPosition, new OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       //Action when any item is clicked 
       dialog.dismiss(); 
      } 
     }); 

     return dialog.create(); 
    } 

dialog.setSingleChoiceItems(cs, position, selectItemListener);使用将创建单一选择对话框。

0

创建XML布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_gravity="bottom" 
android:background="@color/colorWhite" 
android:orientation="vertical"> 


<View 
    android:layout_width="match_parent" 
    android:layout_height="2dp" 
    android:background="@color/colorPrimary"> 

</View> 


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/colorWhite" 
    android:orientation="vertical" 
    android:padding="10dp"> 

    <TextView 

     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="please select a theme" 
     android:textColor="@color/colorRed" 
     android:textSize="18sp" /> 


    <RadioGroup 
     android:id="@+id/cancle_booking_radio_group" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp"> 

     <RadioButton 
      android:id="@+id/theme1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:buttonTint="@color/colorPrimary" 
      android:text="theme1" /> 

     <RadioButton 
      android:id="@+id/theme2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:buttonTint="@color/colorPrimary" 
      android:text="theme2" /> 

     <RadioButton 
      android:id="@+id/theme3" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:buttonTint="@color/colorPrimary" 
      android:text="theme3" /> 

     <RadioButton 
      android:id="@+id/theme4" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:buttonTint="@color/colorPrimary" 
      android:text="theme4" /> 

     <RadioButton 
      android:id="@+id/theme5" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:buttonTint="@color/colorPrimary" 
      android:text="theme5" /> 


    </RadioGroup> 

</LinearLayout> 

现在创建自定义对话框

final Button btnCancelbooking; 
    final TextView tvHideCancelDialog; 
    final RadioButton theme1, theme2, theme3, theme4, theme6; 
    final Dialog cancelDialog = new Dialog(BookingConfirmClass.this, R.style.DialogSlideAnim); 
    //cancelDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); /*use when not set in style file*/ 
    cancelDialog.setContentView(R.layout.custom_cancel_booking); 
    getWindow().setGravity(Gravity.BOTTOM); 
    cancelDialog.show(); 



    theme1 = (RadioButton) cancelDialog.findViewById(R.id.theme1); 
    theme2 = (RadioButton) cancelDialog.findViewById(R.id.theme2); 
    theme3 = (RadioButton) cancelDialog.findViewById(R.id.theme3); 
    theme4 = (RadioButton) cancelDialog.findViewById(R.id.theme4); 
    theme6 = (RadioButton) cancelDialog.findViewById(R.id.theme5); 


    final RadioGroup radioGroup = (RadioGroup) cancelDialog.findViewById(R.id.cancle_booking_radio_group); 


    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 

     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 

      View radioButton = radioGroup.findViewById(checkedId); 
      int index = radioGroup.indexOfChild(radioButton); 
      switch (index) { 
       case 0: 
        //do your action for theme 1 
        break; 
       case 1: 
        //do your action for theme 2 
        break; 
       case 2: 
        //do your action for theme 3 
        break; 
       case 3: 
        //do your action for theme 4 
        break; 
       case 4: 
        //do your action for theme 5 
        break; 
      } 
     } 
    }); 
0

使用最后一个变量显然是不行的(因为它只能被分配一次,在宣布时)。所谓的“全局”变量通常是一种代码异味(特别是当它们成为Activity类的一部分时,它通常是创建AlertDialogs的地方)。更干净的解决方案是将DialogInterface对象转换为AlertDialog,然后调用getListView()。getCheckedItemPosition()。就像这样:

new AlertDialog.Builder(this) 
     .setSingleChoiceItems(items, 0, null) 
     .setPositiveButton(R.string.ok_button_label, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 
       dialog.dismiss(); 
       int selectedPosition = ((AlertDialog)dialog).getListView().getCheckedItemPosition(); 
       // Do something useful withe the position of the selected radio button 
      } 
     }) 
     .show(); 

要在这更定制外观 - How to design Single Selection Dialog?