更改片段内布局的按钮

button to change layout inside fragment

所以我有一个带选项卡的应用程序 activity 我想在单击按钮 (btlink) 时更改第一个选项卡的布局:

登录(第一个fragment/tab)代码

    public class login extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.login, container, false);
        final EditText etUtilizador = (EditText) getView().findViewById(R.id.etUtilizador);
        final EditText etPassword = (EditText) getView().findViewById(R.id.etPassword);
        final Button btLogin = (Button) getView().findViewById(R.id.btLogin);
        Button btlink = (Button) getView().findViewById(R.id.btlink);
        btlink.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent registerIntent;
                registerIntent = new Intent(login.this, registar.class);
                login.this.startActivity(registerIntent);
            }
        });
        return rootView;
    }
}

但它在以下位置显示错误:"registerIntent = new Intent(login.this, registar.class);"

登录xml密码

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
    android:id="@+id/section_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/activity_vertical_margin"
    android:layout_marginEnd="@dimen/activity_horizontal_margin"
    android:layout_marginStart="@dimen/activity_horizontal_margin"
    android:layout_marginTop="@dimen/activity_vertical_margin">
</TextView>

<EditText
    android:id="@+id/etUtilizador"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/section_label"
    android:layout_marginTop="70dp"
    android:ems="10"
    android:hint="Utilizador"
    android:inputType="textPersonName"
    android:textColor="@color/colorPrimaryDark" />

<EditText
    android:id="@+id/etPassword"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/etUtilizador"
    android:layout_marginTop="30dp"
    android:ems="10"
    android:hint="Password"
    android:inputType="textPassword"
    android:textColor="@color/colorPrimaryDark" />

<Button
    android:id="@+id/btLogin"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/etPassword"
    android:layout_marginTop="41dp"
    android:background="@color/colorAccent"
    android:text="Login"
    android:textAppearance="@style/BotaoRegisto" />

<Button
    android:id="@+id/btlink"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/btLogin"
    android:layout_marginTop="49dp"
    android:background="@color/colorAccent"
    android:text="Registar"
    android:textAppearance="@style/BotaoRegisto" />

<TextView
    android:id="@+id/tvregisto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btLogin"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="14dp"
    android:text="TextView"
    android:textColor="@color/colorPrimary"
    tools:text="Se ainda nao tem conta registe-se aqui:" />

我只希望代码在单击按钮时将布局更改为此:

registar.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/section_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/activity_vertical_margin"
    android:layout_marginEnd="@dimen/activity_horizontal_margin"
    android:layout_marginStart="@dimen/activity_horizontal_margin"
    android:layout_marginTop="@dimen/activity_vertical_margin">

</TextView>

<EditText
    android:id="@+id/etNome"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/section_label"
    android:layout_marginTop="70dp"
    android:ems="10"
    android:hint="Nome"
    android:inputType="textPersonName"
    android:textColor="@color/colorPrimaryDark" />

<EditText
    android:id="@+id/etEmail"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/etNome"
    android:layout_marginTop="30dp"
    android:ems="10"
    android:hint="Email"
    android:inputType="textEmailAddress"
    android:textColor="@color/colorPrimaryDark" />

<Button
    android:id="@+id/btPedido"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/etNumero"
    android:layout_marginTop="99dp"
    android:background="@color/colorAccent"
    android:elevation="0dp"
    android:text="Enviar pedido de registo"
    android:textAppearance="@style/BotaoRegisto" />

<EditText
    android:id="@+id/etNumero"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/etEmail"
    android:layout_marginTop="30dp"
    android:ems="10"
    android:hint="Nº faturação ou série"
    android:inputType="textPersonName"
    android:textColor="@color/colorPrimaryDark" />

如果有人可以,我将不胜感激。

一开始设置rootView而不是getView().

        final EditText etUtilizador = (EditText) rootView.findViewById(R.id.etUtilizador);
        final EditText etPassword = (EditText) rootView.findViewById(R.id.etPassword);
        final Button btLogin = (Button) rootView.findViewById(R.id.btLogin);
        Button btlink = (Button) rootView.findViewById(R.id.btlink);

并使用getActivity().

Return the Activity this fragment is currently associated with.

new Intent(getActivity(), registar.class);

代码将

        Intent registerIntent;
        registerIntent = new Intent(getActivity(), registar.class);
        getActivity().startActivity(registerIntent); 

改变

Intent registerIntent;
                registerIntent = new Intent(login.this, registar.class);
                login.this.startActivity(registerIntent);

 Intent   registerIntent = new Intent(getActivity(), registar.class);
        startActivity(registerIntent);

还需要改变

 final EditText etUtilizador = (EditText) rootView.findViewById(R.id.etUtilizador);
 final EditText etPassword = (EditText) rootView.findViewById(R.id.etPassword);
 final Button btLogin = (Button) rootView.findViewById(R.id.btLogin);
public class login extends Fragment{
Context context;    
@Override
onAttach(Activity activity){
context=(Context)activity;}
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
...
 registerIntent = new Intent(context, registar.class);