如何从活动中调用片段

时间:2014-10-25 05:59:52

标签: android

我必须从一个活动中调用一个片段类。在活动中,我必须在调用片段类之前检查用户名和密码。当我单击登录按钮时,必须加载片段类。但是,登录布局与片段布局重叠。如何删除重叠。

login.java

public class Login extends Activity {

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        } 
    }



  public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.login, container,
                    false);

             return rootView;
        }

        public void onStart(){
            super.onStart();


               final Button loginButton = (Button) getActivity().findViewById(R.id.loginButton);

              loginButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    EditText editText1 = (EditText) getActivity().findViewById(R.id.editText1);
                      EditText editText2 = (EditText) getActivity().findViewById(R.id.editText2);
                      final String username = editText1.getText().toString();
                      final String password = editText2.getText().toString();

                     if (username.equals("user") && password.equals("user")){

                          Fragment1 fragment1 = new Fragment1();

                          getFragmentManager().beginTransaction()
                          .replace(android.R.id.content, fragment1).commit();

                         }       
                }
              });


}


    }
}

Fragment1.java

public class Fragment1 extends Fragment{
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.fragment1, container,false);

} }

login.xml

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

<LinearLayout android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"        
android:text="Username" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >

<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"        
android:text="Password" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >

</EditText>
</LinearLayout>

<Button
android:id="@+id/login"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Login" />

</LinearLayout>

fragment1.xml

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

<Button
android:id="@+id/login"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="goto"
android:text="Go to Main Activity" />

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

而不是

getFragmentManager().beginTransaction().add(android.R.id.content, fragment1).commit();

使用

getFragmentManager().beginTransaction().replace(android.R.id.content, fragment1).commit();

android.R.id.content显示整个容器。您正在尝试向已添加的内容添加内容。您应该使用replace,这将删除旧内容并添加新内容

修改

public class Login extends Activity
 implements PlaceholderFragment.LoginInterface{

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    } 
}


 public void login(){
              getFragmentManager().beginTransaction()
                .replace(R.id.container, new Fragment1()).commit();

 }



public static class PlaceholderFragment extends Fragment {

    public interface LoginInterface{
           public void login();
    }

    loginInterface callback;
    public PlaceholderFragment(loginInterface cb) {
      this.callback = cb;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.login, container,
                false);

         return rootView;
    }

    public void onStart(){
        super.onStart();


           final Button loginButton = (Button) getActivity().findViewById(R.id.loginButton);

          loginButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                EditText editText1 = (EditText) getActivity().findViewById(R.id.editText1);
                  EditText editText2 = (EditText) getActivity().findViewById(R.id.editText2);
                  final String username = editText1.getText().toString();
                  final String password = editText2.getText().toString();

                 if (username.equals("user") && password.equals("user")){
                       this.callback.login();

                     }       
            }
          });

}
}
}

答案 1 :(得分:1)

您有两种解决方案:

<强> 1。使用一个活动:为此创建两个片段,一个用于登录,另一个用于afterlogin,然后默认情况下将登录fragmnet添加到Activity容器ID,并在登录后用afterlogin片段替换。

2。使用两个活动(一个用于登录,一个用于包含后面的片段):为此后登录调用第二个活动并完成()第一个活动。

希望,这会奏效!