Android fragments 对不同片段中的放射组进行检查更改后,将使用ViewPager创建片段
我有6个片段,每个片段带有一个包含4个单选按钮的RadioGroup,它们都使用viewpager显示在同一活动中,因此用户可以在其中滑动 我一直在尝试在每个放射组上实现一个onCheckedChangedListener,这样我就可以知道在每个片段中选中了哪个单选按钮 在过去的几天里,我尝试了很多事情,我的工作解决方案非常冗长,肯定有更好的方法 这是我的活动,它调用包含当前解决方案的片段Android fragments 对不同片段中的放射组进行检查更改后,将使用ViewPager创建片段,android-fragments,android-viewpager,android-radiogroup,Android Fragments,Android Viewpager,Android Radiogroup,我有6个片段,每个片段带有一个包含4个单选按钮的RadioGroup,它们都使用viewpager显示在同一活动中,因此用户可以在其中滑动 我一直在尝试在每个放射组上实现一个onCheckedChangedListener,这样我就可以知道在每个片段中选中了哪个单选按钮 在过去的几天里,我尝试了很多事情,我的工作解决方案非常冗长,肯定有更好的方法 这是我的活动,它调用包含当前解决方案的片段 public class QuestionsActivity extends AppCompatActiv
public class QuestionsActivity extends AppCompatActivity {
Toolbar toolbar;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_questions);
// tool bar
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mViewPager = (ViewPager) findViewById(R.id.pager);
/** set the adapter for ViewPager */
mViewPager.setAdapter(new MyPagerAdapter(
getSupportFragmentManager()));
}
/**
* Defining a FragmentPagerAdapter class for controlling the fragments to be shown when user swipes on the screen.
*/
public class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
/** Show a Fragment based on the position of the current screen */
if (position == 0) {
return new Q1Fragment();
} else if (position == 1) {
return new Q2Fragment();
} else if (position == 2) {
return new Q3Fragment();
} else if (position == 3) {
return new Q4Fragment();
} else if (position == 4) {
return new Q5Fragment();
} else
return new Q6Fragment();
}
@Override
public int getCount() {
// Show 2 total pages.
return 6;
}
}
public void onRadio1Clicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.radioButton1:
if (checked) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
CharSequence text = "radio1";
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
break;
case R.id.radioButton2:
if (checked) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
CharSequence text = "radio2";
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
break;
case R.id.radioButton3:
if (checked) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
CharSequence text = "radio3";
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
break;
case R.id.radioButton4:
if (checked) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
CharSequence text = "radio4";
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
break;
}
}
public void onRadio2Clicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.radioButton1:
if (checked) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
CharSequence text = "radio1";
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
break;
case R.id.radioButton2:
if (checked) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
CharSequence text = "radio2";
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
break;
case R.id.radioButton3:
if (checked) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
CharSequence text = "radio3";
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
break;
case R.id.radioButton4:
if (checked) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
CharSequence text = "radio4";
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
break;
}
}
}
对于每个广播组,我使用xml中的android:onClick函数,并为每个组创建一个新方法。上面只是其中的两个
这是一个碎片的样子
<RadioGroup
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
>
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="310dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/border"
android:layout_marginTop="18dp"
android:padding="10dp"
android:text="@string/radio"
android:onClick="onRadio1Clicked"
/>
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="310dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/border"
android:layout_marginTop="18dp"
android:padding="10dp"
android:text="@string/radio1"
android:onClick="onRadio1Clicked"/>
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="310dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/border"
android:layout_marginTop="18dp"
android:padding="10dp"
android:text="@string/radio2"
android:onClick="onRadio1Clicked"/>
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="310dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/border"
android:layout_marginTop="18dp"
android:padding="10dp"
android:text="@string/radio3"
android:onClick="onRadio1Clicked"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/submit"
android:layout_marginTop="13dp"
/>
</RadioGroup>
无论我做什么,我都不能让听者处理第一个片段以外的片段
有什么想法吗?只需从布局中删除所有android:onClick=“onRadio1Clicked”,并从活动中设置OnCheckedChangeListener代码,然后在片段中实现它,如Q1Fragment override onViewCreated方法,并在其他片段中编写代码,依此类推 在Q1中:
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
RadioGroup rg1 = (RadioGroup) view.findViewById(R.id.radioGroup1);
rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged (RadioGroup group,int checkedId){
Context context = getContext();
RadioButton thisButton = (RadioButton) getView().findViewById(checkedId);
Toast toast = Toast.makeText(context, thisButton.getText(), Toast.LENGTH_SHORT);
toast.show();
}
}
);
}
在Q2片段中:-
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
RadioGroup rg2 = (RadioGroup) view.findViewById(R.id.radioButton2);
rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged (RadioGroup group,int checkedId){
Context context = getContext();
RadioButton thisButton = (RadioButton) getView().findViewById(checkedId);
Toast toast = Toast.makeText(context, thisButton.getText(), Toast.LENGTH_SHORT);
toast.show();
}
}
);
}
非常感谢。我没有在ViewCreated()上尝试,只是在ActivityCreated()上尝试。我会给你一些建议,但我只是个普通人,还不能这么做。
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
RadioGroup rg2 = (RadioGroup) view.findViewById(R.id.radioButton2);
rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged (RadioGroup group,int checkedId){
Context context = getContext();
RadioButton thisButton = (RadioButton) getView().findViewById(checkedId);
Toast toast = Toast.makeText(context, thisButton.getText(), Toast.LENGTH_SHORT);
toast.show();
}
}
);
}