简体   繁体   中英

how to get value from radio button and display on edit text on another fragment?

From EditText I open another fragment in which there is a radigroup and radiobutton to select gender option, once I select the button it shows on Toast the gender I selected but now I want to go back to the earlier fragment on OK button and want to show the selected gender on the same edit text from which I came here.

        [[[[radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton radioButton = view.findViewById(checkedId);
            RadioButton rMale= view.findViewById(R.id.male);
            RadioButton rFemale= view.findViewById(R.id.female);
            RadioButton rOthers= view.findViewById(R.id.other);

            Integer id = radioButton.getId();
            String gender = id.toString();

            if (rMale.isChecked()) {
                gender="Male";
            }else if(rFemale.isChecked()){
                gender="Female";
            }else if (rOthers.isChecked()){
                gender="Others";
            }

            Toast.makeText(getContext(),gender,Toast.LENGTH_SHORT).show();

            OKbtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Integer id = radioButton.getId();
                    String gender = id.toString();

                    if (rMale.isChecked()) {
                        gender="Male";
                    }else if(rFemale.isChecked()){
                        gender="Female";
                    }else if (rOthers.isChecked()){
                        gender="Others";
                    }
                    Fragment fragment = new UploadPostFragment();
                    Bundle bundle = new Bundle();
                    bundle.putString("gender",gender);
                    fragment.setArguments(bundle);
                    FragmentManager fragmentManager = getFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.framelayout,fragment);
                    fragmentTransaction.commit();

                    Toast.makeText(getContext(),gender,Toast.LENGTH_SHORT).show();


                }
            });

        }
    });


}
}
}]]]]]]

First fragment;

    Bundle info = new Bundle();

    info.putString("gender", gender);

    fragment.setArguments(info);

    fragmentManager.beginTransaction().replace(R.id.fragment_container,
            fragment).commit();

Second fragment;

Bundle bundle = this.getArguments();

String genderInNewFragment = bundle.getString("gender", "");

See one option to solve this problem of yours is to use shared preferences. But that is going to be a little tedius.

What I am suggesting is, you have to implement something like;

  1. There is edit text with gender option.
  2. You want the on click event to show a dialog where user can select gender.
  3. The again want the selected gender to be reflected on edit text.

For this I suggest you to open up a Custom Alert Dialog once you click the edit text, with a radio group in a custom layout inflated by the alert dialog which is inflating this layout.

For example:

Layout to be inflated in alert dialog

In this layout we have edit text but you have to replace it with radio group of gender

    <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:orientation="vertical" 
  android:paddingLeft="20dp" 
  android:paddingRight="20dp" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent"> 
  
  <EditText 
    android:id="@+id/editText" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"/> 
</LinearLayout>

Main Activity

The button in main activity will trigger the alert dialog but you have to replace it with on click of edit text

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:id="@+id/root"
    android:orientation="vertical"
    tools:context=".MainActivity"> 
  
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="showAlertDialogButtonClicked"
        android:text="Show Dialog"
     /> 
</LinearLayout> 

Main Activity java

public class MainActivity 
    extends AppCompatActivity { 

    @Override
    protected void onCreate( 
        Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
    } 

    public void showAlertDialogButtonClicked(View view) 
    { 

        // Create an alert builder 
        AlertDialog.Builder builder 
            = new AlertDialog.Builder(this); 
        builder.setTitle("Name"); 

        // set the custom layout 
        final View customLayout 
            = getLayoutInflater() 
                .inflate( 
                    R.layout.custom_layout, 
                    null); 
        builder.setView(customLayout); 

        // add a button 
        builder 
            .setPositiveButton( 
                "OK", 
                new DialogInterface.OnClickListener() { 

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

                        // send data from the 
                        // AlertDialog to the Activity 
                        EditText editText 
                            = customLayout 
                                .findViewById( 
                                    R.id.editText); 
                        sendDialogDataToActivity( 
                            editText 
                                .getText() 
                                .toString()); 
                    } 
                }); 

        // create and show 
        // the alert dialog 
        AlertDialog dialog 
            = builder.create(); 
        dialog.show(); 
    } 

    // Do something with the data 
    // coming from the AlertDialog 
    private void sendDialogDataToActivity(String data) 
    { 
        Toast.makeText(this, 
                    data, 
                    Toast.LENGTH_SHORT) 
            .show(); 
    } 
} 

Reference: https://www.geeksforgeeks.org/how-to-create-a-custom-alertdialog-in-android/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM