diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..a684833 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +AndroidApp \ No newline at end of file diff --git a/.idea/dbnavigator.xml b/.idea/dbnavigator.xml new file mode 100644 index 0000000..d9b9424 --- /dev/null +++ b/.idea/dbnavigator.xml @@ -0,0 +1,420 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 6f18ffb..e96e1da 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -41,8 +41,18 @@ dependencies { implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1' implementation 'androidx.navigation:navigation-fragment:2.4.1' implementation 'androidx.navigation:navigation-ui:2.4.1' + implementation 'com.google.android.material:material:1.5.0' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' +} + +dependencies { + def room_version = "2.2.0-alpha01" + + implementation "androidx.room:room-runtime:$room_version" + annotationProcessor "androidx.room:room-compiler:$room_version" // For Kotlin use kapt instead of annotationProcessor + // Test helpers + testImplementation "androidx.room:room-testing:$room_version" } \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c6f0d7a..d74dbc3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -11,9 +11,6 @@ android:supportsRtl="true" android:theme="@style/Theme.MyApplication" tools:targetApi="31"> - diff --git a/app/src/main/java/cc/liuyx/app/LoginActivity.java b/app/src/main/java/cc/liuyx/app/LoginActivity.java index 3edf395..fea5365 100644 --- a/app/src/main/java/cc/liuyx/app/LoginActivity.java +++ b/app/src/main/java/cc/liuyx/app/LoginActivity.java @@ -1,11 +1,11 @@ package cc.liuyx.app; +import android.annotation.SuppressLint; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.widget.EditText; -import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; @@ -23,21 +23,19 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList passwdEdit = findViewById(R.id.login_passwd); findViewById(R.id.btn_login).setOnClickListener(this); - TextView textView = findViewById(R.id.click_register); - textView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - startActivity(new Intent(LoginActivity.this, RegisterActivity.class)); - } - }); + findViewById(R.id.click_register).setOnClickListener(this); } + @SuppressLint("NonConstantResourceId") @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_login: login(); break; + case R.id.click_register: + startActivity(new Intent(LoginActivity.this, RegisterActivity.class)); + break; } } @@ -59,7 +57,7 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList editor.putString("username", username); editor.apply(); - Intent intent = new Intent(LoginActivity.this, UserInfoActivity.class); + Intent intent = new Intent(LoginActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } else { diff --git a/app/src/main/java/cc/liuyx/app/MainActivity.java b/app/src/main/java/cc/liuyx/app/MainActivity.java index 84fbb13..0cfec62 100644 --- a/app/src/main/java/cc/liuyx/app/MainActivity.java +++ b/app/src/main/java/cc/liuyx/app/MainActivity.java @@ -1,45 +1,83 @@ package cc.liuyx.app; import android.annotation.SuppressLint; +import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; -import android.util.Log; -import android.view.View; +import android.view.MenuItem; +import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; -import androidx.fragment.app.FragmentManager; -import androidx.fragment.app.FragmentTransaction; +import androidx.fragment.app.Fragment; + +import com.google.android.material.bottomnavigation.BottomNavigationView; +import com.google.android.material.navigation.NavigationBarView; -import cc.liuyx.app.fragment.MainFragment; import cc.liuyx.app.fragment.InfoFragment; +import cc.liuyx.app.fragment.MainFragment; public class MainActivity extends AppCompatActivity { + private BottomNavigationView bottomNavigationView; + private MainFragment mainFragment; + private InfoFragment infoFragment; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - SharedPreferences preferences = getSharedPreferences("loginStatus", MODE_PRIVATE); - Log.i("log", preferences.getString("username", "no name")); + login(); + + if (savedInstanceState == null) { + getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new MainFragment()).commit(); + } + + bottomNavigationView = findViewById(R.id.bottom_nav); + mainFragment = new MainFragment(); + infoFragment = new InfoFragment(); + initView(); } - @SuppressLint("NonConstantResourceId") - public void selectFragment(View view) { - FragmentManager fm = getSupportFragmentManager(); - FragmentTransaction ft = fm.beginTransaction(); - - MainFragment firstFragment = new MainFragment(); - InfoFragment infoFragment = new InfoFragment(); - - switch (view.getId()) { - case R.id.page_main: - ft.replace(R.id.page_blank, firstFragment); - break; - case R.id.page_info: - ft.replace(R.id.page_blank, infoFragment); - break; + /** + * 判断用户是否已经登录,如果未登录,则跳转至登录页面 + */ + private void login() { + SharedPreferences preferences = getSharedPreferences("loginStatus", MODE_PRIVATE); + boolean isLogin = preferences.getBoolean("isLoggedIn", false); + System.out.println(isLogin); + if (!isLogin) { + // 跳转到 LoginActivity + Intent intent = new Intent(MainActivity.this, LoginActivity.class); + startActivity(intent); + finish(); } - ft.commit(); + } + + /** + * 设置底部主页导航栏的切换事件 + */ + private void initView() { + bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() { + @SuppressLint("NonConstantResourceId") + @Override + public boolean onNavigationItemSelected(@NonNull MenuItem item) { + Fragment fragment = null; + String tag = null; + switch (item.getItemId()) { + case R.id.home: + fragment = mainFragment; + tag = "fm_main"; + break; + case R.id.me: + fragment = infoFragment; + tag = "fm_info"; + break; + } + assert fragment != null; + getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment, tag).commit(); + return true; + } + }); } } \ No newline at end of file diff --git a/app/src/main/java/cc/liuyx/app/UserInfoActivity.java b/app/src/main/java/cc/liuyx/app/UserInfoActivity.java deleted file mode 100644 index a51c414..0000000 --- a/app/src/main/java/cc/liuyx/app/UserInfoActivity.java +++ /dev/null @@ -1,65 +0,0 @@ -package cc.liuyx.app; - -import android.annotation.SuppressLint; -import android.content.Intent; -import android.content.SharedPreferences; -import android.os.Bundle; -import android.view.View; -import android.widget.Button; -import android.widget.TextView; - -import androidx.appcompat.app.AppCompatActivity; - -public class UserInfoActivity extends AppCompatActivity implements View.OnClickListener { - - private String username = ""; - private String phone = ""; - private String email = ""; - - @SuppressLint("SetTextI18n") - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_user_info); - - // 判断用户是否已经登录 - // 如果未登录,则跳转至登录页面 - SharedPreferences preferences = getSharedPreferences("loginStatus", MODE_PRIVATE); - boolean isLogin = preferences.getBoolean("isLoggedIn", false); - System.out.println(isLogin); - if (!isLogin) { - // 跳转到 LoginActivity - Intent intent = new Intent(UserInfoActivity.this, LoginActivity.class); - startActivity(intent); - finish(); - return; - } - - username = preferences.getString("username", null); - email = preferences.getString("email", null); - phone = preferences.getString("phone", null); - - TextView userName = findViewById(R.id.user_username); - TextView userPhone = findViewById(R.id.user_phone); - TextView userEmail = findViewById(R.id.user_email); - userName.setText(userName.getText() + username); - userPhone.setText(userPhone.getText() + phone); - userEmail.setText(userEmail.getText() + email); - - Button btnLogout = findViewById(R.id.logout); - btnLogout.setOnClickListener(this); - } - - @Override - public void onClick(View view) { - if (view.getId() == R.id.logout) { - SharedPreferences preferences = getSharedPreferences("loginStatus", MODE_PRIVATE); - SharedPreferences.Editor edit = preferences.edit(); - edit.remove("isLoggedIn"); - edit.apply(); - - finish(); - startActivity(new Intent(this, LoginActivity.class)); - } - } -} \ No newline at end of file diff --git a/app/src/main/java/cc/liuyx/app/dao/UserDao.java b/app/src/main/java/cc/liuyx/app/dao/UserDao.java new file mode 100644 index 0000000..cbe0037 --- /dev/null +++ b/app/src/main/java/cc/liuyx/app/dao/UserDao.java @@ -0,0 +1,10 @@ +package cc.liuyx.app.dao; + +import androidx.room.Dao; +import androidx.room.Insert; + +@Dao +public interface UserDao { + @Insert + void insert(); +} diff --git a/app/src/main/java/cc/liuyx/app/entity/User.java b/app/src/main/java/cc/liuyx/app/entity/User.java new file mode 100644 index 0000000..8f51de8 --- /dev/null +++ b/app/src/main/java/cc/liuyx/app/entity/User.java @@ -0,0 +1,52 @@ +package cc.liuyx.app.entity; + +import androidx.room.ColumnInfo; +import androidx.room.Entity; +import androidx.room.PrimaryKey; + +@Entity(tableName = "user") +public class User { + + @PrimaryKey(autoGenerate = true) + private int id; + @ColumnInfo(name = "name") + private String name; + @ColumnInfo(name = "passwd") + private String passwd; + @ColumnInfo(name = "phone") + private String phone; + @ColumnInfo(name = "email") + private String email; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPasswd() { + return passwd; + } + + public void setPasswd(String passwd) { + this.passwd = passwd; + } + + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } +} diff --git a/app/src/main/java/cc/liuyx/app/fragment/InfoFragment.java b/app/src/main/java/cc/liuyx/app/fragment/InfoFragment.java index 38631bd..f332c0c 100644 --- a/app/src/main/java/cc/liuyx/app/fragment/InfoFragment.java +++ b/app/src/main/java/cc/liuyx/app/fragment/InfoFragment.java @@ -1,12 +1,22 @@ package cc.liuyx.app.fragment; +import static android.content.Context.MODE_PRIVATE; + +import android.annotation.SuppressLint; +import android.content.Intent; +import android.content.SharedPreferences; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.TextView; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; +import cc.liuyx.app.LoginActivity; +import cc.liuyx.app.MainActivity; import cc.liuyx.app.R; public class InfoFragment extends Fragment { @@ -16,4 +26,43 @@ public class InfoFragment extends Fragment { Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_info, container, false); } + + @SuppressLint("SetTextI18n") + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + // 获取所处的Activity + MainActivity mainActivity = (MainActivity) getActivity(); + assert mainActivity != null; + + // 为登出按钮绑定事件 + mainActivity.findViewById(R.id.logout).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + if (view.getId() == R.id.logout) { + SharedPreferences preferences = mainActivity.getSharedPreferences("loginStatus", MODE_PRIVATE); + SharedPreferences.Editor edit = preferences.edit(); + edit.remove("isLoggedIn"); + edit.apply(); + + mainActivity.finish(); + startActivity(new Intent(mainActivity, LoginActivity.class)); + } + } + }); + + // 获取并设置页面信息 + SharedPreferences preferences = getActivity().getSharedPreferences("loginStatus", MODE_PRIVATE); + String username = preferences.getString("username", null); + String email = preferences.getString("email", null); + String phone = preferences.getString("phone", null); + TextView userName = mainActivity.findViewById(R.id.user_username); + TextView userPhone = mainActivity.findViewById(R.id.user_phone); + TextView userEmail = mainActivity.findViewById(R.id.user_email); + userName.setText(userName.getText() + username); + userPhone.setText(userPhone.getText() + phone); + userEmail.setText(userEmail.getText() + email); + + } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 6737081..ac18643 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,43 +1,25 @@ - - - - - - + android:layout_height="match_parent" /> - - - + - \ No newline at end of file + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_user_info.xml b/app/src/main/res/layout/activity_user_info.xml deleted file mode 100644 index 88918c4..0000000 --- a/app/src/main/res/layout/activity_user_info.xml +++ /dev/null @@ -1,203 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -