简体   繁体   English

如何从单选按钮获取值并显示在另一个片段的编辑文本上?

[英]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.从 EditText 我打开另一个片段,其中有一个 radigroup 和单选按钮到 select 性别选项,一旦我 select 它在 Toast 上显示的按钮我选择的性别但现在我想 Z34D1F91FB2E514B857BZA 返回到 OKA 上的片段在我来到这里的同一编辑文本中选择的性别。

        [[[[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.您希望点击事件显示一个对话框,用户可以在其中 select 性别。
  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主要活动 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/参考: https://www.geeksforgeeks.org/how-to-create-a-custom-alertdialog-in-android/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在编辑文本上显示单选按钮的值? - how to display value from radio button on edit text? 如何获取单选按钮的值并在文本视图上显示(Java for android) - how to get value of radio button and display on text view (Java for android) 如何从按钮获取文本以编辑文本? - How to get text from button to edit text? 更改单选按钮选中状态时如何从单选按钮组中的单选按钮获取文本 - How to get the text from radio button in a radio group when radio button checked state is changed 将编辑文本值从片段传递到活动 - Pass edit text value from Fragment to Activity 如何从一个片段中获取价值并传递给另一个片段 - How to get value from one fragment and pass to another fragment 如何将表的单选按钮值从一个jsp传递到另一个 - How to pass a radio button value of a table from one jsp to another 如何在选择单选按钮上显示文本字段 - how to display text fields on select the radio button 如何从创建的单选按钮类的实例获取文本 - How to get text from an instance of a radio button class created 单击按钮时如何使用编辑文本连续获取输入,删除编辑文本中的上一个值 - How to get the input using the edit text continuously when the button is clicked removing the previous value in edit text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM