Untitled
unknown
plain_text
a year ago
12 kB
3
Indexable
Never
//Mainiferst <activity android:name=".LoginActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".FirstViewActivity" android:exported="true" /> <activity android:name=".MainActivity" android:exported="true" android:theme="@style/Theme.Home"> </activity> // public class FirstViewActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_first_view); EditText nameStudio = findViewById(R.id.txtNameStudio); Button btnSaveNameStudio = findViewById(R.id.btnSaveNameStudio); SharedPreferences sharedPrefs = getSharedPreferences("Pref", Context.MODE_PRIVATE); btnSaveNameStudio.setOnClickListener(view -> { String name = nameStudio.getText().toString().trim(); if (!name.isEmpty()) { // Lưu tên vào SharedPreferences và đánh dấu đã nhập tên SharedPreferences.Editor editor = sharedPrefs.edit(); editor.putString("nameStudio", name); editor.putInt("checkName", 1); editor.apply(); // Chuyển đến màn hình trang chủ Intent intent = new Intent(this, LoginActivity.class); startActivity(intent); finish(); } }); } } //HomeFragment //NotificationFragment public class LoginActivity extends AppCompatActivity { Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); EditText pass = findViewById(R.id.etxtPassword); TextView error = findViewById(R.id.txtErrorPass); Button btn = findViewById(R.id.button); TextView txt = findViewById(R.id.txthello); SharedPreferences sharedPrefs = getSharedPreferences("Pref", Context.MODE_PRIVATE); int check = sharedPrefs.getInt("checkName", 0); if (check == 0) { // Chưa nhập tên, chuyển đến màn hình nhập tên Intent intent = new Intent(this, FirstViewActivity.class); startActivity(intent); finish(); } else { String hello = "hello " + sharedPrefs.getString("nameStudio", ""); txt.setText(hello); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String password = pass.getText().toString().trim(); if(password.equals("123")){ Intent intent1 = new Intent(getApplicationContext(), MainActivity.class); startActivity(intent1); finish(); }else{ error.setVisibility(View.VISIBLE); error.setText("Sai mật khẩu. Vui lòng thử lại."); } } }); pass.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View view, int i, KeyEvent keyEvent) { error.setVisibility(View.GONE); return false; } }); } } } // public class MainActivity extends AppCompatActivity { private BottomNavigationView bottomNavigationView; boolean isDarkModeEnabled; @Override protected void onCreate(Bundle savedInstanceState) { SharedPreferences sharedPreferences = getSharedPreferences("Dark_Mode", Context.MODE_PRIVATE); isDarkModeEnabled= sharedPreferences.getBoolean("isDarkModeEnabled", false); // Áp dụng trạng thái chế độ tối/sáng if (isDarkModeEnabled) { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); } else { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); } super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bottomNavigationView = findViewById(R.id.bottomNavigationView); bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { switch (menuItem.getItemId()) { case R.id.menu_home: loadFragment(new HomeFragment()); return true; case R.id.menu_notifications: loadFragment(new NotificationFragment()); return true; } return false; } }); // Mặc định, hiển thị HomeFragment khi ứng dụng khởi động if (savedInstanceState == null) { loadFragment(new HomeFragment()); } } private void loadFragment(Fragment fragment) { FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.fragment_container, fragment); transaction.commit(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.options_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); switch (id) { case R.id.action_dark_mode: // Đảo ngược trạng thái chế độ tối isDarkModeEnabled = !isDarkModeEnabled; // Áp dụng trạng thái chế độ tối if (isDarkModeEnabled) { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); } else { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); } saveDarkModeState(isDarkModeEnabled); // Khởi động lại Activity để áp dụng chế độ tối/sáng recreate(); return true; default: return super.onOptionsItemSelected(item); } } private void saveDarkModeState(boolean isDarkModeEnabled) { SharedPreferences sharedPreferences = getSharedPreferences("Dark_Mode", Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putBoolean("isDarkModeEnabled", isDarkModeEnabled); editor.apply(); } } //main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" style="@style/screen"> <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/bottomNavigationView" /> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottomNavigationView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" app:menu="@menu/bottom_navigation_menu" app:labelVisibilityMode="labeled" android:background="@color/material_on_surface_stroke"/> </RelativeLayout> //menu/bottom_navigation_menu <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:iconTint="@color/design_default_color_background" android:id="@+id/menu_home" android:icon="@drawable/ic_home" android:title="Nhà" /> <item android:id="@+id/menu_notifications" android:icon="@drawable/ic_notification" android:title="Thông báo" /> </menu> //menu/_menu.xml <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/action_dark_mode" android:title="dark" android:icon="@drawable/ic_dark" app:showAsAction="always"/> </menu> //color.xml <color name="xanhss">#2085FE</color> <color name="dark">#212121</color> //themes <resources xmlns:tools="http://schemas.android.com/tools"> <!-- Base application theme. --> <style name="Theme.Home" parent="Theme.MaterialComponents.Light"> <!-- Primary brand color. --> <item name="colorPrimary">@color/xanhss</item> <item name="colorPrimaryVariant">@color/purple_700</item> <item name="colorOnPrimary">@color/white</item> <!-- Secondary brand color. --> <item name="colorSecondary">@color/teal_200</item> <item name="colorSecondaryVariant">@color/teal_700</item> <item name="colorOnSecondary">@color/black</item> <!-- Status bar color. --> <item name="android:statusBarColor">?attr/colorPrimaryVariant</item> <!-- Customize your theme here. --> </style> <style name="screen"> <item name="android:background">@color/white</item> </style> </resources> //homefragment.xml <?xml version="1.0" encoding="utf-8"?> <FrameLayout 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" tools:context=".HomeFragment" style="@style/screen"> <!-- TODO: Update blank fragment layout --> <EditText android:id="@+id/txtDen" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="hello_blank_fragment_home" android:layout_marginTop="50dp" android:layout_marginBottom="0dp" android:padding="16dp" android:layout_marginHorizontal="20dp"/> <Button android:id="@+id/btnSend" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center" android:layout_marginBottom="550dp" android:text="send"/> </FrameLayout> //homefragment public class HomeFragment extends Fragment { private EditText editText; private Button button; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { // Inflate layout for this fragment View view = inflater.inflate(R.layout.fragment_home, container, false); // Initialize views editText = view.findViewById(R.id.txtDen); button = view.findViewById(R.id.btnSend); FirebaseApp.initializeApp(getContext()); // Lấy tham chiếu đến Firebase Realtime Database FirebaseDatabase database = FirebaseDatabase.getInstance(); DatabaseReference myRef = database.getReference("DEN"); // Set click listener for the button button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Get the text from EditText String content = editText.getText().toString().trim(); if (!content.isEmpty()) { // Send the content to Firebase or perform any other action // You can add your Firebase logic here myRef.setValue(content); // Clear the EditText editText.setText(""); // Show a toast message Toast.makeText(getActivity(), "Content sent to Firebase", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getActivity(), "Please enter content before sending", Toast.LENGTH_SHORT).show(); } } }); return view; } }