2015-05-24 45 views
2

我有一个“活动”类代码,我想复制到“片段”类,但不知道片段结构,有人可以帮我做到这一点?下面是我的两个类:如何在Android中将Activity代码应用到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; 
           } 
          } 
         } 
        }); 

     } 

    } 

} 

同样运行 “活动” 的代码,我想加入到 “ImageFragment” 代码

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

将您的活动的onCreate代码复制到FragmentonCreateView

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

喜欢你的下面的代码来获取按钮。

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

将被转换成

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

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

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

,不要忘记从onCreateView返回layoutView实例。

以上建议为基础,现针对具体要求/问题发布问题,以获得更多有用答案。的这个使用getActivity

编辑,而不是()获取上下文实例。

button1 = new RadioButton(this)你应该使用

button1 = new RadioButton(**getActivity()**);

原因上面,在活动这个为您提供了上下文的实例作为活动的子类的语境,但在片段不能给你相同的访问权限,因此你需要使用getActivity()或者getActivity()。getBaseContext()来获取需要的上下文实例。

+0

嘿谢谢@AAnkit - 我按照你的建议做了,得到一行“button1 = new RadioButton(this)”的错误。错误是“构造函数RadioButton(ImageFragment)未定义”。如何克服这一点? – user1223035

+0

已编辑我的代码 – user1223035

+0

查看更新的答案。 – AAnkit

相关问题