Android片段没有替换

时间:2017-01-12 18:30:21

标签: android android-fragments

我开发了2个xml活动(登录,注册) 我想要2个按钮用两个xml活动交换片段

这是我的代码,它没有任何错误,但没有任何反应

Button btn1,btn2;
Fragment loginfrag,signupfrag;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn1 = (Button) findViewById(R.id.login_button);
    btn2 = (Button) findViewById(R.id.signup_button);
    loginfrag = new loginfr();
    signupfrag = new signupfr();
    btn1.setOnClickListener(this);
    btn2.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    if (view == btn1)
    {
        ft.replace(R.id.fragment_screen,loginfrag);
        ft.commit();
        Toast.makeText(this,"login",Toast.LENGTH_LONG);
    }
    else if (view == btn2){
        ft.replace(R.id.fragment_screen,signupfrag);
        ft.commit();
        Toast.makeText(this,"signup",Toast.LENGTH_LONG);

    }
}

这是登录活动

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">

    <LinearLayout
        android:id="@+id/tab1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingRight="24dp"
            android:weightSum="1">

            <!-- Email Label -->
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/Email"
                android:hint="Eamil"/>

            <!-- Password Label -->
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/password"
                android:hint="password"/>

            <Button
                android:id="@+id/btn_login"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="24dp"
                android:layout_marginBottom="24dp"
                android:layout_gravity="center"
                android:textAlignment="center"
                android:text="تسجيل الدخول"/>

            <TextView android:id="@+id/link_signup"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="24dp"
                android:text="ليس لديك حساب؟سجل الآن"
                android:textColor="#f000"
                android:gravity="center"
                android:textSize="16dip"/>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

注册活动

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">

    <LinearLayout
        android:id="@+id/tab1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="56dp"
            android:paddingLeft="24dp"
            android:paddingRight="24dp"
            android:weightSum="1">


            <!-- Email Label -->
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/Email_signup"
                android:hint="Eamil"/>

            <!-- Password Label -->
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/password_signup"
                android:hint="password"/>

            <Button
                android:id="@+id/btn_signup"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="24dp"
                android:layout_marginBottom="24dp"
                android:layout_gravity="center"
                android:textAlignment="center"
                android:text="تسجيل عضوية جديدة "/>

            <TextView android:id="@+id/link_login"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="24dp"
                android:text=" لديك حساب؟قم بتسجيل الدخول"
                android:textColor="#f000"
                android:gravity="center"
                android:textSize="16dip"/>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

在主要活动中这是xml

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/buttons_container"
        android:layout_below="@id/line0">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/signup_button"
            android:layout_weight="1"
            android:text="التسجيل"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/login_button"
            android:layout_weight="1"
            android:text="تسجيل الدخول" />
    </LinearLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_below="@id/buttons_container"
        android:id="@+id/relativeLayout">

        <fragment
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.example.amr.testapp.loginfr"
            tools:layout="@layout/login"
            android:id="@+id/fragment_screen"
          android:layout_below="@id/buttons_container" />
    </RelativeLayout>

2 个答案:

答案 0 :(得分:0)

根本不要使用静态(xml定义的)Fragment。将<fragment />标记替换为FrameLayout中的activity_main.xml,并动态加载您的片段。

修改:我还注意到您应该尝试将view == btn1替换为view.getId() == R.id.login_button作为示例。

修改2 要在Fragment首次加载时动态加载Activity,请将以下内容添加到onCreate()

if(getFragmentManager().findFragmentById(R.id.fragment_screen) == null) {
    getFragmentManager()
        .beginTransaction()
        .add(R.id.fragment_screen, loginfrag)
        .commit();
}

答案 1 :(得分:0)

使用此代码替换片段:

    @Override
public void onClick(View v) {
    Category fragment2 = new Category();
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.homeFrag, fragment2,"Home").addToBackStack("Home");
    fragmentTransaction.commit();
}