gpt4 book ai didi

android - 如何在 Android 中使用 Activity 代码到 Fragment 类中?

转载 作者:行者123 更新时间:2023-11-29 00:10:40 24 4
gpt4 key购买 nike

我有一个“Activity”类代码,我想将其复制到“Fragment”类中,但不知道 fragment 结构,有人可以帮我做吗?以下是我的两个类(class):

主 Activity .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”代码中的相同运行“Activity”代码

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;
}
}

最佳答案

将您的 Activity 的 onCreate 代码复制到 FragmentonCreateView 中。

在 fragment 中,您必须使用 layoutView 来获取 View 实例。

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

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

会被转换成

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

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

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 实例。

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

Edit 而不是 this 使用 getActivity() 获取上下文实例。

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

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

上面的原因是,在 Activity 中 this 为您提供上下文实例,因为 Activity 是 Context 的子类,但在 fragment this 中不能为您提供相同的访问权限,因此您需要使用 getActivity() 或 getActivity().getBaseContext() 在需要时获取上下文实例。

关于android - 如何在 Android 中使用 Activity 代码到 Fragment 类中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30421381/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com