RadioButton加Fragment,点击按钮替换布局,注册Fragment

activity_main的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <!--将被替换的布局-->
   <RelativeLayout
       android:id="@+id/rela"
       android:layout_weight="6"
       android:layout_width="match_parent"
       android:layout_height="0dp">

   </RelativeLayout>

    <RadioGroup
        android:id="@+id/radio_group"
        android:background="#fddfdd"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RadioButton
            android:checked="true"
            android:id="@+id/btn_zhuce"
            android:gravity="center"
            android:padding="20dp"
            android:textSize="23sp"
            android:button="@null"
            android:text="注册"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />

        <RadioButton
            android:id="@+id/btn_denglu"
            android:gravity="center"
            android:padding="20dp"
            android:textSize="23sp"
            android:button="@null"
            android:text="登录"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />
    </RadioGroup>
</LinearLayout>

Mainactivity的代码。获取id。替换布局

public class MainActivity extends AppCompatActivity {

    private RelativeLayout relativeLayout;
    private RadioGroup radioGroup;
  //  private FragmentTransaction transaction;

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

        //用fragment替换的布局
        relativeLayout = (RelativeLayout) findViewById(R.id.rela);
        radioGroup = (RadioGroup) findViewById(R.id.radio_group);

        //进入页面先展示 注册页面
       getSupportFragmentManager().beginTransaction().replace(R.id.rela,new ZhuceFragment()).commit();


       // transaction = getSupportFragmentManager().beginTransaction();
        //按钮选中的监听
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.btn_zhuce:
                        getSupportFragmentManager().beginTransaction().replace(R.id.rela,new ZhuceFragment()).commit();
                        break;
                    case R.id.btn_denglu:
                        getSupportFragmentManager().beginTransaction().replace(R.id.rela,new DengluFragment()).commit();
                        break;
                }
                    //执行替换
                   // transaction.commit();

            }
        });
    }
}

ZhuceFragment里面

public class ZhuceFragment extends Fragment {
    private EditText zhu_phone;
    private EditText zhu_password;
    private Button zhuce;
    private MyPresenter myPresenter;
    //注册页面

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       //填充布局
        View view = inflater.inflate(R.layout.fragment_zhuce,container,false);

        zhu_phone = (EditText) view.findViewById(R.id.zhu_phone);
        zhu_password = (EditText) view.findViewById(R.id.zhu_password);
        zhuce = (Button) view.findViewById(R.id.zhuce);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
       //new出presenter对象
        myPresenter = new MyPresenter(getActivity(), new MyPresenter.ZhuceViewCallBack() {
            @Override
            public void zhu_phone_empty() {
                Toast.makeText(getActivity(), "注册手机号不能为空", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void zhu_pass_empty() {
                Toast.makeText(getActivity(),"注册密码不能为空",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void zhu_success() {
                Toast.makeText(getActivity(),"注册成功!请前往登录页面!",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void zhu_fail() {
                Toast.makeText(getActivity(),"不存在!",Toast.LENGTH_SHORT).show();
            }
        }, new MyPresenter.LoginViewCallBack() {
            @Override
            public void deng_phone_empty() {

            }

            @Override
            public void deng_pass_empty() {

            }

            @Override
            public void deng_success() {

            }

            @Override
            public void deng_fail() {

            }
        });

        //点击注册按钮 调用p层去逻辑判断非空
        zhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //p层逻辑判断
                myPresenter.Zhuce_Panduan(zhu_phone.getText().toString(),zhu_password.getText().toString());
            }
        });
    }


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值