5.4(小组冲刺第九天)
日志基础功能代码
MainActivity:
package com.example.myapplication10;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main
);
}
public List<Fragment> fragmentList = new ArrayList<>();
private FragmentManager fragmentManager;
// 底部导航栏模块
public void InitBottomNavigation() {
// 添加五个fragment实例到fragmentList,以便管理
fragmentList.add(new TaskFragment());
fragmentList.add(new AbsorbedFragment());
//建立fragment管理器
fragmentManager = getSupportFragmentManager();
//管理器开启事务,将fragment实例加入管理器
fragmentManager.beginTransaction()
.add(R.id.FragmentLayout, fragmentList.get(0), "TASK")
.add(R.id.FragmentLayout, fragmentList.get(1), "ABSOTBED")
.add(R.id.FragmentLayout, fragmentList.get(2), "MUSIC")
.add(R.id.FragmentLayout, fragmentList.get(3), "WEATHER")
.commit();
//设置fragment显示初始状态
fragmentManager.beginTransaction()
.show(fragmentList.get(1))
.hide(fragmentList.get(0))
.hide(fragmentList.get(2))
.hide(fragmentList.get(3))
.commit();
//设置底部导航栏点击选择监听事件
BottomNavigationView bottomNavigationView = findViewById(R.id.BottomNavigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
// @Override
// public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// return false;
// }
@SuppressLint("NonConstantResourceId")
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// return true : show selected style
// return false: do not show
switch (item.getItemId()) {
case R.id.menu_task:
ShowFragment(0);
return true;
case R.id.menu_accounts:
ShowFragment(1);
return true;
case R.id.menu_absorbed:
ShowFragment(2);
return true;
case R.id.menu_weather:
ShowFragment(3);
return true;
default:
Log.i("TAG", "onNavigationItemSelected: Error");
break;
}
return false;
}
});
}
public void ShowFragment(int index) {
fragmentManager.beginTransaction()
.show(fragmentList.get(index))
.hide(fragmentList.get((index + 1) % 4))
.hide(fragmentList.get((index + 2) % 4))
.hide(fragmentList.get((index + 3) % 4))
.commit();
}
private static TaskAdapter mAdapter;
public static void updateAdapterData() {
if (mAdapter != null) {
mAdapter.notifyDataSetChanged();
}
}
}
main.xml:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/baseline_visibility_24"/>
<TextView
android:id="@+id/TypeMenu_default"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全部"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/baseline_visibility_24"/>
<TextView
android:id="@+id/TypeMenu_work"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="工作"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/baseline_visibility_24"/>
<TextView
android:id="@+id/TypeMenu_study"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学习"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/baseline_visibility_24"/>
<TextView
android:id="@+id/TypeMenu_life"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="生活"/>
</LinearLayout>
<ImageView
android:id="@+id/addTaskButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/baseline_visibility_24"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_margin="16dp"/>
TaskItem:
package com.example.myapplication10;
public class TaskItem {
private int id;
private String content;
private String type;
private int status;
public TaskItem(int id, String type, String content, int status){
this.id = id;
this.type = type;
this.content = content;
this.status = status;
}
public TaskItem(int anInt, String string, int anInt1, String string1, String string2, int anInt2) {
}
public TaskItem(String addType, int addLevel, String addContent, String addInfo, int i) {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}
TaskFragment:
package com.example.myapplication10;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;
import java.time.DayOfWeek;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
/**
-
A simple {@link Fragment} subclass.
-
Use the {@link TaskFragment#newInstance} factory method to
-
create an instance of this fragment.
*/
public class TaskFragment extends Fragment {// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private final String ARG_PARAM1 = "param1";
private final String ARG_PARAM2 = "param2";// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
TaskAdapter taskAdapter;
SQLiteDatabase readDatabase;public TaskFragment() {
// Required empty public constructor
}/**
- Use this factory method to create a new instance of
- this fragment using the provided parameters.
- @param param1 Parameter 1.
- @param param2 Parameter 2.
- @return A new instance of fragment TaskFragment.
*/
// TODO: Rename and change types and number of parameters
public TaskFragment newInstance(String param1, String param2) {
TaskFragment fragment = new TaskFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}@Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_task, container, false); return view; }
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ImageView t=view.findViewById(R.id.addTaskButton);
t.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ShowAddTaskDialog();
}
});}
//读取数据库并将数据存到taskList
public void ReadTaskDataFromSQL(){
ListtaskList = null;
if (taskList.size()!=0) {
taskList.clear();
}Cursor cursor = readDatabase.query( "task", new String[]{"id", "type", "level","content", "info", "status"}, null, null, null, null, null ); //隐藏,有分类 String TypeNow = null; if(isHideCompleted && !TypeNow.equals("全部")){ //只获取未完成事项 while(cursor.moveToNext()){ if((cursor.getInt(5) == 0 ) && (cursor.getString(1).equals(TypeNow))){ TaskItem task = new TaskItem( cursor.getInt(0), cursor.getString(1), cursor.getInt(2), cursor.getString(3), cursor.getString(4), cursor.getInt(5) ); taskList.add(task); } } } //不隐藏,有分类 if(!isHideCompleted && !TypeNow.equals("全部")){ while(cursor.moveToNext()){ if(cursor.getString(1).equals(TypeNow)){ TaskItem task = new TaskItem( cursor.getInt(0), cursor.getString(1), cursor.getInt(2), cursor.getString(3), cursor.getString(4), cursor.getInt(5) ); taskList.add(task); } } } //隐藏,不分类 if(isHideCompleted && TypeNow.equals("全部")){ while(cursor.moveToNext()){ if(cursor.getInt(5) == 0){ TaskItem task = new TaskItem( cursor.getInt(0), cursor.getString(1), cursor.getInt(2), cursor.getString(3), cursor.getString(4), cursor.getInt(5) ); taskList.add(task); } } } else{ while(cursor.moveToNext()){ TaskItem task = new TaskItem( cursor.getInt(0), cursor.getString(1), cursor.getInt(2), cursor.getString(3), cursor.getString(4), cursor.getInt(5) ); taskList.add(task); } } // 别忘了通知ListView适配器数据变化 taskAdapter.notifyDataSetChanged(); } public void ShowTaskContent(View convertView, TaskItem task){ //显示事项内容 TextView content = ((ViewHolder) convertView.getTag()).taskContent; int status = task.getStatus(); content.setText(task.getContent()); //事项已完成 中划线 灰色 if(status == 1){ content.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); content.setTextColor(getResources().getColor(R.color.GRAY, null)); } //事项未完成 无中划线 黑色 if(status == 0){ content.getPaint().setFlags(0); content.setTextColor(getResources().getColor(R.color.black, null)); } //事项失败 无中划线 灰色 if(status == -1){ content.getPaint().setFlags(0); content.setTextColor(getResources().getColor(R.color.GRAY, null)); } } public void ShowTaskLevel(View convertView, int level){ // 显示事项重要级别 level : 0~3 四个优先级 Ⅰ Ⅱ Ⅲ Ⅳ TextView levelText = ((ViewHolder) convertView.getTag()).taskLevel; if(level == 0){ levelText.setText("Ⅰ"); levelText.setTextColor(getResources().getColor(R.color.level_0, null)); } if(level == 1){ levelText.setText("Ⅱ"); levelText.setTextColor(getResources().getColor(R.color.level_1, null)); } if(level == 2){ levelText.setText("Ⅲ"); levelText.setTextColor(getResources().getColor(R.color.level_2, null)); } if(level == 3){ levelText.setText("Ⅳ"); levelText.setTextColor(getResources().getColor(R.color.level_3, null)); } } /** 菜单栏模块 **/ public void SetTypeMenuOnClick(View view){ AbstractList<TextView> typeMenuList =null; typeMenuList.add((TextView) view.findViewById(R.id.TypeMenu_default)); typeMenuList.add((TextView) view.findViewById(R.id.TypeMenu_work)); typeMenuList.add((TextView) view.findViewById(R.id.TypeMenu_study)); typeMenuList.add((TextView) view.findViewById(R.id.TypeMenu_life)); int[] color = { getResources().getColor(R.color.defaultColor, null), getResources().getColor(R.color.workColor, null), getResources().getColor(R.color.studyColor, null), getResources().getColor(R.color.lifeColor, null), }; for(int i=0; i<4 ;i++){ int finalI = i; //分类索引值 typeMenuList.get(i).setOnClickListener(v -> { // 点击分类的一项后设置样式 typeMenuList.get(finalI).setTextColor(Color.BLACK); typeMenuList.get(finalI).setBackgroundColor(Color.WHITE); typeMenuList.get((finalI+1) % 4).setBackgroundColor(color[(finalI+1) % 4]); typeMenuList.get((finalI+1) % 4).setTextColor(Color.WHITE); typeMenuList.get((finalI+2) % 4).setBackgroundColor(color[(finalI+2) % 4]); typeMenuList.get((finalI+2) % 4).setTextColor(Color.WHITE); typeMenuList.get((finalI+3) % 4).setBackgroundColor(color[(finalI+3) % 4]); typeMenuList.get((finalI+3) % 4).setTextColor(Color.WHITE); // 显示某一类待办数据,这里筛选taskList即可 List<TaskItem> typeTaskList = new ArrayList<>(); String[] types = {"全部", "工作","学习","生活"}; /* 分类索引值 0 全部 1 工作 2 学习 3 生活 */ // 点击工作 学习 生活时分类 // TypeNow 是一个全局变量,表示当前的分类 String TypeNow = types[finalI]; Log.i("TAG", "SetTypeMenuOnClick: "+TypeNow); ReadTaskFromDatabase(); }); } } private void ReadTaskFromDatabase() { } private boolean isHideCompleted; // 声明全局变量 private void setUpHideCompletedTaskSwitch() { View view = null; Switch hideCompletedTaskSwitch = view.findViewById(R.id.HideCompletedTaskView); hideCompletedTaskSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { isHideCompleted = true; } else { isHideCompleted = false; } // isHideCompleted 是一个全局变量,表示当前是否隐藏已完成事项 ReadTaskFromDatabase(); } }); } public void ShowAddTaskDialog(){ //获取添加事项布局实例 View addView = getLayoutInflater().inflate(R.layout.add_task_dialog, null); // 将该布局添加到对话框 final AlertDialog addDialog = new AlertDialog.Builder(getActivity()).setView(addView).create(); addDialog.show(); //获取对话框中的布局控件 Button cancelButton = (Button) addView.findViewById(R.id.cancelAddButton); Button confirmButton = (Button) addView.findViewById(R.id.confirmAddButton); EditText contentEdit = (EditText) addView.findViewById(R.id.addTaskContentEdit); EditText infoEdit = (EditText) addView.findViewById(R.id.addTaskInfoEdit); RadioGroup typeGroup = (RadioGroup) addView.findViewById(R.id.typeRadioGroup); RadioGroup levelGroup = (RadioGroup) addView.findViewById(R.id.levelRadioGroup); typeGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { } }); levelGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { } }); //确定按钮 confirmButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 获取输入的事项内容和备注 String addContent = contentEdit.getText().toString(); String addInfo = infoEdit.getText().toString(); //RadioGroup的选择项 RadioButton typeSelectBtn = (RadioButton) addView.findViewById(typeGroup.getCheckedRadioButtonId()); String addType = typeSelectBtn.getText().toString(); RadioButton levelSelectBtn = (RadioButton) addView.findViewById(levelGroup.getCheckedRadioButtonId()); int addLevel = Integer.parseInt(levelSelectBtn.getText().toString().substring(0,1)); //插入数据库 InsertTaskToDatabase( new TaskItem(addType, addLevel, addContent, addInfo, 0) ); addDialog.dismiss(); } private void InsertTaskToDatabase(TaskItem taskItem) { } }); // 取消按钮 cancelButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addDialog.dismiss(); } }); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】