2013-02-21 90 views
11

enter image description here
我想添加爲中心的兩個單選第二無線電按鈕B下方的按鈕,當我檢查的選項,並點擊驗證,一個動作發生。任何幫助請與單選按鈕對話框和驗證按鈕

final CharSequence[] photo = {"A","B"}; 

AlertDialog.Builder alert = new AlertDialog.Builder(this); 

alert.setTitle("Select Gender"); 

alert.setSingleChoiceItems(photo,-1, new 

DialogInterface.OnClickListener() 

{ 

    @Override 
    public void onClick(DialogInterface dialog, int which) 
    { 
     if(photo[which]=="A") 

     { 

      gen="B"; 
     } 

     else if (photo[which]=="B") 

     { 

      gen="B"; 

     } 
    } 

}); 
alert.show(); 

回答

13

我的方法從這裏

  1. 製作自定義對話框

http://www.helloandroid.com/tutorials/how-display-custom-dialog-your-android-application參考創建一個XML定製對話框

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <RadioButton 
     android:id="@+id/rd_!" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="A" /> 

    <RadioButton 
     android:id="@+id/rd_2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/rd_!" 
     android:text="B" /> 

    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/rd_2" 
     android:layout_centerInParent="true" 
     android:text="OK" /> 
    </RelativeLayout> 

和activity.java文件

Dialog dialog = new Dialog(Dialogeshow.this); 
    dialog.setContentView(R.layout.custom_dialoge); 
    dialog.setTitle("This is my custom dialog box"); 
    dialog.setCancelable(true); 
    // there are a lot of settings, for dialog, check them all out! 
    // set up radiobutton 
    RadioButton rd1 = (RadioButton) dialog.findViewById(R.id.rd_); 
    RadioButton rd2 = (RadioButton) dialog.findViewById(R.id.rd_2); 

    // now that the dialog is set up, it's time to show it 
    dialog.show(); 
+0

您應該將您的RadioButtons添加到xml文件中的RadioGroup。 – Kostya 2016-02-25 21:25:22

+0

如何添加onclicklistener – 2016-06-29 09:16:00

+1

@HarishReddy給聽衆: RadioButton rd1 =(RadioButton)dialog.findViewById(R.id.rd_1); rd1.setOnClickListener(new View.OnClickListener(){ @ Override public void onClick(View v){ } }); – davidivad 2016-08-17 15:13:46

1

您可以使用Builder.setNeutralButton添加一個按鈕到您的對話框。

+0

,但如何選擇只在按鈕的無線選項,單擊 – Dimitri 2013-02-21 06:30:00

+0

請參閱本網站http://wptrafficanalyzer.in/blog/alert-dialog-window-with-radio-buttons-in-android/ – AndroidEnthusiastic 2013-02-21 06:31:54

+0

@peter您的解決方案不支持froyo 2.2 – Dimitri 2013-02-21 06:49:10

5

您可以使用以下方法來顯示對話框

public void showDialog(Context context, String title, String[] btnText, 
     DialogInterface.OnClickListener listener) { 

    final CharSequence[] items = { "One", "Two" }; 

    if (listener == null) 
     listener = new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface paramDialogInterface, 
        int paramInt) { 
       paramDialogInterface.dismiss(); 
      } 
     }; 
    AlertDialog.Builder builder = new AlertDialog.Builder(context); 
    builder.setTitle(title); 

    builder.setSingleChoiceItems(items, -1, 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int item) { 

       } 
      }); 
    builder.setPositiveButton(btnText[0], listener); 
    if (btnText.length != 1) { 
     builder.setNegativeButton(btnText[1], listener); 
    } 
    builder.show(); 
} 

而且主叫方可以按以下步驟進行:

showDialog(MainActivity.this, "Your Title", new String[] { "Ok" }, 
    new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 

      if(which==-1) 
      Log.d("Neha", "On button click"); 
      //Do your functionality here 
     } 
    }); 
+1

我不知道如何檢查一個單選按鈕是否與您的解決方案 – Dimitri 2013-02-21 08:03:13

+3

檢查時,當我把它叫做兩個值是-1 – Dimitri 2013-02-21 08:09:05

+0

你能解釋當我們選擇這個代碼中的項目時,圓圈是如何顯示綠色嗎?並且我們可以直接在onClick監聽器中調用這個警告對話框,而不用這個showDialog方法嗎?我試過這個,但圓圈不顯示選擇。 @Neha Dhanwani – 2015-12-31 11:29:30

14

嘗試做到這一點,您只需選擇默認選擇並在對話框中添加一個整數 - >inputSelection

final CharSequence[] items = { " HDMI IN ", " AV IN" }; 

     // Creating and Building the Dialog 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Select Input Type"); 

     builder.setSingleChoiceItems(items,inputSelection, 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int item) { 
         inputSelection = item; 
         levelDialog.dismiss(); 
        } 
       }); 
     levelDialog = builder.create(); 
     levelDialog.show();