2015-05-24 62 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

相關問題