如何在Android中的Fragment类中使用Activity代码?

时间:2015-05-24 08:13:20

标签: android android-fragments

我有一个"活动"我要复制到的类代码" Fragment"上课但不知道片段结构,有人可以帮助我这样做吗?以下是我的两个班级:

MainActivity.java

public class MainActivity extends Activity {

    private String[] application = { "ABC", "DEF", "GHI", "HIJ" };                     
    private String[] device = { "ABC", "DEF", "GHI", "HIJ" }; 
    private RadioGroup radioGroup1;
    private RadioGroup radioGroup2;
    private RadioButton btn;
    private RadioButton btn2;
    private String text1;
    private String text2;
    RadioButton button1;
    RadioButton button2;
    Button selectall;

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

        checkButtonClick();
        drawRadiobuttons();

    }

    private void checkButtonClick() {

        Button myButton = (Button) 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(
                        getApplicationContext(),
                        "Data Posting : APPLICATION : "
                                + text1 + " \nDEVICE : " + text2,
                        Toast.LENGTH_LONG).show();


            }
        });

    }

    private void drawRadiobuttons(){

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

        ViewGroup hourButtonLayout = (ViewGroup) findViewById(R.id.radio1);
        for (int i = 0; i < application.length; i++) {
            button1 = new RadioButton(this);
            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(getApplicationContext(),
                                            "You selected : " + text1,
                                            Toast.LENGTH_SHORT).show();
                                    return;
                                }
                            }
                        }
                    });

        }

        ViewGroup hourButtonLayout2 = (ViewGroup) findViewById(R.id.radio2); 
        for (int i = 0; i < device.length; i++) {
            button2 = new RadioButton(this);
            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(getApplicationContext(),
                                            "You selected : " + text2,
                                            Toast.LENGTH_SHORT).show();
                                    return;
                                }
                            }
                        }
                    });

        }

    }

}

同样的跑步&#34;活动&#34;代码我想添加到&#34; ImageFragment&#34;代码

ImageFragment.java

public class ImageFragment extends Fragment {
    int fragVal;
    private String[] application = { "ABC", "DEF", "GHI", "HIJ" };                     
    private String[] device = { "ABC", "DEF", "GHI", "HIJ" }; 
    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 ImageFragment init(int val) {
        ImageFragment truitonFrag = new ImageFragment();
        // 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(this);
            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(this);
            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;
    }
}

1 个答案:

答案 0 :(得分:1)

将您活动的onCreate代码复制到onCreateView的{​​{1}}。

在片段中,您必须使用Fragment来获取视图实例。

就像下面的代码获取按钮一样。

layoutView

将转换为

    Button myButton = (Button) findViewById(R.id.findSelected);

你可以在onCreateView中将layoutView声明为类变量而不是局部变量,以使其可以在整个片段中访问。

    Button myButton = (Button)layoutView.findViewById(R.id.findSelected);

并且不要忘记从View layoutView; // making it accessible @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { layoutView = inflater.inflate(R.layout.activity_main, container, false); 返回layoutView个实例。

以上建议是基础,现在发布具体要求/问题的问题,以获得更有帮助的答案。

修改而非使用 getActivity()来获取上下文实例。

就像在onCreateView中一样,你应该使用

button1 = new RadioButton(this)

以上原因是,在活动中这个为您提供了上下文的实例,因为Activity是Context的子类,但在片段中,不能为您提供相同的访问权限因此,您需要使用getActivity()或getActivity()。getBaseContext()来获取所需的上下文实例。