AlertDialog показывает выбранный результат из RadioButton, заставьте пользователей проверить еще раз - PullRequest
0 голосов
/ 13 сентября 2018

Я новичок в Android Studio. Я не знаю, как решить мою проблему. Есть фрагмент, который я бы хотел, чтобы пользователь выбирал то, что ему нравится, в Radio Group, у которой есть три переключателя. Перед тем, как отправить свой результат В другом фрагменте мне нужно открыть диалоговое окно Alert, чтобы пользователь снова мог проверить, что он выбрал. Можно ли как-нибудь собрать результат всех кнопок и показать его пользователям?

public class MainActivity extends Fragment {
@Nullable
@Override
 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.radio_button, container, false);

    RadioGroup rgdSize = view.findViewById(R.id.rdgSize);
    rgdSize.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            switch (i) {

                case (R.id.rdbSmall):

                    break;

                case (R.id.rdbMedium):

                    break;

                case (R.id.rdbLarge):

                    break;
            }


        }
    });

    RadioGroup rgdRank = view.findViewById(R.id.rdgRank);
    rgdRank.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            switch (i) {

                case (R.id.rdbFirst):

                    break;

                case (R.id.rdbSecond):

                    break;

                case (R.id.rdbThird):

                    break;
            }


        }
    });


    Button btnSend;
    btnSend = view.findViewById(R.id.btnSend);
    btnSend.setOnClickListener(btnSendListener);

    return view;
}

private Button.OnClickListener btnSendListener = new Button.OnClickListener(){


    @Override
    public void onClick(View view) {

        switch (view.getId()){

            case R.id.btnSend:
                new AlertDialog.Builder(getActivity())
                        .setTitle("Please make sure you choose correct")
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {


                            }
                        })

                        .setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {


                            }
                        })
                        .show();

        }

        }

    };
}

Вот мой XML

<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/rdgSize">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Size"
        android:textSize="40sp"/>

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Small"
        android:textSize="20sp"
        android:id="@+id/rdbSmall"/>


    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium"
        android:textSize="20sp"
        android:id="@+id/rdbMedium"/>


    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large"
        android:textSize="20sp"
        android:id="@+id/rdbLarge"/>


</RadioGroup>


<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintTop_toBottomOf="@+id/rdgSize"
    android:id="@+id/rdgRank">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40sp"
        android:text="Rank"/>


    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="First"
        android:textSize="20sp"
        android:id="@+id/rdbFirst"/>


    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Second"
        android:textSize="20sp"
        android:id="@+id/rdbSecond"/>


    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Third"
        android:textSize="20sp"
        android:id="@+id/rdbThird"/>


</RadioGroup>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@+id/rdgRank"
    android:text="Send"
    android:id="@+id/btnSend"/>

Ответы [ 3 ]

0 голосов
/ 13 сентября 2018

Вы можете сделать так:

RadioButton rb = (RadioButton)rgdRank.findViewById(rgdRank.getCheckedRadioButtonId());//get checked radiobutton
String s =  rb.getText(); //get text of this checked radiobutton

После этого с помощью функции класса String отобразите то, что вы хотите.

0 голосов
/ 13 сентября 2018

Таким образом, вы можете сделать ваши радиогруппы универсальными:

  RadioGroup rgdSize,rgdRank;

 @Nullable
 @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable 
 ViewGroup container, @Nullable Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.radio_button, container, false);

 rgdSize = view.findViewById(R.id.rdgSize);
 rgdRank = view.findViewById(R.id.rdgRank);

 Button btnSend;
btnSend = view.findViewById(R.id.btnSend);
btnSend.setOnClickListener(btnSendListener);

return view;
 }

Замените свой код алертиалдог на этот:

         private Button.OnClickListener btnSendListener = new Button.OnClickListener(){


@Override
public void onClick(View view) {

    switch (view.getId()){

        case R.id.btnSend:
            int selectedId = rgdSize.getCheckedRadioButtonId();

        // find the radiobutton by returned id
        radioButton = (RadioButton) findViewById(selectedId);
        int selectedId2 = rgdRank.getCheckedRadioButtonId();

        // find the radiobutton by returned id
        radioButton2 = (RadioButton) findViewById(selectedId2);

      String result = radioButton.getText()+""+radioButton2.getText();



           new AlertDialog.Builder(getActivity())
                    .setTitle("Please make sure you choose correct")
                    .setMessage(""+result)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {


                        }
                    })

                    .setNegativeButton("CANCEL", new 
              DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {


                        }
                    })
                    .show();

    }

    }

};
0 голосов
/ 13 сентября 2018

Вы можете использовать rgdRank.getCheckedRadioButtonId();, чтобы получить проверенный идентификатор радиокнопки.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...