AlertDialog显示从RadioButton中选择的结果,让用户再次检查

问题描述 投票:0回答:3

我是Android Studio的初学者。我不知道如何解决我的问题。有一个片段,我希望用户在Radio Group中选择他们喜欢的东西,它有三个单选按钮。在他们将结果发送到另一个片段之前,我需要弹出警告对话框,让用户再次检查他们选择的内容。有什么方法可以收集所有按钮结果并显示给用户?

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"/>
java android android-studio
3个回答
0
投票

通过这种方式,您可以使您的无线电组成为通用的:

  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;
 }

用以下代码替换你的alertdialog代码:

         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
投票

您可以使用qazxsw poi获取已选中的单选按钮ID。


0
投票

你可以这样做:

rgdRank.getCheckedRadioButtonId();

在此之后,使用RadioButton rb = (RadioButton)rgdRank.findViewById(rgdRank.getCheckedRadioButtonId());//get checked radiobutton String s = rb.getText(); //get text of this checked radiobutton 类函数来显示您想要的内容。

© www.soinside.com 2019 - 2024. All rights reserved.