用home片段替换登录片段

时间:2018-04-17 15:43:49

标签: java android android-studio android-fragments

我是编程android j的新手想要使用片段,但是当我们点击连接j时我们想用片段home替换片段登录无法得到任何想法

我必须尝试很多技巧,但我无法得到我想要的东西

这是我所拥有的代码。 Class MainActivity:

package com.cuveri.mobile.vue;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.widget.TextView;
import android.widget.*;
import com.cuveri.mobile.controleur.LoginFragment;
import com.example.symameri.mhis.R;
import static com.example.symameri.mhis.R.layout.activity_Main;
public class MainActivity extends FragmentActivity{


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(activity_Main);
    LoginFragment fragment = (LoginFragment) getFragmentManager().
            findFragmentById(R.id.logFragment);






}





/* public void navigatetoHomeActivity(){
    Intent homeIntent = new 
Intent(getApplicationContext(),HomeActivity.class);
    homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(homeIntent);
    finish();
}*/

}

布局执行力_主要:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingLeft= 
"@android:dimen/app_icon_size"
android:paddingRight="@android:dimen/app_icon_size"
android:paddingTop="@android:dimen/app_icon_size"
android:paddingBottom = "@android:dimen/app_icon_size" tools:context = 
"com.cuveri.mobile.vue.MainActivity"
android:background="#e2dfdf">

<?xml version="1.0" encoding="utf-8"?>
<fragment
    android:id="@+id/logFragment"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.cuveri.mobile.controleur.LoginFragment"/>

<fragment
    android:id="@+id/hFragment"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.cuveri.mobile.controleur.HomeFragment"/>

</RelativeLayout>

Class Login_Fragment

package com.cuveri.mobile.controleur;


import android.app.Fragment;
import android.app.ProgressDialog;
import android.os.*;

import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.*;
import android.view.ViewGroup;
import android.widget.Button;
 import android.widget.EditText;
 import android.widget.TextView;

import com.cuveri.mobile.vue.MainActivity;
import com.example.symameri.mhis.R;

import java.io.IOException;

import ca.mimic.oauth2library.OAuth2Client;
import ca.mimic.oauth2library.OAuthError;
import ca.mimic.oauth2library.OAuthResponse;

public class LoginFragment extends Fragment{
//code le comportement du login
ProgressDialog prgDialog;
TextView errorMsg;
EditText identify;
EditText password;
Button connexion;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.login_fragment, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    errorMsg = (TextView) view.findViewById(R.id.login_error);
    password =(EditText) view.findViewById(R.id.Mot_de_passe);
    identify=(EditText) view.findViewById(R.id.Identifiant);
    connexion =(Button) view.findViewById(R.id.button_connexion) ;

    if (android.os.Build.VERSION.SDK_INT > 9)
    {
        StrictMode.ThreadPolicy policy = new 
    StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }
    }
    public void loginUser (View view) {
    String username = identify.getText().toString();
    String passwords = password.getText().toString();


    OAuth2Client client = new OAuth2Client.Builder("macuverie_mobile", 
"appmobile", "http://10.0.2.2:8080/oauth/token")

.grantType("password").username(username).password(passwords).build();

    OAuthResponse response = null;
    try {
        response = client.requestAccessToken();
    } catch (IOException e) {
        e.printStackTrace();
        //Toast.makeText(this.getContext(), e.getMessage(), 
  Toast.LENGTH_LONG).show();

    }

    String accessToken = "";
    if (response.isSuccessful()) {
        accessToken = response.getAccessToken();
       // Toast.makeText(getApplicationContext(), "acces_token" + 
 accessToken, Toast.LENGTH_LONG).show();
       new MainActivity().navigatetoHomeActivity();
        //String refreshToken = response.getRefreshToken();
    } else {
        OAuthError error = response.getOAuthError();
        String errorMsg = error.getError();
        //Toast.makeText(getApplicationContext(), errorMsg, 
  Toast.LENGTH_LONG).show();
    }


  }


  }

Class Home_Fragment:

package com.cuveri.mobile.controleur;

 import android.os.Bundle;
 import android.support.annotation.Nullable;
 import android.support.v4.app.Fragment;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;

 import com.example.symameri.mhis.R;

 public class HomeFragment extends Fragment{
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.home_fragment, container, false);
}
}

2 个答案:

答案 0 :(得分:0)

解决此问题的最佳方法是使用FrameLayout替换Fragment小部件,并且需要将此最后一个小部件用作容器。

之后,您可以使用FragmentTransation在运行时添加,替换或删除片段。

https://developer.android.com/reference/android/app/FragmentTransaction.html

答案 1 :(得分:0)

有太多的选择,但你不能使用任何一个。

由于"Separate of Concern Principle""Single-responsibility Principle"

让我解释一下:Login和Home片段属于不同的业务逻辑。所以两者都必须有不同的活动。在那之后如果你不想重复使用这些活动的某些部分,那么你实际上不需要碎片。