我有一個「活動」類代碼,我想複製到「片段」類,但不知道片段結構,有人可以幫我做到這一點?下面是我的兩個類:如何在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;
}
}
嘿謝謝@AAnkit - 我按照你的建議做了,得到一行「button1 = new RadioButton(this)」的錯誤。錯誤是「構造函數RadioButton(ImageFragment)未定義」。如何克服這一點? – user1223035
已編輯我的代碼 – user1223035
查看更新的答案。 – AAnkit