Bonjour , je viens vers vous car je suis un peu perdu entre le code java pour une activité , et le passage a une version avec fragment...
je découvre petit a petit se monde et j’évolue a coup de tuto et autres, mais la je ne trouve pas quelque choses de clair sur la façon de faire ....
je me suis fais une première appli avec des bouton pour passer d'une activité a une autre , pas de soucis , c'est plutôt simple... ce sont 3 activité de calcul.
Voici un des code java qui fonctionne plutôt/presque bien...
maintenant je voulais faire une appli un peu plus stylé et je me suis lancé dans une version avec un bottommenu des fragment ... et la je sèche ...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.ItemTouchUIUtil; import android.icu.text.DecimalFormat; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import com.google.android.gms.ads.AdListener; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdView; public class Extrudeur extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_extrudeur); Button calcul; final EditText longa, longb, longc, stepa; final TextView stepb; longa = findViewById(R.id.longa); longb = findViewById(R.id.longb); longc = findViewById(R.id.longc); stepa = findViewById(R.id.stepa); stepb = findViewById(R.id.stepb); calcul = findViewById(R.id.calcul); calcul.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { double longanum = Double.parseDouble(longa.getText().toString()); double longbnum = Double.parseDouble(longb.getText().toString()); double longcnum = Double.parseDouble(longc.getText().toString()); double stepanum = Double.parseDouble(stepa.getText().toString()); double calcul1 = longbnum - longanum; double calcul2 = calcul1 * stepanum; double calcul3 = longbnum - longcnum; double resultat = calcul2 / calcul3; DecimalFormat resultatfinal = new DecimalFormat("#.##"); // application creer par Fabian Defossez - 2019 stepb.setText("le step est de \n" + resultatfinal.format(resultat)); } }); } }
je ne trouve pas comment intégrer mon code précédent , j'ai tenté dans le mainactivity
dans le fragment Java que android studio ma créer , mais rien a faire je ne trouve pas ou ni comment le faire fonctionner ???
j'ai ceci comme code java attaché à mon fragment :
et voici a quoi ressemble mon Mainactivity
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 import android.os.Bundle; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * A simple {@link Fragment} subclass. */ public class ExtrudeurFragment extends Fragment { public ExtrudeurFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_extrudeur, container, false); } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 package com.example.menubas; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentTransaction; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.FrameLayout; import com.google.android.material.bottomnavigation.BottomNavigationView; public class MainActivity extends AppCompatActivity { private BottomNavigationView mMainNav; private FrameLayout mMainFrame; private HomeFragment homeFragment; private ExtrudeurFragment extrudeurFragment; private DebitFragment debitFragment; private AxesFragment axesFragment; private InfosFragment infosFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mMainFrame = (FrameLayout) findViewById(R.id.frame_nav); mMainNav = (BottomNavigationView) findViewById(R.id.main_nav); homeFragment = new HomeFragment(); extrudeurFragment = new ExtrudeurFragment(); debitFragment = new DebitFragment(); axesFragment = new AxesFragment(); infosFragment = new InfosFragment(); setFragment(homeFragment); mMainNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item){ switch (item.getItemId()){ case R.id.home : mMainNav.setItemBackgroundResource(R.color.secondaryColor); setFragment(homeFragment); return true; case R.id.extrudeur : mMainNav.setItemBackgroundResource(R.color.secondaryLightColor); setFragment(extrudeurFragment); return true; case R.id.debit : mMainNav.setItemBackgroundResource(R.color.secondaryColor); setFragment(debitFragment); return true; case R.id.axes : mMainNav.setItemBackgroundResource(R.color.secondaryLightColor); setFragment(axesFragment); return true; case R.id.infos : mMainNav.setItemBackgroundResource(R.color.secondaryColor); setFragment(infosFragment); return true; default: return false; } } }); } private void setFragment(Fragment fragment) { FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction.replace(R.id.frame_nav,fragment); fragmentTransaction.commit(); } public void youtube2(View view) { Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/c/Egalistel")); startActivity(browserIntent); } public void calibration(View view) { Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/watch?v=Io2DHaSRVGA")); startActivity(browserIntent); } }
je pensais récupérer le fichier fonctionnel et faire un genre d'include mais idem je ne trouve pas vraiment de réponse à savoir si cela est possible ???
en vous remerciant d'avance
Partager