根据单选按钮选择更改/替换碎片

时间:2016-03-09 11:57:28

标签: java android android-fragments fragment

我有Activity作为两个单选按钮 我想根据单选按钮选择更改/替换片段

public class SignUp extends FragmentActivity implements OnCheckedChangeListener {

RadioGroup rg;
Signup_BloodBank sb;  //fragment 1
Signup_User su;   //fragment 2
android.support.v4.app.FragmentTransaction ft;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_up);

    rg = (RadioGroup) findViewById(R.id.rg1);
    rg.setOnCheckedChangeListener(this);

    sb = new Signup_BloodBank();
    su = new Signup_User();

    RadioButton btn1 = (RadioButton)findViewById(R.id.rbBloodBank);
    btn1.setChecked(true);
    getSupportFragmentManager().beginTransaction().add(R.id.linear2, sb).commit();


}

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {

    ft = getSupportFragmentManager().beginTransaction();
    switch (checkedId) {
    case R.id.rbBloodBank:
            ft.replace(R.id.linear2,sb);
    case R.id.rbUser:
        ft.replace(R.id.linear2,su);
        break;

    default:
        break;
    }
    ft.commit();
}

}

片段1

public class Signup_BloodBank extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        container.removeAllViews();
        return inflater.inflate(R.layout.fragment_sing_up__bb, null);
    }

}

Fragment2

public class Signup_User extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { container.removeAllViews(); return inflater.inflate(R.layout.fragment_sign_up__user, null); } }

3 个答案:

答案 0 :(得分:0)

尝试以下代码..............

1.首先给这样的单选按钮命名

在xml文件中

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >



    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

         <RadioButton
            android:id="@+id/rd1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="fragment_one" />


        <RadioButton
            android:id="@+id/rd2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="fragment_two" />        

        </RadioGroup>

</LinearLayout>

然后在你的java文件中....................

RadioButton rd1,rd2,rd_choose; RadioGroup rGroup;

    rd1=(RadioButton) findViewById(R.id.rd1);
    rd2=(RadioButton) findViewById(R.id.rd2);   


    rGroup=(RadioGroup) findViewById(R.id.radioGroup);
    rGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

              int id=group.getCheckedRadioButtonId();
              rd_choose=(RadioButton) findViewById(id);
              String rd_text=rbtanswer.getText().toString();

               if(rd_text.equals(fragment_one))
               {
                  //  replace fragment here
               }

               else{
                  //  replace fragment here
                }


        }
    }); 

希望它对你有帮助

答案 1 :(得分:0)

如果没有对您所面临的问题进行更详细的解释,我会假设它不适合您。

粗略看看,以下代码在.replace之后缺少.commit()。

case R.id.rbBloodBank:
    ft.replace(R.id.linear2,sb);
case R.id.rbUser:
    ft.replace(R.id.linear2,su);

答案 2 :(得分:0)

首先,您应该在定义无线电组后定义两个单选按钮。 请尝试以下代码:

 RadioButton rd1,rd2,rd_choose;
RadioGroup rGroup;


        rGroup=(RadioGroup) findViewById(R.id.radioGroup);
        rd1=(RadioButton) findViewById(R.id.rd1);
        rd2=(RadioButton) findViewById(R.id.rd2);  

        rGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
android.support.v4.app.Fragment f = null;
 switch (group.getCheckedRadioButtonId()) {

            case "R.id.rbBloodBank":
                f = new Signup_BloodBank();

                break;

            case " R.id.rbUser":
                f =new Signup_User();

                break;





            }
            getSupportFragmentManager().beginTransaction().replace(R.id.linear2, f).addToBackStack(null).commit();

        }); 
相关问题