1

각 단편에는 4 개의 라디오 버튼이 포함 된 6 개의 단편이 있습니다. 단, 각 단편은보기 페이지를 사용하여 동일한 활동에 모두 표시되므로 사용자는이를 스 와이프 할 수 있습니다.onCheckedChanged 다른 단편의 radioGroups에 대해 단편은 ViewPager를 사용하여 생성됩니다.

각 RadioGroup에서 onCheckedChangedListener를 구현하려고 했으므로 각 단편에서 어떤 라디오 버튼을 선택했는지 알 수 있습니다.

나는 지난 일 동안 많은 일들을 시도해 보았습니다. 그리고 제 일하는 해결책은 극도로 오래 지속되었고 확실한 방법이 있습니다. XML의 온 클릭 기능 각각에 대한 새로운 방법을 창조 :

이 난 로이드를 사용하는 각각의 무선 그룹의 그것

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

의 전류 용액 단편 호출 내 활동이다. 위의 것은 단지 2 개입니다. 여기

내가 주요 활동 및 조각 활동 모두에서 같은 리스너를 사용하여 시도하고 내가 두번째을 radioGroup에 대한 널 포인터 예외를 얻을

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

같은 조각이 모습입니다. 여기

이 내가 내가 청취자가 처음보다 다른 조각에 동작하지 않습니다 무엇 이건 조각 활동

RadioGroup rg1 = (RadioGroup)  getView().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(); 
} 
} 

); 

에 사용하려고 하나, 청취자의 변화가 모습입니다.

어떤 아이디어?

답변

2

귀하의 레이아웃에서 모든 android : onClick = "onRadio1Clicked"및 activity에서 setOnCheckedChangeListener 코드를 제거하고 Q1Fragment override onViewCreated 메소드와 같은 조각으로 구현하고 거기에 코드를 작성하십시오. Q1Fragment에서

: -

@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(); 
} 
} 

); 
    } 

Q2Fragment에서 : -

@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(); 
} 
} 

); 
    } 
+0

감사합니다! 나는 onActivityCreated() 만 onViewCreated()를 시도하지 않았다. – Shannon

+0

나는 당신에게 약간의 대표를 줄 것이다. 그러나 나는 단지 pleb이고, 캔트 does not는 그것을 아직한다. – Shannon

+0

@Shannon 그것이 당신을 위해 일해 주어 기쁘다 ... – Priya

관련 문제