如何从滑动片段中的所有视图中获取所有选定单选按钮的值

时间:2015-05-24 09:45:33

标签: android android-fragments

我有三个视图在滑动片段与调查问题有动态radiobuttons(所有视图的布局是相同的,我只是重复)。我可以从一个屏幕获取所选无线电按钮的数据,但是如何从所有屏幕获取所有选定无线电按钮的数据?以下是我的工作代码

enter image description here

FragmentActivity.java

public class MainActivity extends FragmentActivity {
     static final int ITEMS = 3;
     MyAdapter mAdapter;
     ViewPager mPager;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.fragment_pager);
     mAdapter = new MyAdapter(getSupportFragmentManager());
     mPager = (ViewPager) findViewById(R.id.pager);
     mPager.setAdapter(mAdapter);

     Button button = (Button) findViewById(R.id.first);
     Button submitaldatabutton = (Button) findViewById(R.id.first);
     button.setOnClickListener(new OnClickListener() {
     public void onClick(View v) {
     mPager.setCurrentItem(0);
     }
     });

     button = (Button) findViewById(R.id.last);
     button.setOnClickListener(new OnClickListener() {
     public void onClick(View v) {
     mPager.setCurrentItem(ITEMS - 1);
     }
     });

     submitaldatabutton = (Button) findViewById(R.id.submitalldata);
     submitaldatabutton.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {

             //Get all data

     }
     });

     }

     public static class MyAdapter extends FragmentStatePagerAdapter {
     public MyAdapter(FragmentManager fragmentManager) {
     super(fragmentManager);
     }

     @Override
     public int getCount() {
     return ITEMS;
     }

    @Override
    public Fragment getItem(int position) {
        // TODO Auto-generated method stub
        switch (position) {
         case 0: // Fragment # 0 - This will show image
         return LayoutFragment.init(position);
         case 1: // Fragment # 1 - This will show image
         return LayoutFragment.init(position);
         default:// Fragment # 2-9 - Will show list
         return LayoutFragment.init(position);
         }
    }

     //@Override
     /*public Fragment getItem(int position) {
     switch (position) {
     case 0: // Fragment # 0 - This will show image
     return ImageFragment.init(position);
     case 1: // Fragment # 1 - This will show image
     return ImageFragment.init(position);
     default:// Fragment # 2-9 - Will show list
     return ArrayListFragment.init(position);
     }
     }*/
     }


}

LayoutFragment.java

public class LayoutFragment extends Fragment {
    int fragVal;
    private String[] application = { "Country1", "Country2", "Country3", "Country4", "Country5", "Country6", "Country7", "Country8" };                     
    private String[] device = { "Country9", "Country10", "Country11", "Country12", "Country13", "Country14", "Country15", "Country16" }; 
    private RadioGroup radioGroup1;
    private RadioGroup radioGroup2;
    private RadioButton btn;
    private RadioButton btn2;
    private String text1;
    private String text2;
    RadioButton button1;
    RadioButton button2;
    Button selectall;
    Context thiscontext;

    static LayoutFragment init(int val) {
        LayoutFragment truitonFrag = new LayoutFragment();
        // Supply val input as an argument.
        Bundle args = new Bundle();
        args.putInt("val", val);
        truitonFrag.setArguments(args);
        return truitonFrag;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fragVal = getArguments() != null ? getArguments().getInt("val") : 1;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        thiscontext = container.getContext();
        View layoutView = inflater.inflate(R.layout.activity_main, container, false);

        Button myButton = (Button) layoutView.findViewById(R.id.findSelected);
        myButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                StringBuffer responseText = new StringBuffer();
                responseText.append("");

                // Get selected radiobuttons
                if (radioGroup1.getCheckedRadioButtonId() != -1) {
                    text1 = btn.getText().toString();
                    Log.d("Button", "Text 1 : " + text1);
                }

                if (radioGroup2.getCheckedRadioButtonId() != -1) {
                    text2 = btn2.getText().toString();
                    Log.d("Button", "Text 2 : " + text2);
                }


                Toast.makeText(
                        thiscontext,
                        "Data Posting : APPLICATION : "
                                + text1 + " \nDEVICE : " + text2,
                        Toast.LENGTH_LONG).show();


            }
        });


        //Draw Radiobuttons

        radioGroup1 = (RadioGroup) layoutView.findViewById(R.id.radio1);
        radioGroup2 = (RadioGroup) layoutView.findViewById(R.id.radio2);

        ViewGroup hourButtonLayout = (ViewGroup) layoutView.findViewById(R.id.radio1);
        for (int i = 0; i < application.length; i++) {
            button1 = new RadioButton(thiscontext);
            button1.setId(i);
            button1.setText(application[i]);
            hourButtonLayout.addView(button1);

            radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                        public void onCheckedChanged(RadioGroup mRadioGroup2,
                                int checkedId2) {
                            for (int i = 0; i < mRadioGroup2.getChildCount(); i++) {
                                btn = (RadioButton) mRadioGroup2.getChildAt(i);
                                int t = mRadioGroup2.getId();
                                System.out.println(t);

                                if (btn.getId() == checkedId2) {
                                    text1 = btn.getText().toString();
                                    Toast.makeText(thiscontext,
                                            "You selected : " + text1,
                                            Toast.LENGTH_SHORT).show();
                                    return;
                                }
                            }
                        }
                    });

        }

        ViewGroup hourButtonLayout2 = (ViewGroup) layoutView.findViewById(R.id.radio2); 
        for (int i = 0; i < device.length; i++) {
            button2 = new RadioButton(thiscontext);
            button2.setId(i);
            button2.setText(device[i]);
            hourButtonLayout2.addView(button2);

            radioGroup2
                    .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                        public void onCheckedChanged(RadioGroup mRadioGroup,
                                int checkedId) {
                            for (int i = 0; i < mRadioGroup.getChildCount(); i++) {
                                btn2 = (RadioButton) mRadioGroup.getChildAt(i);
                                int t = mRadioGroup.getId();
                                System.out.println(t);

                                if (btn2.getId() == checkedId) {
                                    text2 = btn2.getText().toString();
                                    Toast.makeText(thiscontext,
                                            "You selected : " + text2,
                                            Toast.LENGTH_SHORT).show();
                                    return;
                                }
                            }
                        }
                    });

        }



        return layoutView;
    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#fff">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textColor="#000"
        android:text="Select Question1"
        android:textSize="18sp"/>

    <Button
        android:id="@+id/findSelected"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Submit"/>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="200dip"
        android:orientation="vertical"
        android:scrollbars="none" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="200dip"
            android:orientation="vertical">

            <RadioGroup
                    android:id="@+id/radio1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dip"
                    android:layout_marginTop="5dip"
                    android:background="#fff"
                    android:checkedButton="@+id/sound" >
                </RadioGroup>
        </LinearLayout>
    </ScrollView>

     <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:layout_marginTop="5dip"
        android:layout_marginBottom="5dip"
        android:text="Select Question2"
        android:textColor="#000"
        android:textSize="18sp"/>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="360dip"
        android:orientation="vertical"
        android:scrollbars="none" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="560dip"
            android:orientation="vertical">

            <RadioGroup
                    android:id="@+id/radio2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dip"
                    android:layout_marginTop="5dip"
                    android:background="#fff"
                    android:checkedButton="@+id/sound">
                </RadioGroup>

        </LinearLayout>
    </ScrollView>

</LinearLayout>

fragment_pager.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="4dip" >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1" >
    </android.support.v4.view.ViewPager>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:gravity="center"
        android:measureWithLargestChild="true"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/first"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="First" >
        </Button>

        <Button
            android:id="@+id/last"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Last" >
        </Button>

        <Button
            android:id="@+id/submitalldata"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Submit AllData" >
        </Button>
    </LinearLayout>
</LinearLayout>

0 个答案:

没有答案