1

ラジオグループに4つのラジオボタンが含まれている6個のフラグメントがあり、ビューパージャーを使用して同じアクティビティに表示されます。onCheckedChanged異なるフラグメントのラジオグループの場合、フラグメントはViewPagerを使用して作成されます

各RadioGroupでonCheckedChangedListenerを実装しようとしていたので、各ラジオボタンが各フラグメントでチェックされていることがわかりました。

私は過去の日々に多くのことを試してきましたが、私の仕事の解決策は非常に長引いており、確実に良い方法があります。 XMLでのonClick機能と各1のための新しい方法を作成します。

これは私がアンドロイドを使用して各無線グループにとって

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つだけです。ここで

は、フラグメントは、私が主な活動とフラグメントの活性の両方のようにリスナーを使用してみましたが、私は第二ラジオグループにnullポインタ例外を取得

<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:onRadio1Clickedを削除し、アクティビティからsetOnCheckedChangeListenerコードを削除し、Q1FragmentオーバーライドonViewCreatedメソッドのようなフラグメントで実装し、そこにコードを書き込んでください。 Q1Fragmentで

: - Q2Fragmentで

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

); 
    } 

: -

@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

私はあなたにいくつかの担当者を与えるだろうが、私はちょうど嘆願とそれを行うことはできません。 – Shannon

+0

@Shannonそれはあなたのために働いてうれしい... – Priya

関連する問題