활동에서 조각 클래스를 호출해야합니다. 활동에서 조각 클래스를 호출하기 전에 사용자 이름과 암호를 확인해야합니다. 로그인 버튼을 클릭하면 프래그먼트 클래스가로드되어야합니다. 그러나 로그인 레이아웃은 조각 레이아웃과 겹칩니다. 겹침을 제거하는 방법.
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>
이에 대한 두 가지 솔루션이 있습니다.
1. 하나의 활동을 사용하십시오. 이를 위해 하나는 로그인 용이고 다른 하나는 afterlogin 용으로 두 개의 조각을 만든 다음 기본적으로 로그인 fragmnet을 활동 컨테이너 ID에 추가하고 로그인 후 afterlogin 조각으로 바꿉니다.
2. 두 개의 활동을 사용하십시오 (하나는 로그인을위한 것이고 다른 하나는 loginafter 프래그먼트를 포함 함) : 이것을 위해 로그인 후에 두 번째 활동을 호출하고 첫 번째 활동을 finish ()하십시오.
희망, 작동합니다!
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다