2013-02-21 5 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

Мой метод создания пользовательских диалоговых

http://www.helloandroid.com/tutorials/how-display-custom-dialog-your-android-application ссылка здесь

  1. Создание пользовательских диалог один 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 в RadioGroup в xml-файл. – Kostya

+0

как добавить onclicklistener –

+1

@HarishReddy Для слушателей: RadioButton rd1 = (RadioButton) dialog.findViewById (R.id.rd_1); rd1.setOnClickListener (новый View.OnClickListener() { @ Override public void onClick (View v) { } }); – davidivad

1

Вы можете добавить одну кнопку, чтобы ваш диалог с помощью Builder.setNeutralButton.

+0

но как выбрать опцию радио только на кнопку мыши – Dimitri

+0

см этот сайт http://wptrafficanalyzer.in/blog/alert-dialog-window-with-radio-buttons-in-android/ – AndroidEnthusiastic

+0

@peter ваше решение не поддерживает froyo 2.2 – Dimitri

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

+3

, когда я назвал его оба значения, которое равно -1 – Dimitri

+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(); 
Смежные вопросы