注册(Home)
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top">
<TextView
android:id="@+id/text_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
android:layout_columnSpan="4"
android:layout_gravity="fill"
android:gravity="center"
android:layout_column="0"
android:layout_row="0"
/>
<TextView android:id="@+id/home_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户注册"
android:layout_column="0"
android:layout_row="1"
android:layout_columnSpan="4"
android:layout_gravity="fill"
android:gravity="center"
/>
<TextView android:id="@+id/school"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学院"
android:layout_column="0"
android:layout_row="2"
/>
<EditText android:id="@+id/school_edit"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_column="1"
/>
<TextView android:id="@+id/classes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="班级"
android:layout_column="2"
android:layout_row="2"
/>
<EditText android:id="@+id/classes_edit"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_row="2"
/>
<TextView android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名"
android:layout_column="0"
android:layout_row="3"
/>
<EditText android:id="@+id/username_edit"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="3"
android:layout_gravity="fill"
/>
<TextView android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名"
android:layout_column="2"
android:layout_row="3"
/>
<EditText android:id="@+id/name_edit"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_row="3"
/>
<TextView android:id="@+id/sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="性别"
android:layout_column="0"
android:layout_row="4"
/>
<RadioGroup
android:layout_row="4"
android:layout_column="1"
android:orientation="horizontal"
android:layout_gravity="center"
>
<RadioButton
android:id="@+id/man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"/>
<RadioButton
android:id="@+id/woman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
</RadioGroup>
<TextView android:id="@+id/minzu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="民族"
android:layout_column="2"
android:layout_row="4"
/>
<RadioGroup
android:layout_row="4"
android:layout_column="3"
android:orientation="horizontal"
android:layout_gravity="center"
>
<RadioButton
android:id="@+id/hanzu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="汉"/>
<RadioButton
android:id="@+id/manzu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="满"/>
</RadioGroup>
<TextView android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"
android:layout_column="0"
android:layout_row="5"
/>
<EditText android:id="@+id/password_edit"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="5"
/>
<TextView android:id="@+id/repassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认密码"
android:layout_column="2"
android:layout_row="5"
/>
<EditText android:id="@+id/repassword_edit"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_row="5"
/>
<TextView android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="手机号"
android:layout_column="0"
android:layout_row="7"
/>
<EditText android:id="@+id/phone_edit"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="7"
/>
<TextView android:id="@+id/birthday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="出生日期"
android:layout_column="2"
android:layout_row="7"
/>
<Button
android:id="@+id/birthday_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="birthday_onClick"
android:text="录入"/>
<TextView android:id="@+id/hobby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="业余爱好"
android:layout_column="0"
android:layout_row="8"
/>
<LinearLayout
android:layout_row="8"
android:layout_column="1"
android:layout_columnSpan="3"
android:orientation="horizontal"
android:layout_gravity="center_vertical">
<CheckBox
android:id="@+id/hobby1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="足球"/>
<CheckBox
android:id="@+id/hobby2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="手球"/>
<CheckBox
android:id="@+id/hobby3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="网球"/>
<CheckBox
android:id="@+id/hobby4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="篮球"/>
</LinearLayout>
<Button
android:id="@+id/zubutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
android:layout_column="0"
android:layout_row="9"
android:layout_columnSpan="4"
android:layout_gravity="fill"
/>
</GridLayout>
Java
public class HomeFragment extends Fragment {
private StringBuffer date;
private int year, month, day;
private void initDateTime() {
Calendar calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH) ;
day = calendar.get(Calendar.DAY_OF_MONTH);
//hour = calendar.get(Calendar.HOUR);
//minute = calendar.get(Calendar.MINUTE);
}
private HomeViewModel homeViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel.class);
final View root = inflater.inflate(R.layout.fragment_home, container, false);
final TextView textView = root.findViewById(R.id.text_home);
homeViewModel.getText().observe(this, new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText(s);
}
});
//注册按钮点击
Button zubutton=root.findViewById(R.id.zubutton);
//日期录入的点击
final Button birthday_onClick=root.findViewById(R.id.birthday_edit);
zubutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//学院
final TextView school_edit=root.findViewById(R.id.school_edit);
school_edit.setText(school_edit.getText().toString().trim());
//班级
final TextView classes_edit=root.findViewById(R.id.classes_edit);
classes_edit.setText(classes_edit.getText().toString().trim());
//用户名
final TextView username_edit=root.findViewById(R.id.username_edit);
username_edit.setText(username_edit.getText().toString().trim());
//姓名
final TextView name_edit=root.findViewById(R.id.name_edit);
name_edit.setText(name_edit.getText().toString().trim());
//性别
RadioButton man=root.findViewById(R.id.man);
RadioButton woman=root.findViewById(R.id.woman);
//民族
RadioButton hanzu=root.findViewById(R.id.hanzu);
RadioButton manzu=root.findViewById(R.id.manzu);
//密码
final TextView password_edit=root.findViewById(R.id.password_edit);
password_edit.setText(password_edit.getText().toString().trim());
//确认密码
final TextView repassword_edit=root.findViewById(R.id.repassword_edit);
repassword_edit.setText(repassword_edit.getText().toString().trim());
//手机号
final TextView phone_edit=root.findViewById(R.id.phone_edit);
phone_edit.setText(phone_edit.getText().toString().trim());
//爱好
CheckBox zq=root.findViewById(R.id.hobby1);
CheckBox sq=root.findViewById(R.id.hobby2);
CheckBox wq=root.findViewById(R.id.hobby3);
CheckBox lq=root.findViewById(R.id.hobby4);
if (school_edit.getText().toString().length()==0) ts("学院必填");
else if(classes_edit.getText().toString().length()==0) ts("班级必填");
else if(username_edit.getText().toString().length()==0) ts("用户名必填");
else if(name_edit.getText().toString().length()==0) ts("姓名必填");
else if (!man.isChecked()&&!woman.isChecked()) ts("性别必选");
else if (!hanzu.isChecked()&&!manzu.isChecked()) ts("民族必选");
else if(password_edit.getText().toString().length()==0) ts("密码必填");
else if(repassword_edit.getText().toString().length()==0) ts("确认密码必填");
else if (!repassword_edit.getText().toString().equals(password_edit.getText().toString())) ts("密码和确认密码不一致!");
else if(phone_edit.getText().toString().length()==0) ts("手机号必填");
else {
int ahs = 0;
String ah = "";
if (zq.isChecked()) {
ahs++;
ah = ah + zq.getText().toString() + ",";
}
if (sq.isChecked()) {
ahs++;
ah = ah + sq.getText().toString() + ",";
}
if (wq.isChecked()) {
ahs++;
ah = ah + wq.getText().toString() + ",";
}
if (lq.isChecked()) {
ahs++;
ah = ah + lq.getText().toString() + ",";
}
if (ahs < 1) ts("爱好至少选择一项");
else if (!birthday_onClick.getText().toString().equals("录入")){
SharedPreferences sp= getActivity().getSharedPreferences("userInfo", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("school",school_edit.getText().toString());
editor.putString("classes",classes_edit.getText().toString());
editor.putString("username",username_edit.getText().toString().trim());
editor.putString("name",name_edit.getText().toString().trim());
editor.putString("password",password_edit.getText().toString().trim());
if (man.isChecked()) editor.putString("sex","男");
else editor.putString("sex","女");
if (hanzu.isChecked()) editor.putString("minzu","汉");
else editor.putString("minzu","藏");
editor.putString("phone",phone_edit.getText().toString().trim());
editor.putString("birthday_onClick",birthday_onClick.getText().toString().trim());
editor.putString("hobby",ah);
editor.commit();
ts("注册成功");
//注册成功后,跳到登录页面
final NavController navController=
Navigation.findNavController(getActivity(),R.id.nav_host_fragment);
navController.navigate(R.id.navigation_dashboard);
}
else ts("请录入出生日期");
}
}
});
//录入出生日期
birthday_onClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setPositiveButton("录入", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
birthday_onClick.setText(String.valueOf(year)+"-"+String.valueOf(month+1)+"-"+String.valueOf(day));
dialog.dismiss();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
final AlertDialog dialog = builder.create();
View dialogView = View.inflate(getActivity(), R.layout.dialog_date, null);
final DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.datePicker);
initDateTime();
datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int pyear, int pmonth, int pday) {
year = pyear;
month = pmonth;
day = pday;
}
});
dialog.setTitle("录入日期");
dialog.setView(dialogView);
dialog.show();
}
});
return root;
}
public void ts(String st) {
Toast.makeText(getActivity(),st,
Toast.LENGTH_SHORT).show();
}
}
登录(Dashboard)
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top">
<TextView
android:id="@+id/text_dashboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
android:layout_column="0"
android:layout_row="0"
android:layout_columnSpan="2"
android:layout_gravity="fill_horizontal"/>
<TextView android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名"
android:layout_column="0"
android:layout_row="1"
/>
<EditText android:id="@+id/username_edit"
android:layout_width="100dp"
android:layout_height="wrap_content"
/>
<TextView android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"
android:layout_column="0"
android:layout_row="2"
/>
<EditText android:id="@+id/password_edit"
android:layout_width="100dp"
android:inputType="textPassword"
android:layout_height="wrap_content"
/>
<Button android:id="@+id/login"
android:layout_column="0"
android:layout_row="3"
android:layout_columnSpan="2"
android:layout_gravity="fill_horizontal"
android:text="登录"/>
</GridLayout>
Java
import static android.content.Context.MODE_PRIVATE;
public class DashboardFragment extends Fragment {
private SharedPreferences sp;
private DashboardViewModel dashboardViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
dashboardViewModel =
ViewModelProviders.of(this).get(DashboardViewModel.class);
final View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
final TextView textView = root.findViewById(R.id.text_dashboard);
dashboardViewModel.getText().observe(this, new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText(s);
}
});
Button login=root.findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sp=getActivity().getSharedPreferences("userInfo",MODE_PRIVATE);
//将注册的用户名密码从SharedPreferences中取出来
String username=sp.getString("username","null");
String password=sp.getString("password","null");
//获取登录输入的用户名和密码
TextView username_edit=root.findViewById(R.id.username_edit);
TextView password_edit=root.findViewById(R.id.password_edit);
username_edit.setText(username_edit.getText().toString().trim());
password_edit.setText(password_edit.getText().toString().trim());
if (username_edit.getText().toString().length()==0) ts("用户名不能为空");
else if(password_edit.getText().toString().length()==0)ts("密码不能为空");
else if (!username.equals(username_edit.getText().toString())||!password.equals(password_edit.getText().toString()))
ts("用户名或密码错误");
else {
//登陆成功,跳转到“我的”界面
final NavController navController=
Navigation.findNavController(getActivity(),R.id.nav_host_fragment);
navController.navigate(R.id.navigation_notifications);
}
}
});
return root;
}
public void ts(String st) {
Toast.makeText(getActivity(),st,
Toast.LENGTH_SHORT).show();
}
}
个人信息(Notifications)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_notifications"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toBottomOf="@+id/text_notifications"
app:layout_constraintStart_toStartOf="@id/text_notifications"
>
<TextView
android:id="@+id/school"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="school"
/>
<TextView
android:id="@+id/classes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="classes"
/>
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="username"
/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name"
/>
<TextView
android:id="@+id/sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sex"
/>
<TextView
android:id="@+id/minzu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="minzu"
/>
<TextView
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="phone"
/>
<TextView
android:id="@+id/birthday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="birthday"
/>
<TextView
android:id="@+id/hobby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hobby"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Java
public class NotificationsFragment extends Fragment {
private SharedPreferences sp;
private NotificationsViewModel notificationsViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
notificationsViewModel =
ViewModelProviders.of(this).get(NotificationsViewModel.class);
View root = inflater.inflate(R.layout.fragment_notifications, container, false);
final TextView textView = root.findViewById(R.id.text_notifications);
notificationsViewModel.getText().observe(this, new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText(s);
}
});
TextView school=root.findViewById(R.id.school);
TextView classes=root.findViewById(R.id.classes );
TextView username=root.findViewById(R.id.username );
TextView name=root.findViewById(R.id.name);
TextView sex=root.findViewById(R.id.sex);
TextView minzu=root.findViewById(R.id.minzu );
TextView phone=root.findViewById(R.id.phone );
TextView birthday=root.findViewById(R.id.birthday );
TextView hobby=root.findViewById(R.id.hobby );
sp=getActivity().getSharedPreferences("userInfo",MODE_PRIVATE);
school.setText("学院:"+sp.getString("school","null"));
classes.setText("班级:"+sp.getString("classes","null"));
username.setText("用户名:"+sp.getString("username","null"));
name.setText("姓名:"+sp.getString("name","null"));
sex.setText("性别:"+sp.getString("xb","null"));
minzu.setText("民族:"+sp.getString("minzu","null"));
phone.setText("手机号:"+sp.getString("phone","null"));
birthday.setText("出生日期:"+sp.getString("lrrq","null"));
hobby.setText("爱好:"+sp.getString("hobby","null"));
return root;
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navView = findViewById(R.id.nav_view);//底部导航
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
.build();
final NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
navView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
//跳转相应fragment
//提示标题
//Toast.makeText(MainActivity.this,menuItem.getTitle(),Toast.LENGTH_SHORT).show();
sp=getSharedPreferences("userInfo",MODE_PRIVATE);
String username=sp.getString("username","null");
if (menuItem.getTitle().equals("登录")){
if (username.equals("null")) navController.navigate(R.id.navigation_home);
else navController.navigate(menuItem.getItemId());
}else if (menuItem.getTitle().equals("我的")){
if (username.equals("null")) navController.navigate(R.id.navigation_home);
else navController.navigate(menuItem.getItemId());
}else if (menuItem.getTitle().equals("注册")){
navController.navigate(menuItem.getItemId());
}
return true;
}
});
}
}
新建dialog_date
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
>
<DatePicker android:id="@+id/datePicker"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>