Android_小账本小结

 登录界面:支持登录、注册以及游客登录,单纯的小账本的话其实用不到这些个登录,单纯为了巩固学习知识。

 

尚未部署到服务器,账号等数据暂时保存在本地数据库中。

 

 

 

 

游客登陆:游客登录会直接跳到主页中,不影响使用。

 

注册和登录时会弹出自定义view的dialog,这时可能会有黑影,这个和默认的主题有关。参考我的这篇博客:https://www.cnblogs.com/XiaoGao128/p/12283918.html

注册:注册时会判断数据库中是否存在此账号,注册完成后直接跳转到主界面。

 

 

登录有记住密码和自动登录两个选项。这两个选项通过SharedPreferences实现。具体参考我的这篇博客https://www.cnblogs.com/XiaoGao128/p/12268652.html

 

LogInActivity.java

  1 package com.example.didida_corder;
  2 
  3 import androidx.appcompat.app.AlertDialog;
  4 import androidx.appcompat.app.AppCompatActivity;
  5 
  6 import android.content.Intent;
  7 import android.database.sqlite.SQLiteDatabase;
  8 import android.os.Bundle;
  9 import android.util.Log;
 10 import android.view.LayoutInflater;
 11 import android.view.View;
 12 import android.widget.Button;
 13 import android.widget.CheckBox;
 14 import android.widget.EditText;
 15 import android.widget.Toast;
 16 
 17 import com.example.didida_corder.ToolClass.Login;
 18 import com.example.didida_corder.ToolClass.SPUtils;
 19 import com.example.didida_corder.ToolClass.SQLiteUserChager;
 20 
 21 public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
 22     private Button btn_log,btn_register,btn_defult;
 23     private String nowusername;
 24     @Override
 25     protected void onCreate(Bundle savedInstanceState) {
 26         super.onCreate(savedInstanceState);
 27         setContentView(R.layout.activity_login2);
 28         if (getIntent().getExtras()!=null)
 29             if(getIntent().getExtras().get("refresh")!=null)
 30             autoIn();
 31             autoIn_2();
 32         btn_log=findViewById(R.id.login_btn_log);
 33         btn_register=findViewById(R.id.login_btn_regster);
 34         btn_defult=findViewById(R.id.login_btn_defult);
 35         btn_defult.setOnClickListener(this);
 36         btn_log.setOnClickListener(this);
 37         btn_register.setOnClickListener(this);
 38 
 39     }
 40     public void autoIn(){
 41             Bundle bundle=new Bundle();
 42             bundle.putString("username",getIntent().getExtras().getString("username"));
 43             bundle.putInt("type",1);
 44             bundle.putInt("refresh",0);
 45             Intent intent=new Intent(LoginActivity.this,MainActivity.class);
 46             intent.putExtras(bundle);
 47             startActivity(intent);
 48 
 49         }
 50         public void autoIn_2(){
 51             if ((Integer.parseInt(SPUtils.get(getApplicationContext(),"auto",222).toString())==1)){
 52                 Bundle bundle=new Bundle();
 53                 bundle.putString("username",SPUtils.get(getApplicationContext(),"username","222").toString());
 54                 bundle.putInt("type",0);
 55                 Log.d("autoin_2===========","");
 56                 Intent intent=new Intent(LoginActivity.this,MainActivity.class);
 57                 intent.putExtras(bundle);
 58                 startActivity(intent);
 59             }
 60 
 61 
 62     }
 63     @Override
 64     public void onClick(View v) {
 65         switch (v.getId()){
 66             case R.id.login_btn_regster:{
 67                 final AlertDialog.Builder builder4=new AlertDialog.Builder(this);
 68                 LayoutInflater inflater=LayoutInflater.from(this);
 69                 View view=inflater.inflate(R.layout.layout_register,null);
 70                 Button button=view.findViewById(R.id.adm_btn);
 71                 final EditText editText=view.findViewById(R.id.adm_et);
 72                 final EditText editText1=view.findViewById(R.id.adm_et2);
 73                 final EditText et_confirm=view.findViewById(R.id.adm_et_confirm);
 74                 final EditText et_name=view.findViewById(R.id.adm_name);
 75                 final AlertDialog alertDialog=builder4.setView(view).show();
 76                 alertDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
 77                 button.setOnClickListener(new View.OnClickListener() {
 78                     @Override
 79                     public void onClick(View v) {
 80                         if(editText.getText().toString().equals("")||editText1.getText().toString().equals("")||et_confirm.getText().toString().equals("")||et_name.getText().toString().equals("")) {
 81                             Toast.makeText(getApplicationContext(), "请补全信息!", Toast.LENGTH_LONG).show();
 82                         }else if (new Login(getApplicationContext()).isInstance(editText.getText().toString())){
 83                             Toast.makeText(getApplicationContext(),"用户名重复!请重新输入!",Toast.LENGTH_SHORT).show();
 84                             editText.setText("");
 85                         } else {
 86                             SPUtils.put(getApplicationContext(),"username",editText.getText().toString()).
 87                                     put(getApplicationContext(),"password",editText1.getText().toString()).
 88                                     put(getApplicationContext(),"name",et_name.getText().toString());
 89 
 90                             SQLiteUserChager sc=new SQLiteUserChager(getApplicationContext(),"Userr",null,1);
 91                             SQLiteDatabase db= sc.getWritableDatabase();
 92                             db.execSQL("insert into user values(?,?,?)",new String[]{et_name.getText().toString(),editText.getText().toString(),editText1.getText().toString()});
 93                             nowusername=editText.getText().toString();
 94                             Bundle bundle=new Bundle();
 95                             bundle.putString("username",nowusername);
 96                             bundle.putInt("type",0);
 97                             Intent intent=new Intent(LoginActivity.this,MainActivity.class);
 98                             intent.putExtras(bundle);
 99                             startActivity(intent);
100                             alertDialog.dismiss();
101                             Toast.makeText(getApplicationContext(), "注册成功!", Toast.LENGTH_SHORT).show();
102                         }
103                     }
104                 });
105                 break;
106             }
107 
108             case R.id.login_btn_log:{
109                 final AlertDialog.Builder builder4=new AlertDialog.Builder(this);
110                 LayoutInflater inflater=LayoutInflater.from(this);
111                 View view=inflater.inflate(R.layout.layout_admin,null);
112                 Button button=view.findViewById(R.id.adm_btn);
113                 final EditText editText=view.findViewById(R.id.adm_et);
114                 final EditText editText1=view.findViewById(R.id.adm_et2);
115                 final CheckBox rb_rember=view.findViewById(R.id.admin_radio_remeber);
116                 final CheckBox rb_auto=view.findViewById(R.id.admin_radio_autoin);
117                 Log.d("----------",SPUtils.get(this,"rember",3).toString());
118                 if (Integer.parseInt(SPUtils.get(this,"rember",3).toString())==1){
119                     editText.setText(SPUtils.get(this,"username","111").toString());
120                     editText1.setText(SPUtils.get(this,"password","111").toString());
121                 }
122                 final AlertDialog alertDialog=builder4.setView(view).show();
123                 alertDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
124                 button.setOnClickListener(new View.OnClickListener() {
125                     @Override
126                     public void onClick(View v) {
127                         if(editText.getText().toString().equals("")||editText1.getText().toString().equals("")) {
128                             Toast.makeText(getApplicationContext(), "请补全信息!", Toast.LENGTH_LONG).show();
129                         }else {
130                             if (new Login(getApplicationContext()).sentenceLog(editText.getText().toString(),editText1.getText().toString()))
131                             {
132                                 SPUtils.put(getApplicationContext(),"username",editText.getText().toString()).
133                                         put(getApplicationContext(),"password",editText1.getText().toString());
134                                 if (rb_rember.isChecked()) SPUtils.put(getApplicationContext(),"rember",1);
135                                 else SPUtils.put(getApplicationContext(),"rember",0);
136                                 if (rb_auto.isChecked()) SPUtils.put(getApplicationContext(),"auto",1);
137                                 else SPUtils.put(getApplicationContext(),"auto",0);
138                                 Toast.makeText(getApplicationContext(), "登陆成功!", Toast.LENGTH_LONG).show();
139                                 nowusername=editText.getText().toString();
140                                 Bundle bundle=new Bundle();
141                                 bundle.putString("username",nowusername);
142                                 bundle.putInt("type",0);
143                                 Intent intent=new Intent(getApplicationContext(),MainActivity.class);
144                                 intent.putExtras(bundle);
145                                 startActivity(intent);
146                                 alertDialog.dismiss();
147                             }
148                             else Toast.makeText(getApplicationContext(),"用户名或密码错误!",Toast.LENGTH_SHORT).show();
149                         }
150                     }
151                 });
152 
153                 break;
154             }
155             case R.id.login_btn_defult:{
156                 nowusername="defult";
157                 Bundle bundle=new Bundle();
158                 bundle.putString("username",nowusername);
159                 bundle.putInt("type",1);
160                 Intent intent=new Intent(LoginActivity.this,MainActivity.class);
161                 intent.putExtras(bundle);
162                 startActivity(intent);
163                 break;
164             }
165 
166         }
167 
168     }
169 }
View Code

activity_log2.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:background="@drawable/wolf"
 7     android:orientation="vertical"
 8     android:gravity="center"
 9     tools:context=".LoginActivity">
10     <Button
11         android:id="@+id/login_btn_log"
12         android:layout_width="270dp"
13         android:layout_height="50dp"
14         android:background="@drawable/riple_btn_pink"
15         android:text="登录"
16         android:textColor="#FFFFFF"
17         android:layout_marginBottom="30dp"
18         android:textSize="15dp"></Button>
19     <LinearLayout
20         android:layout_width="wrap_content"
21         android:layout_height="wrap_content"
22         android:orientation="horizontal">
23     <Button
24         android:id="@+id/login_btn_regster"
25         android:layout_width="90dp"
26         android:layout_height="wrap_content"
27         android:background="@drawable/riple_btn_pink"
28         android:text="注册"
29         android:textColor="#FFFFFF"
30         android:layout_marginTop="20dp"
31         android:layout_marginRight="60dp"
32         android:textSize="15dp"></Button>
33     <Button
34         android:id="@+id/login_btn_defult"
35         android:layout_width="120dp"
36         android:layout_height="wrap_content"
37         android:background="@drawable/riple_btn_pink"
38         android:text="游客登陆"
39         android:textColor="#FFFFFF"
40         android:layout_marginTop="20dp"
41         android:textSize="15dp"></Button>
42     </LinearLayout>
43 </LinearLayout>
View Code

 

页面:这个app预计分为四个页面,分别用四个fragment实现。

MainActivity.java

  1 package com.example.didida_corder;
  2 
  3 import androidx.annotation.RequiresApi;
  4 import androidx.appcompat.app.AppCompatActivity;
  5 import androidx.drawerlayout.widget.DrawerLayout;
  6 import androidx.fragment.app.Fragment;
  7 import androidx.fragment.app.FragmentManager;
  8 import androidx.viewpager.widget.ViewPager;
  9 
 10 import android.app.Dialog;
 11 import android.app.FragmentTransaction;
 12 import android.content.Intent;
 13 import android.database.sqlite.SQLiteDatabase;
 14 import android.os.Build;
 15 import android.os.Bundle;
 16 import android.util.Log;
 17 import android.view.Gravity;
 18 import android.view.LayoutInflater;
 19 import android.view.View;
 20 import android.view.Window;
 21 import android.view.WindowManager;
 22 import android.widget.Adapter;
 23 import android.widget.Button;
 24 import android.widget.ImageView;
 25 import android.widget.LinearLayout;
 26 import android.widget.RadioButton;
 27 import android.widget.RadioGroup;
 28 import android.widget.RelativeLayout;
 29 import android.widget.TextView;
 30 
 31 import com.example.didida_corder.ToolClass.SQLiteUserChager;
 32 
 33 import java.util.List;
 34 
 35 public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, RadioGroup.OnCheckedChangeListener,
 36         View.OnClickListener, InOutFragment.IOinput {
 37 
 38     private ViewPager viewPager;
 39     private TextView textView;
 40     private ImageView my_right_menu;
 41     private RadioGroup radioGroup;
 42     private DrawerLayout drawerLayout;
 43     private RadioButton radioButton_crod;
 44     private RadioButton radioButton_inout;
 45     private RadioButton radioButton_myitem;
 46     private int PAGE_NUM = 3,ii=0;
 47     private final int PAGE_CORD = 0;
 48     private final int PAGE_INOUT = 1;
 49     private final int PAGE_MY = 2;
 50     private MyFragPageAdapter myFragPageAdapter;
 51     private MyFragment myFragment;
 52     private FragmentManager fragmentManager;
 53     private String nowusername="defult";
 54     private int type;
 55     private boolean isrefresh=false;
 56     @Override
 57     protected void onCreate(Bundle savedInstanceState) {
 58         super.onCreate(savedInstanceState);
 59         setContentView(R.layout.activity_main);
 60         //从login传入参数
 61         Bundle bundle=getIntent().getExtras();
 62         nowusername=bundle.getString("username");
 63         type=bundle.getInt("type");
 64         Log.d("userame===========",nowusername);
 65         fragmentManager = getSupportFragmentManager();
 66         myFragPageAdapter = new MyFragPageAdapter(getSupportFragmentManager(),nowusername);
 67         myFragment = (MyFragment) fragmentManager.findFragmentById(R.id.fg_left_menu);
 68         myFragment.sentenceAndSet(nowusername,type);
 69         bindViews();
 70         if (bundle.getInt("refresh")!=1){
 71             radioButton_inout.setChecked(true);
 72             radioButton_inout.setBackground(getDrawable(R.drawable.money_2));
 73         }else {
 74             radioButton_crod.setChecked(true);
 75             radioButton_crod.setBackground(getDrawable(R.drawable.hostory_1));
 76         }initLeftMenu();
 77     }
 78 
 79     private void bindViews() {
 80         radioButton_crod = findViewById(R.id.main_rd_cord);
 81         radioButton_inout = findViewById(R.id.main_rd_inout);
 82         radioButton_myitem = findViewById(R.id.main_rd_my);
 83         radioGroup = findViewById(R.id.main_radio_group);
 84         radioGroup.setOnCheckedChangeListener(this);
 85         viewPager = findViewById(R.id.main_viewpager);
 86         viewPager.setAdapter(myFragPageAdapter);
 87         viewPager.addOnPageChangeListener(this);
 88         viewPager.setCurrentItem(PAGE_INOUT);
 89         drawerLayout = findViewById(R.id.main_drawlayout);
 90     }
 91 
 92     public void initLeftMenu() {
 93         my_right_menu = findViewById(R.id.main_leftmenu);
 94         my_right_menu.setOnClickListener(this);
 95     }
 96 
 97     @Override
 98     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
 99 
100     }
101 
102     @Override
103     public void onPageSelected(int position) {
104 
105     }
106 
107     @Override
108     public void onPageScrollStateChanged(int state) {
109         if (state == 2) {
110             switch (viewPager.getCurrentItem()) {
111                 case PAGE_CORD:
112                     radioButton_crod.setChecked(true);
113                     radioButton_inout.setBackground(getDrawable(R.drawable.money));
114                     radioButton_crod.setBackground(getDrawable(R.drawable.hostory_1));
115                     radioButton_myitem.setBackground(getDrawable(R.drawable.society));
116                     //new 可解决翻页时 No adapter attached; skipping layout
117                     viewPager.setAdapter(new MyFragPageAdapter(getSupportFragmentManager(),nowusername));
118                     break;
119                 case PAGE_INOUT:
120                     radioButton_inout.setChecked(true);
121                     radioButton_inout.setBackground(getDrawable(R.drawable.money_2));
122                     radioButton_crod.setBackground(getDrawable(R.drawable.hostory));
123                     radioButton_myitem.setBackground(getDrawable(R.drawable.society));
124                     break;
125                 case PAGE_MY:
126                     radioButton_myitem.setChecked(true);
127                     radioButton_inout.setBackground(getDrawable(R.drawable.money));
128                     radioButton_crod.setBackground(getDrawable(R.drawable.hostory));
129                     radioButton_myitem.setBackground(getDrawable(R.drawable.society_1));
130                     break;
131             }
132         }
133     }
134 
135     @Override
136     public void onCheckedChanged(RadioGroup group, int checkedId) {
137         switch (checkedId) {
138             case R.id.main_rd_cord: {
139                 viewPager.setCurrentItem(PAGE_CORD);
140                 //new 可解决翻页时 No adapter attached; skipping layout
141                 radioButton_inout.setBackground(getDrawable(R.drawable.money));
142                 radioButton_crod.setBackground(getDrawable(R.drawable.hostory_1));
143                 radioButton_myitem.setBackground(getDrawable(R.drawable.society));
144                 Log.d("mainact------username",nowusername);
145                 viewPager.setAdapter(new MyFragPageAdapter(getSupportFragmentManager(),nowusername));
146                 break;
147             }
148             case R.id.main_rd_inout: {
149                 viewPager.setCurrentItem(PAGE_INOUT);
150                 radioButton_inout.setBackground(getDrawable(R.drawable.money_2));
151                 radioButton_crod.setBackground(getDrawable(R.drawable.hostory));
152                 radioButton_myitem.setBackground(getDrawable(R.drawable.society));
153                 break;
154             }
155             case R.id.main_rd_my: {
156                 viewPager.setCurrentItem(PAGE_MY);
157                 radioButton_inout.setBackground(getDrawable(R.drawable.money));
158                 radioButton_crod.setBackground(getDrawable(R.drawable.hostory));
159                 radioButton_myitem.setBackground(getDrawable(R.drawable.society_1));
160                 break;
161             }
162         }
163     }
164 
165     @Override
166     public void onClick(View v) {
167         drawerLayout.openDrawer(Gravity.LEFT);
168     }
169 
170     @RequiresApi(api = Build.VERSION_CODES.N)
171     @Override
172     public void send(String s1, String s2) {
173         Dialog mCameraDialog = new Dialog(this, R.style.BottomDialog);
174         RelativeLayout root = (RelativeLayout) LayoutInflater.from(this).inflate(
175                 R.layout.layout_fragment_calcul, null);
176         CalculFragment calculFragment = new CalculFragment(root,MainActivity.this,s1,s2,nowusername,mCameraDialog);
177         root = calculFragment.bindandset(root);
178         mCameraDialog.setContentView(root);
179         Window dialogWindow = mCameraDialog.getWindow();
180         dialogWindow.setGravity(Gravity.BOTTOM);
181         dialogWindow.setWindowAnimations(R.style.DialogAnimation); // 添加动画
182         WindowManager.LayoutParams lp = dialogWindow.getAttributes(); // 获取对话框当前的参数值
183         lp.x = 0; // 新位置X坐标
184         lp.y = 0; // 新位置Y坐标
185         lp.width = (int) getResources().getDisplayMetrics().widthPixels; // 宽度
186         root.measure(0, 0);
187         lp.height = root.getMeasuredHeight();
188 //        lp.alpha = 9f; // 透明度
189         dialogWindow.setAttributes(lp);
190         mCameraDialog.show();
191 
192 
193     }
194 
195 
196 }
View Code

activity_main.xml

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <androidx.drawerlayout.widget.DrawerLayout
  3     xmlns:android="http://schemas.android.com/apk/res/android"
  4     xmlns:tools="http://schemas.android.com/tools"
  5     android:id="@+id/main_drawlayout"
  6     android:layout_width="match_parent"
  7     android:layout_height="match_parent">
  8 
  9     <RelativeLayout
 10         android:layout_width="match_parent"
 11         android:layout_height="match_parent"
 12         android:background="#FAFAFA"
 13         android:orientation="horizontal"
 14         tools:context=".MainActivity">
 15         <LinearLayout
 16             android:id="@+id/main_toplinear"
 17             android:layout_width="match_parent"
 18             android:layout_height="50dp"
 19             android:background="#FFFFFF"
 20             android:gravity="center"
 21             android:orientation="horizontal">
 22 
 23             <ImageView
 24                 android:id="@+id/main_leftmenu"
 25                 android:layout_width="35dp"
 26                 android:layout_height="35dp"
 27                 android:layout_marginLeft="27dp"
 28                 android:src="@drawable/sanxian"></ImageView>
 29 
 30             <LinearLayout
 31                 android:layout_width="match_parent"
 32                 android:layout_height="70dp"
 33                 android:gravity="center">
 34 
 35                 <TextView
 36                     android:layout_width="wrap_content"
 37                     android:layout_height="wrap_content"
 38                     android:text="滴滴嗒小账本"
 39                     android:textColor="#B3000000"
 40                     android:textSize="20dp"></TextView>
 41             </LinearLayout>
 42 
 43             <ImageView
 44                 android:layout_width="35dp"
 45                 android:layout_height="35dp"
 46                 android:layout_marginRight="27dp"></ImageView>
 47         </LinearLayout>
 48 
 49         <View
 50             android:layout_width="match_parent"
 51             android:layout_height="0.5dp"
 52             android:layout_below="@+id/main_toplinear"
 53             android:background="#232"></View>
 54 
 55         <View
 56             android:layout_width="match_parent"
 57             android:layout_height="0.5dp"
 58             android:layout_above="@+id/main_radio_group"
 59             android:background="#232"></View>
 60 
 61         <RadioGroup
 62             android:id="@+id/main_radio_group"
 63             android:layout_width="match_parent"
 64             android:layout_height="70dp"
 65             android:background="#00F8A4B4"
 66             android:layout_alignParentBottom="true"
 67             android:gravity="center"
 68             android:orientation="horizontal">
 69 
 70             <RadioButton
 71                 android:id="@+id/main_rd_cord"
 72                 android:layout_width="wrap_content"
 73                 android:layout_height="match_parent"
 74                 android:layout_weight="1"
 75                 android:button="@null"
 76                 android:background="@drawable/hostory"
 77                 android:scaleX="0.2"
 78                 android:scaleY="0.3"
 79                 android:gravity="center"
 80                 android:textColor="#5FB1D1"
 81                 android:textSize="25dp"></RadioButton>
 82 
 83             <RadioButton
 84                 android:id="@+id/main_rd_inout"
 85                 android:layout_width="wrap_content"
 86                 android:layout_height="match_parent"
 87                 android:layout_weight="1"
 88                 android:button="@null"
 89                 android:gravity="center"
 90                 android:background="@drawable/money"
 91                 android:scaleX="0.2"
 92                 android:scaleY="0.3"
 93                 android:textColor="#5FB1D1"
 94                 android:textSize="25dp"></RadioButton>
 95 
 96             <RadioButton
 97                 android:id="@+id/main_rd_my"
 98                 android:layout_width="wrap_content"
 99                 android:layout_height="match_parent"
100                 android:layout_weight="1"
101                 android:button="@null"
102                 android:gravity="center"
103                 android:background="@drawable/society"
104                 android:scaleX="0.2"
105                 android:scaleY="0.3"
106                 android:textColor="#5FB1D1"
107                 android:textSize="25dp"></RadioButton>
108         </RadioGroup>
109 
110         <androidx.viewpager.widget.ViewPager
111             android:id="@+id/main_viewpager"
112             android:layout_width="match_parent"
113             android:layout_height="match_parent"
114             android:layout_above="@id/main_radio_group"
115             android:layout_below="@+id/main_toplinear"></androidx.viewpager.widget.ViewPager>
116     </RelativeLayout>
117     <fragment
118         android:id="@+id/fg_left_menu"
119         class="com.example.didida_corder.MyFragment"
120         android:layout_width="wrap_content"
121         android:layout_height="match_parent"
122         android:layout_gravity="start"
123         tools:layout="@layout/layout_myfragment"
124         ></fragment>
125 </androidx.drawerlayout.widget.DrawerLayout>
View Code

  

其中,三个主页面放在一个viewPager中。

InOutFragment(收支页面,记录收支信息)

收支页面分为两个大项十一个小项

其具体布局如下:

 

 

其他十一个小项默认隐藏,在点击相应按钮后相应小项会显示出来:

  

 

 

 

 

 

点击小项后会从下方弹出一个输入框,它相当于一个小型计算器,具体参考我的这两篇博客:

https://www.cnblogs.com/XiaoGao128/p/12263125.html

https://www.cnblogs.com/XiaoGao128/p/12245459.html

 

 

其中点击时间按钮可弹出一个DatePicker设置日期,设置完成后将时间、收支、类型、金额等存储到SQLite中。

 

因为viewPager会复用缓存的Fragment的view,所以数据更新比较麻烦,为了能够在输入完成之后滑到CordFragment时能够正确的显示,我在划过去时new了一个adapter,这样会导致一定的资源浪费,但暂时还没找到相对好的解决方法。

 InOutFragment.java

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <androidx.drawerlayout.widget.DrawerLayout
  3     xmlns:android="http://schemas.android.com/apk/res/android"
  4     xmlns:tools="http://schemas.android.com/tools"
  5     android:id="@+id/main_drawlayout"
  6     android:layout_width="match_parent"
  7     android:layout_height="match_parent">
  8 
  9     <RelativeLayout
 10         android:layout_width="match_parent"
 11         android:layout_height="match_parent"
 12         android:background="#FAFAFA"
 13         android:orientation="horizontal"
 14         tools:context=".MainActivity">
 15         <LinearLayout
 16             android:id="@+id/main_toplinear"
 17             android:layout_width="match_parent"
 18             android:layout_height="50dp"
 19             android:background="#FFFFFF"
 20             android:gravity="center"
 21             android:orientation="horizontal">
 22 
 23             <ImageView
 24                 android:id="@+id/main_leftmenu"
 25                 android:layout_width="35dp"
 26                 android:layout_height="35dp"
 27                 android:layout_marginLeft="27dp"
 28                 android:src="@drawable/sanxian"></ImageView>
 29 
 30             <LinearLayout
 31                 android:layout_width="match_parent"
 32                 android:layout_height="70dp"
 33                 android:gravity="center">
 34 
 35                 <TextView
 36                     android:layout_width="wrap_content"
 37                     android:layout_height="wrap_content"
 38                     android:text="滴滴嗒小账本"
 39                     android:textColor="#B3000000"
 40                     android:textSize="20dp"></TextView>
 41             </LinearLayout>
 42 
 43             <ImageView
 44                 android:layout_width="35dp"
 45                 android:layout_height="35dp"
 46                 android:layout_marginRight="27dp"></ImageView>
 47         </LinearLayout>
 48 
 49         <View
 50             android:layout_width="match_parent"
 51             android:layout_height="0.5dp"
 52             android:layout_below="@+id/main_toplinear"
 53             android:background="#232"></View>
 54 
 55         <View
 56             android:layout_width="match_parent"
 57             android:layout_height="0.5dp"
 58             android:layout_above="@+id/main_radio_group"
 59             android:background="#232"></View>
 60 
 61         <RadioGroup
 62             android:id="@+id/main_radio_group"
 63             android:layout_width="match_parent"
 64             android:layout_height="70dp"
 65             android:background="#00F8A4B4"
 66             android:layout_alignParentBottom="true"
 67             android:gravity="center"
 68             android:orientation="horizontal">
 69 
 70             <RadioButton
 71                 android:id="@+id/main_rd_cord"
 72                 android:layout_width="wrap_content"
 73                 android:layout_height="match_parent"
 74                 android:layout_weight="1"
 75                 android:button="@null"
 76                 android:background="@drawable/hostory"
 77                 android:scaleX="0.2"
 78                 android:scaleY="0.3"
 79                 android:gravity="center"
 80                 android:textColor="#5FB1D1"
 81                 android:textSize="25dp"></RadioButton>
 82 
 83             <RadioButton
 84                 android:id="@+id/main_rd_inout"
 85                 android:layout_width="wrap_content"
 86                 android:layout_height="match_parent"
 87                 android:layout_weight="1"
 88                 android:button="@null"
 89                 android:gravity="center"
 90                 android:background="@drawable/money"
 91                 android:scaleX="0.2"
 92                 android:scaleY="0.3"
 93                 android:textColor="#5FB1D1"
 94                 android:textSize="25dp"></RadioButton>
 95 
 96             <RadioButton
 97                 android:id="@+id/main_rd_my"
 98                 android:layout_width="wrap_content"
 99                 android:layout_height="match_parent"
100                 android:layout_weight="1"
101                 android:button="@null"
102                 android:gravity="center"
103                 android:background="@drawable/society"
104                 android:scaleX="0.2"
105                 android:scaleY="0.3"
106                 android:textColor="#5FB1D1"
107                 android:textSize="25dp"></RadioButton>
108         </RadioGroup>
109 
110         <androidx.viewpager.widget.ViewPager
111             android:id="@+id/main_viewpager"
112             android:layout_width="match_parent"
113             android:layout_height="match_parent"
114             android:layout_above="@id/main_radio_group"
115             android:layout_below="@+id/main_toplinear"></androidx.viewpager.widget.ViewPager>
116     </RelativeLayout>
117     <fragment
118         android:id="@+id/fg_left_menu"
119         class="com.example.didida_corder.MyFragment"
120         android:layout_width="wrap_content"
121         android:layout_height="match_parent"
122         android:layout_gravity="start"
123         tools:layout="@layout/layout_myfragment"
124         ></fragment>
125 </androidx.drawerlayout.widget.DrawerLayout>
View Code

  layout_inout.xml

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3     android:orientation="vertical" android:layout_width="match_parent"
  4     android:background="#FFFFFF"
  5     android:layout_height="match_parent">
  6     <LinearLayout
  7         android:layout_marginTop="20dp"
  8         android:layout_width="match_parent"
  9         android:layout_height="90dp"
 10         android:layout_weight="1"
 11         >
 12         <ImageView
 13             android:id="@+id/inout_shuidian"
 14             android:layout_width="70dp"
 15             android:layout_height="70dp"
 16             android:src="@drawable/shuidian_hong"
 17             android:layout_weight="1"
 18             android:visibility="invisible"
 19             ></ImageView>
 20         <ImageView
 21             android:id="@+id/inout_canyin"
 22             android:layout_width="70dp"
 23             android:layout_height="70dp"
 24             android:src="@drawable/cnayin_hong"
 25             android:layout_weight="1"
 26             android:visibility="invisible"
 27             ></ImageView>
 28     </LinearLayout>
 29     <LinearLayout
 30         android:layout_width="match_parent"
 31         android:layout_height="90dp"
 32         android:layout_weight="1"
 33         >
 34         <ImageView
 35             android:id="@+id/inout_yiwu"
 36             android:layout_width="70dp"
 37             android:layout_height="70dp"
 38             android:src="@drawable/yiwu_hong"
 39             android:layout_weight="1"
 40             android:visibility="invisible"
 41             ></ImageView>
 42         <ImageView
 43             android:id="@+id/inout_in"
 44             android:layout_width="70dp"
 45             android:layout_height="70dp"
 46             android:src="@drawable/zhichu_hei"
 47             android:layout_weight="1"
 48             ></ImageView>
 49         <ImageView
 50             android:id="@+id/inout_qita"
 51             android:layout_width="70dp"
 52             android:layout_height="70dp"
 53             android:src="@drawable/qita_hong"
 54             android:layout_weight="1"
 55             android:visibility="invisible"
 56             ></ImageView>
 57     </LinearLayout>
 58     <LinearLayout
 59         android:layout_width="match_parent"
 60         android:layout_height="90dp"
 61         android:layout_weight="1"
 62         >
 63         <ImageView
 64             android:id="@+id/inout_chuxing"
 65             android:layout_width="70dp"
 66             android:layout_height="70dp"
 67             android:src="@drawable/jiaotong_hong"
 68             android:layout_weight="1"
 69             android:visibility="invisible"
 70             ></ImageView>
 71         <ImageView
 72             android:id="@+id/inout_dianzi"
 73             android:layout_width="70dp"
 74             android:layout_height="70dp"
 75             android:src="@drawable/dianzi_hong"
 76             android:layout_weight="1"
 77             android:visibility="invisible"
 78             ></ImageView>
 79         <ImageView
 80             android:id="@+id/inout_yule"
 81             android:layout_width="70dp"
 82             android:layout_height="70dp"
 83             android:src="@drawable/yule_hong"
 84             android:layout_weight="1"
 85             android:visibility="invisible"
 86             ></ImageView>
 87     </LinearLayout>
 88     <LinearLayout
 89         android:layout_width="match_parent"
 90         android:layout_height="90dp"
 91         android:layout_weight="1"
 92         >
 93         <ImageView
 94             android:id="@+id/inout_gongzi"
 95             android:layout_width="70dp"
 96             android:layout_height="70dp"
 97             android:src="@drawable/gongzi_hong"
 98             android:layout_weight="1"
 99             android:visibility="invisible"
100             ></ImageView>
101         <ImageView
102             android:id="@+id/inout_out"
103             android:layout_width="70dp"
104             android:layout_height="70dp"
105             android:src="@drawable/shouru_hei"
106             android:layout_weight="1"
107             ></ImageView>
108         <ImageView
109             android:id="@+id/inout_jianqian"
110             android:layout_width="70dp"
111             android:layout_height="70dp"
112             android:src="@drawable/jiandao_hong"
113             android:layout_weight="1"
114             android:visibility="invisible"
115             ></ImageView>
116     </LinearLayout>
117     <LinearLayout
118         android:layout_width="match_parent"
119         android:layout_height="90dp"
120         android:layout_weight="1"
121         >
122         <ImageView
123             android:id="@+id/inout_jianzhi"
124             android:layout_width="70dp"
125             android:layout_height="70dp"
126             android:src="@drawable/jianzhi_hong"
127             android:layout_weight="1"
128             android:visibility="invisible"
129             ></ImageView>
130         <ImageView
131             android:id="@+id/inout_hongbao"
132             android:layout_width="70dp"
133             android:layout_height="70dp"
134             android:src="@drawable/hongbao_hong"
135             android:layout_weight="1"
136             android:visibility="invisible"
137             ></ImageView>
138     </LinearLayout>
139 
140 
141 
142 
143 
144 
145 
146 </LinearLayout>
147  
View Code

下方弹出的输入页面: 

CalculFragment

  1 package com.example.didida_corder;
  2 
  3 import android.app.AlertDialog;
  4 import android.app.DatePickerDialog;
  5 import android.app.Dialog;
  6 import android.content.Context;
  7 import android.icu.util.Calendar;
  8 import android.icu.util.ChineseCalendar;
  9 import android.os.Build;
 10 import android.os.Bundle;
 11 import android.util.Log;
 12 import android.view.LayoutInflater;
 13 import android.view.View;
 14 import android.view.ViewGroup;
 15 import android.widget.Button;
 16 import android.widget.DatePicker;
 17 import android.widget.EditText;
 18 import android.widget.RelativeLayout;
 19 import android.widget.TextView;
 20 import android.widget.Toast;
 21 
 22 import androidx.annotation.NonNull;
 23 import androidx.annotation.Nullable;
 24 import androidx.annotation.RequiresApi;
 25 import androidx.fragment.app.Fragment;
 26 
 27 //import java.util.Calendar;
 28 import com.example.didida_corder.ToolClass.Login;
 29 
 30 import java.util.Date;
 31 import java.util.zip.Inflater;
 32 
 33 import static java.lang.Math.sqrt;
 34 
 35 public class CalculFragment implements InOutFragment.IOinput{
 36     private String inout,type,number,date,info;
 37     private Button btn_0, btn_1, btn_2, btn_3, btn_4, btn_5, btn_6, btn_7, btn_8, btn_9, btn_add,
 38             btn_del, btn_mul, btn_ac, btn_sub, btn_equal, btn_divide, btn_date,
 39             btn_point;
 40     private TextView tv_cord, tv_calcul;
 41     private EditText et_tell;
 42     private double init = 0;//当前输入数据
 43     private char calcul = '#';//运算符
 44     private double nowsum = 0;//当前运算结果
 45     private int pointnum = 0;//小数点输入次数
 46     private RelativeLayout root;
 47     private Context context;
 48     private String username;
 49     private Dialog dialog;
 50     boolean isaheadchar = false, isnowpoint = false, isaheadequal = false, isaheadsqr = false;
 51 
 52     //isaheadchar: 是否输入过运算符
 53     //isnowpoint:小数点状态
 54     //isaheadequal:前一个运算符是否为=
 55     //isaheadsqr:前一个运算符是否为平方,主要为setText方便而设置
 56     @RequiresApi(api = Build.VERSION_CODES.N)
 57     public CalculFragment(RelativeLayout root, Context context,String inout,String type,String username,Dialog dialog) {
 58         this.root = root;
 59         this.context = context;
 60         this.inout=inout;
 61         this.type=type;
 62         this.dialog=dialog;
 63         Log.d("=========clacu",username);
 64         this.username=username;
 65         bindandset(this.root);
 66     }
 67 
 68     @RequiresApi(api = Build.VERSION_CODES.N)
 69     public RelativeLayout bindandset(View view) {
 70         btn_0 = root.findViewById(R.id.calcul_btn_0);
 71         btn_1 = root.findViewById(R.id.calcul_btn_1);
 72         btn_2 = root.findViewById(R.id.calcul_btn_2);
 73         btn_3 = root.findViewById(R.id.calcul_btn_3);
 74         btn_4 = root.findViewById(R.id.calcul_btn_4);
 75         btn_5 = root.findViewById(R.id.calcul_btn_5);
 76         btn_6 = root.findViewById(R.id.calcul_btn_6);
 77         btn_7 = root.findViewById(R.id.calcul_btn_7);
 78         btn_8 = root.findViewById(R.id.calcul_btn_8);
 79         btn_9 = root.findViewById(R.id.calcul_btn_9);
 80         btn_date = root.findViewById(R.id.calcul_btn_date);
 81         btn_point = root.findViewById(R.id.calcul_btn_point);
 82         btn_sub = root.findViewById(R.id.calcul_btn_sub);
 83         btn_ac = root.findViewById(R.id.calcul_btn_ac);
 84         btn_add = root.findViewById(R.id.calcul_btn_add);
 85         et_tell=root.findViewById(R.id.tv_tell);
 86         btn_equal = root.findViewById(R.id.calcul_btn_equal);
 87         btn_del = root.findViewById(R.id.calcul_btn_del);
 88         btn_divide = root.findViewById(R.id.calcul_btn_divide);
 89         btn_mul = root.findViewById(R.id.calcul_btn_multiply);
 90         tv_calcul = root.findViewById(R.id.tv_calcul);
 91         tv_cord = root.findViewById(R.id.tv_cord);
 92         tv_calcul.setText("");
 93         tv_cord.setText("");
 94         btn_0.setOnClickListener(new View.OnClickListener() {
 95             @Override
 96             public void onClick(View v) {
 97                 if (isaheadequal || isaheadchar) {
 98                     btn_equal.setText("=");
 99                 } else tv_calcul.setText(tv_calcul.getText() + "0");
100                 init = Double.parseDouble((String) tv_calcul.getText());
101                 isaheadequal = false;
102                 isaheadchar = false;
103             }
104         });
105         btn_1.setOnClickListener(new View.OnClickListener() {
106             @Override
107             public void onClick(View v) {
108                 if (isaheadequal || isaheadchar) {
109                     btn_equal.setText("=");
110                     tv_calcul.setText("1");
111                 } else tv_calcul.setText(tv_calcul.getText() + "1");
112                 init = Double.parseDouble((String) tv_calcul.getText());
113                 isaheadequal = false;
114                 isaheadchar = false;
115             }
116         });
117         btn_2.setOnClickListener(new View.OnClickListener() {
118             @Override
119             public void onClick(View v) {
120                 if (isaheadequal || isaheadchar) {
121                     btn_equal.setText("=");
122                     tv_calcul.setText("2");
123                 } else tv_calcul.setText(tv_calcul.getText() + "2");
124                 init = Double.parseDouble((String) tv_calcul.getText());
125                 isaheadequal = false;
126                 isaheadchar = false;
127             }
128         });
129         btn_3.setOnClickListener(new View.OnClickListener() {
130             @Override
131             public void onClick(View v) {
132                 if (isaheadequal || isaheadchar) {
133                     btn_equal.setText("=");
134                     tv_calcul.setText("3");
135                 } else tv_calcul.setText(tv_calcul.getText() + "3");
136                 init = Double.parseDouble((String) tv_calcul.getText());
137                 isaheadequal = false;
138                 isaheadchar = false;
139             }
140         });
141         btn_4.setOnClickListener(new View.OnClickListener() {
142             @Override
143             public void onClick(View v) {
144                 if (isaheadequal || isaheadchar) {
145                     btn_equal.setText("=");
146                     tv_calcul.setText("4");
147                 } else tv_calcul.setText(tv_calcul.getText() + "4");
148                 init = Double.parseDouble((String) tv_calcul.getText());
149                 isaheadequal = false;
150                 isaheadchar = false;
151             }
152         });
153         btn_5.setOnClickListener(new View.OnClickListener() {
154             @Override
155             public void onClick(View v) {
156                 if (isaheadequal || isaheadchar) {
157                     btn_equal.setText("=");
158                     tv_calcul.setText("5");
159                 } else tv_calcul.setText(tv_calcul.getText() + "5");
160                 init = Double.parseDouble((String) tv_calcul.getText());
161                 isaheadequal = false;
162                 isaheadchar = false;
163             }
164         });
165         btn_6.setOnClickListener(new View.OnClickListener() {
166             @Override
167             public void onClick(View v) {
168                 if (isaheadequal || isaheadchar) {
169                     btn_equal.setText("=");
170                     tv_calcul.setText("6");
171                 } else tv_calcul.setText(tv_calcul.getText() + "6");
172                 init = Double.parseDouble((String) tv_calcul.getText());
173                 isaheadequal = false;
174                 isaheadchar = false;
175             }
176         });
177         btn_7.setOnClickListener(new View.OnClickListener() {
178             @Override
179             public void onClick(View v) {
180                 if (isaheadequal || isaheadchar) {
181                     btn_equal.setText("=");
182                     tv_calcul.setText("7");
183                 } else tv_calcul.setText(tv_calcul.getText() + "7");
184                 init = Double.parseDouble((String) tv_calcul.getText());
185                 isaheadequal = false;
186                 isaheadchar = false;
187             }
188         });
189         btn_8.setOnClickListener(new View.OnClickListener() {
190             @Override
191             public void onClick(View v) {
192                 if (isaheadequal || isaheadchar) {
193                     btn_equal.setText("=");
194                     tv_calcul.setText("8");
195                 } else tv_calcul.setText(tv_calcul.getText() + "8");
196                 init = Double.parseDouble((String) tv_calcul.getText());
197                 isaheadequal = false;
198                 isaheadchar = false;
199             }
200         });
201         btn_9.setOnClickListener(new View.OnClickListener() {
202             @Override
203             public void onClick(View v) {
204                 if (isaheadequal || isaheadchar) {
205                     btn_equal.setText("=");
206                     tv_calcul.setText("9");
207                 } else tv_calcul.setText(tv_calcul.getText() + "9");
208                 init = Double.parseDouble((String) tv_calcul.getText());
209                 isaheadequal = false;
210                 isaheadchar = false;
211             }
212         });
213         btn_add.setOnClickListener(new View.OnClickListener() {
214             @Override
215             public void onClick(View v) {
216 
217                 switch (calcul) {
218                     case '+': {
219                         nowsum += init;
220                         break;
221                     }
222                     case '-': {
223                         nowsum -= init;
224 
225                         break;
226                     }
227                     case '*': {
228                         nowsum *= init;
229 
230                         break;
231                     }
232                     case '/': {
233                         if (init != 0) {
234                             nowsum /= init;
235                         } else
236                             break;
237                     }
238                     default: {
239                         nowsum += init;
240                     }
241                 }
242                 if (isaheadchar) {
243                     if (!isaheadsqr)
244                         tv_cord.setText(tv_cord.getText().subSequence(0, tv_cord.getText().length() - 1));
245                     calcul = '+';
246 
247                 }
248                 if (!isaheadequal) {
249                     if (!isaheadchar)
250                         tv_cord.setText(tv_cord.getText() + "" + init + "+");
251                     else tv_cord.setText(tv_cord.getText() + "+");
252                     tv_calcul.setText("" + nowsum);
253                 } else {
254                     tv_cord.setText("" + nowsum + "+");
255                     tv_calcul.setText("" + nowsum);
256                 }
257                 calcul = '+';
258                 init = 0;
259                 isaheadequal = false;
260                 isaheadchar = true;
261                 isaheadsqr = false;
262                 isnowpoint = false;
263                 pointnum = 0;
264             }
265         });
266         btn_sub.setOnClickListener(new View.OnClickListener() {
267             @Override
268             public void onClick(View v) {
269                 if (isaheadequal) {
270                     tv_cord.setText("");
271                 }
272                 switch (calcul) {
273                     case '+': {
274                         nowsum += init;
275 
276                         break;
277                     }
278                     case '-': {
279                         nowsum -= init;
280 
281                         break;
282                     }
283                     case '*': {
284                         nowsum *= init;
285                         break;
286                     }
287                     case '/': {
288                         if (init != 0) {
289                             nowsum /= init;
290                         } else
291                             break;
292                     }
293                     case '士': {
294                         nowsum *= -1;
295                         break;
296                     }
297                     case '%': {
298                         nowsum %= init;
299                         break;
300                     }
301                     default: {
302                         nowsum += init;
303                     }
304                 }
305                 if (isaheadchar) {
306                     if (!isaheadsqr)
307                         tv_cord.setText(tv_cord.getText().subSequence(0, tv_cord.getText().length() - 1));
308                     calcul = '-';
309                 }
310                 if (!isaheadequal) {
311                     if (!isaheadchar)
312                         tv_cord.setText(tv_cord.getText() + "" + init + "-");
313                     else tv_cord.setText(tv_cord.getText() + "-");
314                     tv_calcul.setText("" + nowsum);
315                 } else {
316                     tv_cord.setText("" + nowsum + "-");
317                     tv_calcul.setText("" + nowsum);
318                 }
319                 calcul = '-';
320                 init = 0;
321                 isaheadequal = false;
322                 isaheadchar = true;
323                 isaheadsqr = false;
324                 isnowpoint = false;
325                 pointnum = 0;
326             }
327         });
328         btn_mul.setOnClickListener(new View.OnClickListener() {
329             @Override
330             public void onClick(View v) {
331                 if (isaheadequal) {
332                     tv_cord.setText("");
333                 }
334                 switch (calcul) {
335                     case '+': {
336                         nowsum += init;
337                         break;
338                     }
339                     case '-': {
340                         nowsum -= init;
341                         break;
342                     }
343                     case '*': {
344                         nowsum *= init;
345                         break;
346                     }
347                     case '/': {
348                         if (init != 0) {
349                             nowsum /= init;
350                         } else
351                             break;
352                     }
353                     case '士': {
354                         nowsum *= -1;
355                         break;
356                     }
357                     case '%': {
358                         nowsum %= init;
359                         break;
360                     }
361                     default: {
362                         nowsum += init;
363                     }
364                 }
365                 //if ptr calcul is calcul change it
366                 if (isaheadchar) {
367                     if (!isaheadsqr)
368                         tv_cord.setText(tv_cord.getText().subSequence(0, tv_cord.getText().length() - 1));
369                     calcul = '*';
370                 }
371                 if (!isaheadequal) {
372                     if (!isaheadchar)
373                         tv_cord.setText(tv_cord.getText() + "" + init + "×");
374                     else tv_cord.setText(tv_cord.getText() + "×");
375                     tv_calcul.setText("" + nowsum);
376                 } else {
377                     tv_cord.setText("" + nowsum + "×");
378                     tv_calcul.setText("" + nowsum);
379                 }
380                 calcul = '*';
381                 init = 0;
382                 isaheadchar = true;
383                 isaheadequal = false;
384                 isaheadsqr = false;
385                 isnowpoint = false;
386                 pointnum = 0;
387             }
388         });
389         btn_divide.setOnClickListener(new View.OnClickListener() {
390             @Override
391             public void onClick(View v) {
392                 if (isaheadequal) {
393                     tv_cord.setText("");
394                 }
395                 switch (calcul) {
396                     case '+': {
397                         nowsum += init;
398                         break;
399                     }
400                     case '-': {
401                         nowsum -= init;
402                         break;
403                     }
404                     case '*': {
405                         nowsum *= init;
406                         break;
407                     }
408                     case '/': {
409                         if (init != 0) {
410                             nowsum /= init;
411                         } else
412                             break;
413                     }
414                     case '士': {
415                         nowsum *= -1;
416                         break;
417                     }
418                     case '%': {
419                         nowsum %= init;
420                         break;
421                     }
422                     default: {
423                         nowsum += init;
424                     }
425                 }
426                 if (isaheadchar) {
427                     if (!isaheadsqr)
428                         tv_cord.setText(tv_cord.getText().subSequence(0, tv_cord.getText().length() - 1));
429                     calcul = '/';
430                 }
431                 if (!isaheadequal) {
432                     if (!isaheadchar)
433                         tv_cord.setText(tv_cord.getText() + "" + init + "÷");
434                     else tv_cord.setText(tv_cord.getText() + "÷");
435                     tv_calcul.setText("" + nowsum);
436                 } else {
437 
438                     tv_cord.setText("" + nowsum + "÷");
439                     tv_calcul.setText("" + nowsum);
440                 }
441                 calcul = '/';
442                 init = 0;
443                 isaheadequal = false;
444                 isaheadchar = true;
445                 isaheadsqr = false;
446                 isnowpoint = false;
447                 pointnum = 0;
448             }
449         });
450         btn_point.setOnClickListener(new View.OnClickListener() {
451             @Override
452             public void onClick(View v) {
453                 tv_calcul.setText(tv_calcul.getText() + ".");
454                 isnowpoint = true;
455             }
456         });
457         btn_equal.setOnClickListener(new View.OnClickListener() {
458             @Override
459             public void onClick(View v) {
460                 if (isaheadequal) {
461                     tv_cord.setText("");
462                 }
463                 switch (calcul) {
464                     case '+': {
465                         nowsum += init;
466                         break;
467                     }
468                     case '-': {
469                         nowsum -= init;
470                         break;
471                     }
472                     case '*': {
473                         nowsum *= init;
474 
475                         break;
476                     }
477                     case '/': {
478                         if (init != 0) {
479                             nowsum /= init;
480                         } else
481                             break;
482                     }
483                     case '士': {
484                         nowsum *= -1;
485                         break;
486                     }
487                     case '%': {
488                         nowsum %= init;
489                         break;
490                     }
491                     default: {
492                         nowsum = init;
493                     }
494                 }
495                 if (!isaheadsqr)
496                     tv_cord.setText(tv_cord.getText() + "" + init + "=");
497                 else tv_cord.setText(tv_cord.getText() + "=");
498                 tv_calcul.setText("" + nowsum);
499                 calcul = '#';
500                 init = 0;
501 
502                 if (btn_equal.getText().toString().equals("完成")){
503                     new Login(context).dataIn(username,inout,type,tv_calcul.getText().toString(),btn_date.getText().toString(),et_tell.getText().toString());
504                     dialog.dismiss();
505                     Log.d("=-=-=-=----------------","传输完成"+username);
506                 }
507                 btn_equal.setText("完成");
508                 isaheadequal = true;
509                 isaheadsqr = false;
510                 isnowpoint = false;
511                 pointnum = 0;
512             }
513         });
514         btn_ac.setOnClickListener(new View.OnClickListener() {
515             @Override
516             public void onClick(View v) {
517                 tv_calcul.setText("");
518                 tv_cord.setText("");
519                 calcul = '#';
520                 init = 0;
521                 nowsum = 0;
522                 isaheadchar = false;
523                 isnowpoint = false;
524                 isaheadequal = false;
525                 isaheadsqr = false;
526                 pointnum = 0;
527             }
528         });
529         btn_del.setOnClickListener(new View.OnClickListener() {
530             @Override
531             public void onClick(View v) {
532                 if (tv_calcul.getText().length() != 0)
533                     tv_calcul.setText(tv_calcul.getText().subSequence(0, tv_calcul.getText().length() - 1));
534             }
535         });
536         final Calendar calendar = Calendar.getInstance();
537         String time = "" + calendar.get(Calendar.YEAR) + "/" + (calendar.get(Calendar.MONTH) + 1) + "/" + calendar.get(Calendar.DAY_OF_MONTH);
538         btn_date.setText(time);
539         btn_date.setOnClickListener(new View.OnClickListener() {
540             @Override
541             public void onClick(View v) {
542                 Calendar calendar1=ChineseCalendar.getInstance();
543                 DatePickerDialog datePickerDialog=new DatePickerDialog(context, AlertDialog.THEME_HOLO_LIGHT,
544                         new DatePickerDialog.OnDateSetListener() {
545                     @Override
546                     public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
547                         btn_date.setText(""+year+"/"+(month+1)+"/"+dayOfMonth);
548                     }
549                 }, calendar1.get(Calendar.YEAR), calendar1.get(Calendar.MONTH), calendar1.get(Calendar.DAY_OF_MONTH));
550                 datePickerDialog.show();
551             }
552         });
553         return (RelativeLayout) view;
554     }
555 
556     @Override
557     public void send(String s1, String s2) {
558         this.inout=s1;
559         this.type=s2;
560         Log.d("inout=================","完成");
561     }
562 
563 }
View Code

layout_fragment_calcul

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3     xmlns:tools="http://schemas.android.com/tools"
  4     android:layout_width="match_parent"
  5     android:layout_height="500dp"
  6    >
  7     <EditText
  8         android:id="@+id/tv_tell"
  9         android:layout_width="match_parent"
 10         android:layout_height="50dp"
 11         android:background="@drawable/btn_nocornor_3"
 12         android:textColor="#212"
 13         android:gravity="right"
 14         android:hint="备注"
 15         android:textColorHint="#B1A1A1"
 16         android:textSize="30dp"></EditText>
 17     <TextView
 18         android:id="@+id/tv_cord"
 19         android:layout_width="match_parent"
 20         android:layout_height="50dp"
 21         android:layout_below="@id/tv_tell"
 22         android:background="@drawable/riple_btn_nocorner_3"
 23         android:gravity="right"
 24         android:textColor="#212"
 25         android:hint="0"
 26         android:textColorHint="#B1A1A1"
 27         android:textSize="30dp"></TextView>
 28 
 29     <TextView
 30         android:id="@+id/tv_calcul"
 31         android:layout_width="match_parent"
 32         android:layout_height="50dp"
 33         android:layout_below="@+id/tv_cord"
 34         android:background="@drawable/riple_btn_nocorner_3"
 35         android:textColor="#212"
 36         android:gravity="right"
 37         android:hint="支出"
 38         android:textColorHint="#B1A1A1"
 39         android:textSize="30dp"></TextView>
 40     <View
 41         android:layout_width="match_parent"
 42         android:layout_height="0.3dp"
 43         android:background="#212"
 44         ></View>
 45     <LinearLayout
 46         android:layout_width="match_parent"
 47         android:layout_height="match_parent"
 48         android:layout_below="@id/tv_calcul"
 49         android:orientation="vertical"
 50         android:gravity="center">
 51         <LinearLayout
 52             android:layout_width="match_parent"
 53             android:layout_height="0dp"
 54             android:layout_weight="1"
 55             android:orientation="horizontal"
 56 
 57             ><Button
 58             android:id="@+id/calcul_btn_ac"
 59             android:layout_width="140dp"
 60             android:layout_height="match_parent"
 61             android:layout_marginTop="0dp"
 62             android:background="@drawable/riple_btn_nocorner_3"
 63             android:fontFamily="宋体"
 64             android:text="AC"
 65             android:textSize="20dp"
 66             android:layout_weight="2"
 67             android:textColor="#169FDF"
 68             ></Button>
 69 
 70             <Button
 71                 android:id="@+id/calcul_btn_date"
 72                 android:layout_width="210dp"
 73                 android:layout_height="match_parent"
 74                 android:layout_marginTop="0dp"
 75                 android:background="@drawable/riple_btn_nocorner_3"
 76                 android:fontFamily="宋体"
 77                 android:text="日期"
 78                 android:textAllCaps="false"
 79                 android:layout_weight="3"
 80                 android:textColor="#169FDF"
 81                 android:textSize="20dp"></Button>
 82         </LinearLayout>
 83         <LinearLayout
 84             android:layout_width="match_parent"
 85             android:layout_weight="1"
 86             android:layout_height="0dp"
 87             android:orientation="horizontal"
 88             >
 89             <Button
 90                 android:id="@+id/calcul_btn_1"
 91                 android:layout_width="70dp"
 92                 android:layout_height="match_parent"
 93                 android:layout_marginTop="0dp"
 94                 android:background="@drawable/riple_btn_nocorner_3"
 95                 android:fontFamily="宋体"
 96                 android:text="1"
 97                 android:layout_weight="1"
 98                 android:textColor="#169FDF"
 99                 android:textSize="25dp"></Button>
100             <Button
101                 android:id="@+id/calcul_btn_2"
102                 android:layout_width="70dp"
103                 android:layout_height="match_parent"
104                 android:layout_marginTop="0dp"
105                 android:background="@drawable/riple_btn_nocorner_3"
106                 android:text="2"
107                 android:layout_weight="1"
108                 android:textSize="25dp"
109                 android:textColor="#169FDF"
110                 android:fontFamily="宋体"></Button>
111             <Button
112                 android:id="@+id/calcul_btn_3"
113                 android:layout_width="70dp"
114                 android:layout_height="match_parent"
115                 android:layout_marginTop="0dp"
116                 android:background="@drawable/riple_btn_nocorner_3"
117                 android:text="3"
118                 android:textSize="25dp"
119                 android:textColor="#169FDF"
120                 android:layout_weight="1"
121                 android:fontFamily="宋体"></Button>
122 
123             <Button
124                 android:id="@+id/calcul_btn_del"
125                 android:layout_width="140dp"
126                 android:layout_height="match_parent"
127                 android:layout_marginTop="0dp"
128                 android:background="@drawable/riple_btn_nocorner_3"
129                 android:text="退格"
130                 android:textSize="20dp"
131                 android:textColor="#169FDF"
132                 android:layout_weight="2"
133                 android:textAllCaps="false"
134                 android:fontFamily="宋体"></Button>
135 
136         </LinearLayout>
137         <LinearLayout
138             android:layout_width="match_parent"
139             android:layout_weight="1"
140             android:layout_height="0dp"
141             android:orientation="horizontal"
142             >
143 
144             <Button
145                 android:id="@+id/calcul_btn_5"
146                 android:layout_width="70dp"
147                 android:layout_height="match_parent"
148                 android:layout_marginTop="0dp"
149                 android:background="@drawable/riple_btn_nocorner_3"
150                 android:text="5"
151                 android:textSize="25dp"
152                 android:textColor="#169FDF"
153                 android:layout_weight="1"
154                 android:fontFamily="宋体"></Button>
155             <Button
156                 android:id="@+id/calcul_btn_6"
157                 android:layout_width="70dp"
158                 android:layout_height="match_parent"
159                 android:layout_marginTop="0dp"
160                 android:background="@drawable/riple_btn_nocorner_3"
161                 android:text="6"
162                 android:textSize="25dp"
163                 android:textColor="#169FDF"
164                 android:layout_weight="1"
165                 android:fontFamily="宋体"></Button>
166             <Button
167                 android:id="@+id/calcul_btn_4"
168                 android:layout_width="70dp"
169                 android:layout_height="match_parent"
170                 android:layout_marginTop="0dp"
171                 android:background="@drawable/riple_btn_nocorner_3"
172                 android:text="4"
173                 android:textSize="25dp"
174                 android:layout_weight="1"
175                 android:textColor="#169FDF"
176                 android:fontFamily="宋体"></Button>
177             <Button
178                 android:id="@+id/calcul_btn_add"
179                 android:layout_width="70dp"
180                 android:layout_height="match_parent"
181                 android:layout_marginTop="0dp"
182                 android:background="@drawable/riple_btn_nocorner_3"
183                 android:text="+"
184                 android:textSize="25dp"
185                 android:textColor="#169FDF"
186                 android:fontFamily="宋体"
187                 android:layout_weight="1"
188                 ></Button>
189             <Button
190                 android:id="@+id/calcul_btn_multiply"
191                 android:layout_width="70dp"
192                 android:layout_height="match_parent"
193                 android:layout_weight="1"
194                 android:layout_marginTop="0dp"
195                 android:background="@drawable/riple_btn_nocorner_3"
196                 android:text="×"
197                 android:textSize="25dp"
198                 android:textColor="#169FDF"
199                 android:fontFamily="宋体"></Button></LinearLayout>
200         <LinearLayout
201             android:layout_width="match_parent"
202             android:layout_weight="1"
203             android:layout_height="0dp"
204             android:orientation="horizontal"
205             >
206 
207             <Button
208                 android:id="@+id/calcul_btn_7"
209                 android:layout_width="70dp"
210                 android:layout_height="match_parent"
211                 android:layout_below="@id/calcul_btn_4"
212                 android:layout_marginTop="0dp"
213                 android:layout_toLeftOf="@id/calcul_btn_5"
214                 android:background="@drawable/riple_btn_nocorner_3"
215                 android:text="7"
216                 android:textSize="25dp"
217                 android:layout_weight="1"
218                 android:textColor="#169FDF"
219                 android:fontFamily="宋体"></Button>
220             <Button
221                 android:id="@+id/calcul_btn_8"
222                 android:layout_width="70dp"
223                 android:layout_height="match_parent"
224                 android:layout_below="@id/calcul_btn_5"
225                 android:layout_marginTop="0dp"
226                 android:layout_toRightOf="@id/calcul_btn_7"
227                 android:background="@drawable/riple_btn_nocorner_3"
228                 android:text="8"
229                 android:layout_weight="1"
230                 android:textSize="25dp"
231                 android:textColor="#169FDF"
232                 android:fontFamily="宋体"></Button>
233             <Button
234                 android:id="@+id/calcul_btn_9"
235                 android:layout_width="70dp"
236                 android:layout_height="match_parent"
237                 android:layout_below="@id/calcul_btn_6"
238                 android:layout_marginTop="0dp"
239                 android:layout_toRightOf="@id/calcul_btn_8"
240                 android:background="@drawable/riple_btn_nocorner_3"
241                 android:text="9"
242                 android:layout_weight="1"
243                 android:textColor="#169FDF"
244                 android:textSize="25dp"
245                 android:fontFamily="宋体"></Button>
246             <Button
247                 android:id="@+id/calcul_btn_sub"
248                 android:layout_width="70dp"
249                 android:layout_height="match_parent"
250                 android:layout_marginTop="0dp"
251                 android:background="@drawable/riple_btn_nocorner_3"
252                 android:text="-"
253                 android:textSize="25dp"
254                 android:textColor="#169FDF"
255                 android:layout_weight="1"
256                 android:fontFamily="宋体"></Button>
257             <Button
258                 android:id="@+id/calcul_btn_divide"
259                 android:layout_width="70dp"
260                 android:layout_height="match_parent"
261                 android:layout_marginTop="0dp"
262                 android:background="@drawable/riple_btn_nocorner_3"
263                 android:layout_weight="1"
264                 android:text="÷"
265                 android:textSize="25dp"
266                 android:textColor="#169FDF"
267                 android:fontFamily="宋体"></Button>
268         </LinearLayout>
269         <LinearLayout
270             android:layout_width="match_parent"
271             android:layout_weight="1"
272             android:layout_height="0dp"
273             android:orientation="horizontal"
274             >
275             <Button
276                 android:id="@+id/calcul_btn_point"
277                 android:layout_width="70dp"
278                 android:layout_height="match_parent"
279                 android:layout_below="@id/calcul_btn_9"
280                 android:background="@drawable/riple_btn_nocorner_3"
281                 android:text="."
282                 android:gravity="center"
283                 android:textAllCaps="false"
284                 android:layout_weight="1"
285                 android:textSize="40dp"
286                 android:textColor="#169FDF"
287                 android:fontFamily="宋体"></Button>
288             <Button
289                 android:id="@+id/calcul_btn_0"
290                 android:layout_width="140dp"
291                 android:layout_height="match_parent"
292                 android:layout_below="@id/calcul_btn_8"
293                 android:layout_marginTop="0dp"
294                 android:layout_toRightOf="@id/calcul_btn_7"
295                 android:background="@drawable/riple_btn_nocorner_3"
296                 android:text="0"
297                 android:textSize="25dp"
298                 android:fontFamily="宋体"
299                 android:layout_weight="2"
300                 android:textColor="#169FDF"
301                 ></Button>
302 
303             <Button
304                 android:id="@+id/calcul_btn_equal"
305                 android:layout_width="140dp"
306                 android:layout_height="match_parent"
307                 android:layout_below="@id/calcul_btn_divide"
308                 android:layout_marginTop="0dp"
309                 android:background="@drawable/riple_btn_nocorner_3"
310                 android:text="完成"
311                 android:textSize="20dp"
312                 android:layout_weight="2"
313                 android:textColor="#169FDF"
314                 android:fontFamily="宋体"></Button>
315         </LinearLayout>
316     </LinearLayout>
317 </RelativeLayout>
View Code

  

CordFragment(记录页面,即记录信息)

 

这里用到的是recycleView放进了这个Fragment中,所以数据更新很令我头痛。

在初始化界面时从SQLite读取数据传到recycleView中显示。

recycleView里放的是TextView,可以实现跑马灯式的循环以解决一行显示不下,两行不好看的问题。

 

点击TextView后会弹出详细信息界面,在这个界面中可删除此条记录。

删除这里同样有数据更新的问题,我直接跳到了另一个activity又跳回来,以实现删除时数据的动态更新。

 CordFragment.java

  1 package com.example.didida_corder;
  2 
  3 import android.content.Context;
  4 import android.content.Intent;
  5 import android.database.Cursor;
  6 import android.database.sqlite.SQLiteDatabase;
  7 import android.os.Bundle;
  8 import android.util.Log;
  9 import android.view.LayoutInflater;
 10 import android.view.View;
 11 import android.view.ViewGroup;
 12 import android.widget.Button;
 13 import android.widget.TextView;
 14 
 15 import androidx.annotation.NonNull;
 16 import androidx.annotation.Nullable;
 17 import androidx.appcompat.app.AlertDialog;
 18 import androidx.fragment.app.Fragment;
 19 import androidx.recyclerview.widget.LinearLayoutManager;
 20 import androidx.recyclerview.widget.RecyclerView;
 21 
 22 import com.example.didida_corder.Adapter.CordAdapter;
 23 import com.example.didida_corder.R;
 24 import com.example.didida_corder.ToolClass.CustomDialog;
 25 import com.example.didida_corder.ToolClass.SQLiteUserChager;
 26 
 27 import java.util.ArrayList;
 28 import java.util.zip.Inflater;
 29 
 30 public class CordFragment extends Fragment implements CordAdapter.Clickiii {
 31     private RecyclerView recyclerView;
 32     private String[] strings;
 33     private int count;
 34     private String nowusername="defult";
 35     private int i = 0;
 36     private boolean isCreat=false;
 37     private ArrayList<String[]> arr=new ArrayList<>();
 38     public  CordFragment() {
 39 
 40     }
 41     public static CordFragment newInstance(String nowusername) {
 42 
 43         Bundle args = new Bundle();
 44         args.putString("username",nowusername);
 45         CordFragment fragment = new CordFragment();
 46         fragment.setArguments(args);
 47         return fragment;
 48     }
 49     @Override
 50     public void onAttach(@NonNull Context context) {
 51         super.onAttach(context);
 52         nowusername=getArguments().getString("username");
 53         Log.d("Cord-----------",""+nowusername);
 54     }
 55 
 56     @Override
 57     public void onResume() {
 58         super.onResume();
 59     }
 60 
 61     @Nullable
 62     @Override
 63     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
 64         View view = inflater.inflate(R.layout.layout_cordfragment, null, false);
 65 
 66         recyclerView = view.findViewById(R.id.crod_recy);
 67         recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
 68         recyclerView.setAdapter(new CordAdapter(view.getContext(), count, readCord(),this));
 69         isCreat=true;
 70         return view;
 71     }
 72 
 73 
 74     public String[] readCord() {
 75         SQLiteUserChager sqLiteUserChager = new SQLiteUserChager(getContext(), "Userr", null, 1);
 76         SQLiteDatabase sqLiteDatabase = sqLiteUserChager.getReadableDatabase();
 77         Cursor cursor = sqLiteDatabase.rawQuery("select * from Cord where username = ?", new String[]{nowusername});
 78         String[] strings = new String[count = cursor.getCount()];
 79         int i = 0;
 80         if (cursor.moveToNext())
 81             do {
 82                 String[] strings1=new String[7];
 83                 strings1[0]=cursor.getString(cursor.getColumnIndex("date"));
 84                 strings1[1]=cursor.getString(cursor.getColumnIndex("type"));
 85                 strings1[2]=cursor.getString(cursor.getColumnIndex("inout"));
 86                 strings1[3]=cursor.getString(cursor.getColumnIndex("number"));
 87                 strings1[4]=cursor.getString(cursor.getColumnIndex("info"));
 88                 strings1[5]=cursor.getString(cursor.getColumnIndex("username"));
 89                 strings1[6]=cursor.getString(cursor.getColumnIndex("id"));
 90                 arr.add(strings1);
 91                 strings[i] = "我" + cursor.getString(cursor.getColumnIndex("date")) +
 92                         " 在" + cursor.getString(cursor.getColumnIndex("type")) +
 93                         "上" + cursor.getString(cursor.getColumnIndex("inout")) +
 94                         "了" + cursor.getString(cursor.getColumnIndex("number")) + "元";
 95                 i++;
 96             } while (cursor.moveToNext());
 97         cursor.close();
 98         sqLiteDatabase.close();
 99         sqLiteUserChager.close();
100         return strings;
101     }
102 
103     @Override
104     public void onDestroy() {
105         super.onDestroy();
106 
107     }
108 
109     @Override
110     public void clik(int position) {
111         final String[] strings=arr.get(position);
112         final AlertDialog.Builder builder4=new AlertDialog.Builder(getContext());
113         LayoutInflater inflater=LayoutInflater.from(getContext());
114         View view=inflater.inflate(R.layout.layout_dialog_info,null);
115         TextView textView=view.findViewById(R.id.info_tx_2);
116         TextView textView2=view.findViewById(R.id.info_tx_3);
117         TextView textView3=view.findViewById(R.id.info_tx_4);
118         TextView textView4=view.findViewById(R.id.info_tx_5);
119         TextView textView5=view.findViewById(R.id.info_tx_6);
120         Button button=view.findViewById(R.id.btn_ok);
121         Button button1=view.findViewById(R.id.btn_delete);
122         textView.setText(strings[0]);
123         textView2.setText(strings[2]);
124         textView3.setText(strings[1]);
125         textView4.setText(strings[3]);
126         textView5.setText(strings[4]);
127         final AlertDialog alertDialog=builder4.setView(view).show();
128         alertDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
129         button.setOnClickListener(new View.OnClickListener() {
130             @Override
131             public void onClick(View v) {
132                 alertDialog.dismiss();
133             }
134         });
135         button1.setOnClickListener(new View.OnClickListener() {
136             @Override
137             public void onClick(View v) {
138                 final CustomDialog customDialog=new CustomDialog(getContext());
139                 customDialog.setMessage("确定删除?");
140                 customDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
141                 customDialog.setConfirm("确定", new CustomDialog.IOnConfirmListener() {
142                     @Override
143                     public void onConfirm(CustomDialog dialog) {
144                         SQLiteUserChager sqLiteUserChager=new SQLiteUserChager(getContext(),"Userr",null,1);
145                         SQLiteDatabase sqLiteDatabase=sqLiteUserChager.getWritableDatabase();
146                         sqLiteDatabase.execSQL("delete from Cord where id = ?",new String[]{strings[6]});
147                         sqLiteDatabase.close();
148                         sqLiteUserChager.close();
149                         Intent intent=new Intent(getActivity(),LoginActivity.class);
150                         Bundle bundle=new Bundle();
151                         bundle.putInt("refresh",1);
152                         bundle.putString("username",nowusername);
153                         Log.d("--------cord-log",nowusername);
154                         intent.putExtras(bundle);
155                         startActivity(intent);
156                     }
157                 }).setCancel("取消", new CustomDialog.IOnCancelListener() {
158                     @Override
159                     public void onCancel(CustomDialog dialog) {
160                         customDialog.dismiss();
161                     }
162                 }).show();
163 
164             }
165         });
166     }
167 }
View Code

 layout_cord_fragment    recycleview里放的是一个Textview的ltem这里就不贴了 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:id="@+id/cord_linear"
 4     android:background="#FAFAFA"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent">
 7     <androidx.recyclerview.widget.RecyclerView
 8         android:id="@+id/crod_recy"
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         >
12     </androidx.recyclerview.widget.RecyclerView>
13 </RelativeLayout>
View Code

SociatyFragment(社区页面,大家在社区中讨论省钱心得等等,尚未开发)

 

MyFragment(我的信息页面,它是一个侧滑菜单)

相关实现方法在我的这篇博客中https://www.cnblogs.com/XiaoGao128/p/12260383.html

它在初始化时同样会从数据库中读取用户昵称

点击退出登录会重新跳回登陆界面。

游客登录会显示请先登录。

 

 

 

 MyFragment.java

  1 package com.example.didida_corder;
  2 
  3 import android.content.Context;
  4 import android.content.Intent;
  5 import android.database.Cursor;
  6 import android.database.sqlite.SQLiteDatabase;
  7 import android.database.sqlite.SQLiteOpenHelper;
  8 import android.os.Bundle;
  9 import android.util.Log;
 10 import android.view.LayoutInflater;
 11 import android.view.View;
 12 import android.view.ViewGroup;
 13 import android.widget.Button;
 14 import android.widget.CheckBox;
 15 import android.widget.EditText;
 16 import android.widget.ImageView;
 17 import android.widget.LinearLayout;
 18 import android.widget.RadioButton;
 19 import android.widget.TextView;
 20 import android.widget.Toast;
 21 
 22 import androidx.annotation.NonNull;
 23 import androidx.annotation.Nullable;
 24 import androidx.appcompat.app.AlertDialog;
 25 import androidx.fragment.app.Fragment;
 26 
 27 import com.example.didida_corder.ToolClass.CustomDialog;
 28 import com.example.didida_corder.ToolClass.Login;
 29 import com.example.didida_corder.ToolClass.SPUtils;
 30 import com.example.didida_corder.ToolClass.SQLiteUserChager;
 31 
 32 import java.util.zip.Inflater;
 33 
 34 public class MyFragment extends Fragment implements View.OnClickListener {
 35     private TextView tv_like, tv_likeme, tv_depatch;
 36     private LinearLayout linearLayout;
 37     private int like, likeme, depatch;
 38     private Context context;
 39     private boolean flag = false;
 40     private TextView tv_name;
 41     private ImageView iv_head;
 42     private int type;
 43     private String nowusername="defult";
 44     private Button button_log;
 45     public MyFragment() {
 46 
 47     }
 48 //    public static String getNowusername(){
 49 //        return nowusername;
 50 //    }
 51     //在activity中设置数字
 52     public void Set(int like, int likeme, int depatch) {
 53         this.like = like;
 54         this.likeme = likeme;
 55         this.depatch = depatch;
 56         flag = true;
 57     }
 58 
 59     @Nullable
 60     @Override
 61     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
 62         View view = inflater.inflate(R.layout.layout_myfragment, null, false);
 63         tv_like = view.findViewById(R.id.myfrag_likeone);
 64         tv_likeme = view.findViewById(R.id.myfrag_likeme);
 65         tv_depatch = view.findViewById(R.id.myfrag_patch);
 66         iv_head=view.findViewById(R.id.myfrag_head);
 67         tv_name=view.findViewById(R.id.tv_name);
 68         button_log=view.findViewById(R.id.myfrag_log);
 69         linearLayout=view.findViewById(R.id.myfrag_linear_out);
 70         linearLayout.setOnClickListener(this);
 71         button_log.setOnClickListener(this);
 72         if (flag) {
 73             tv_like.setText(like);
 74             tv_likeme.setText(likeme);
 75             tv_depatch.setText(depatch);
 76         }
 77         return view;
 78     }
 79 
 80     public void sentenceAndSet(String username,int type){
 81         this.nowusername=username;
 82         this.type=type;
 83         if (type==0) {
 84             SQLiteUserChager sqLiteUserChager = new SQLiteUserChager(getContext(), "Userr", null, 1);
 85             SQLiteDatabase sqLiteDatabase = sqLiteUserChager.getReadableDatabase();
 86 
 87             Cursor cursor = sqLiteDatabase.rawQuery("select * from user where username = ?", new String[]{nowusername});
 88             cursor.moveToNext();
 89             tv_name.setText(cursor.getString(cursor.getColumnIndex("name")));
 90             button_log.setVisibility(View.INVISIBLE);
 91             tv_name.setVisibility(View.VISIBLE);
 92             iv_head.setVisibility(View.VISIBLE);
 93 
 94         }else {
 95             tv_name.setVisibility(View.INVISIBLE);
 96             iv_head.setVisibility(View.INVISIBLE);
 97             button_log.setVisibility(View.VISIBLE);
 98         }
 99     }
100     @Override
101     public void onClick(View v) {
102         switch (v.getId()){
103             case R.id.myfrag_linear_out:{
104                 CustomDialog dialog=new CustomDialog(getContext(),R.layout.layout_custom_dialog);
105                 dialog.setTitle("提示").setMessage("退出登录?").setConfirm("退出", new CustomDialog.IOnConfirmListener() {
106                     @Override
107                     public void onConfirm(CustomDialog dialog) {
108                         tv_name.setVisibility(View.INVISIBLE);
109                         iv_head.setVisibility(View.INVISIBLE);
110                         button_log.setVisibility(View.VISIBLE);
111                         nowusername="defult";
112                         SPUtils.put(getContext(),"auto",0);
113                         Intent intent=new Intent(getContext(),LoginActivity.class);
114                         startActivity(intent);
115                     }
116                 }).setCancel("算了", new CustomDialog.IOnCancelListener() {
117                     @Override
118                     public void onCancel(CustomDialog dialog) {
119                         dialog.dismiss();
120                     }
121                 }).show();
122                 break;
123             }
124             case R.id.myfrag_log:{
125                 Intent intent=new Intent(getContext(),LoginActivity.class);
126                 startActivity(intent);
127                 break;
128             }
129 
130         }
131     }
132 
133 
134 }
View Code

 layout_myfragment.xml 

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3     android:layout_width="270dp"
  4     android:layout_height="match_parent"
  5     android:background="#F4F4F4"
  6     android:orientation="vertical">
  7 
  8     <FrameLayout
  9         android:layout_width="match_parent"
 10         android:layout_height="wrap_content">
 11         <Button
 12             android:id="@+id/myfrag_log"
 13             android:layout_width="180dp"
 14             android:layout_height="60dp"
 15             android:layout_gravity="center"
 16             android:background="@drawable/riple_btn_corner_3"
 17             android:text="请先登录"
 18             android:textColor="#212"
 19             android:textSize="20dp"
 20             android:visibility="invisible"></Button>
 21         <LinearLayout
 22             android:layout_width="wrap_content"
 23             android:layout_height="match_parent"
 24             android:orientation="vertical"
 25             android:gravity="center"
 26             android:layout_marginLeft="19dp"
 27             >
 28         <ImageView
 29             android:id="@+id/myfrag_head"
 30             android:layout_width="100dp"
 31             android:layout_height="100dp"
 32             android:layout_margin="10dp"
 33             android:src="@drawable/sor"
 34             android:visibility="visible"></ImageView>
 35 
 36         <TextView
 37             android:id="@+id/tv_name"
 38             android:layout_width="wrap_content"
 39             android:layout_height="wrap_content"
 40             android:text="小高128"
 41             android:textColor="#212"
 42             android:textSize="25dp"
 43             android:layout_marginBottom="10dp"
 44             android:visibility="visible"></TextView>
 45         </LinearLayout>
 46     </FrameLayout>
 47 
 48     <View
 49         android:layout_width="300dp"
 50         android:layout_height="0.1dp"
 51         android:layout_marginTop="5dp"
 52         android:background="#B3221122"></View>
 53 
 54     <LinearLayout
 55         android:layout_width="match_parent"
 56         android:layout_height="40dp"
 57         android:orientation="horizontal">
 58 
 59         <TextView
 60             android:id="@+id/myfrag_patch"
 61             android:layout_width="wrap_content"
 62             android:layout_height="match_parent"
 63             android:layout_weight="1"
 64             android:background="#FFFFFF"
 65             android:gravity="center"
 66             android:text="0"
 67             android:textColor="#B3222222"
 68             android:textSize="25dp"></TextView>
 69 
 70         <TextView
 71             android:id="@+id/myfrag_likeone"
 72             android:layout_width="wrap_content"
 73             android:layout_height="match_parent"
 74             android:layout_weight="1"
 75             android:background="#FFFFFF"
 76             android:gravity="center"
 77             android:text="0"
 78             android:textColor="#B3222222"
 79             android:textSize="25dp"></TextView>
 80 
 81         <TextView
 82             android:id="@+id/myfrag_likeme"
 83             android:layout_width="wrap_content"
 84             android:layout_height="match_parent"
 85             android:layout_weight="1"
 86             android:background="#FFFFFF"
 87             android:gravity="center"
 88             android:text="0"
 89             android:textColor="#B3222222"
 90             android:textSize="25dp"></TextView>
 91     </LinearLayout>
 92 
 93     <LinearLayout
 94         android:layout_width="match_parent"
 95         android:layout_height="40dp"
 96         android:orientation="horizontal">
 97 
 98         <TextView
 99             android:layout_width="wrap_content"
100             android:layout_height="match_parent"
101             android:layout_weight="1"
102             android:background="#FFFFFF"
103             android:gravity="center"
104             android:text="动态"
105             android:textColor="#B3222222"
106             android:textSize="17dp"></TextView>
107 
108         <TextView
109             android:layout_width="wrap_content"
110             android:layout_height="match_parent"
111             android:layout_weight="1"
112             android:background="#FFFFFF"
113             android:gravity="center"
114             android:text="关注"
115             android:textColor="#B3222222"
116             android:textSize="17dp"></TextView>
117 
118         <TextView
119             android:layout_width="wrap_content"
120             android:layout_height="match_parent"
121             android:layout_weight="1"
122             android:background="#FFFFFF"
123             android:gravity="center"
124             android:text="粉丝"
125             android:textColor="#B3222222"
126             android:textSize="17dp"></TextView>
127 
128     </LinearLayout>
129 
130     <View
131         android:layout_width="300dp"
132         android:layout_height="0.1dp"
133         android:background="#212"></View>
134 
135     <LinearLayout
136         android:layout_width="match_parent"
137         android:layout_height="wrap_content"
138         android:background="@drawable/riple_btn_nocorner_3"
139         android:layout_marginTop="20dp"
140         android:layout_weight="1"
141         android:gravity="center_vertical"
142         android:orientation="horizontal">
143 
144         <ImageView
145             android:layout_width="25dp"
146             android:layout_height="25dp"
147             android:layout_marginLeft="20dp"
148             android:src="@drawable/xiaoxi"></ImageView>
149 
150         <RadioButton
151             android:layout_width="wrap_content"
152             android:layout_height="match_parent"
153             android:layout_marginLeft="20dp"
154             android:button="@null"
155             android:text="我的消息"
156             android:textColor="#212"
157             android:textSize="15dp"></RadioButton>
158 
159         <ImageView
160             android:layout_width="20dp"
161             android:layout_height="27dp"
162             android:layout_marginLeft="40dp"
163             android:src="@drawable/jiantou"></ImageView>
164     </LinearLayout>
165 
166     <LinearLayout
167         android:layout_width="match_parent"
168         android:background="@drawable/riple_btn_nocorner_3"
169         android:layout_height="wrap_content"
170         android:layout_marginTop="20dp"
171         android:layout_weight="1"
172         android:gravity="center_vertical"
173         android:orientation="horizontal">
174 
175         <ImageView
176             android:layout_width="28dp"
177             android:layout_height="28dp"
178             android:layout_marginLeft="20dp"
179             android:src="@drawable/shouc"></ImageView>
180 
181         <RadioButton
182             android:layout_width="wrap_content"
183             android:layout_height="match_parent"
184             android:layout_marginLeft="20dp"
185             android:button="@null"
186             android:text="我的收藏"
187             android:textColor="#212"
188             android:textSize="15dp"></RadioButton>
189 
190         <ImageView
191             android:layout_width="20dp"
192             android:layout_height="27dp"
193             android:layout_marginLeft="40dp"
194             android:src="@drawable/jiantou"></ImageView>
195     </LinearLayout>
196 
197     <LinearLayout
198         android:layout_width="match_parent"
199         android:layout_height="wrap_content"
200         android:layout_marginTop="20dp"
201         android:background="@drawable/radio"
202         android:layout_weight="1"
203         android:gravity="center_vertical"
204         android:orientation="horizontal">
205 
206         <ImageView
207             android:layout_width="25dp"
208             android:layout_height="25dp"
209             android:layout_marginLeft="20dp"
210             android:src="@drawable/kefu"></ImageView>
211 
212         <RadioButton
213             android:layout_width="wrap_content"
214             android:layout_height="match_parent"
215             android:layout_marginLeft="20dp"
216             android:button="@null"
217             android:text="联系客服"
218             android:textColor="#212"
219             android:textSize="15dp"
220             ></RadioButton>
221 
222         <ImageView
223             android:layout_width="20dp"
224             android:layout_height="27dp"
225             android:layout_marginLeft="40dp"
226             android:src="@drawable/jiantou"></ImageView>
227     </LinearLayout>
228 
229     <LinearLayout
230         android:id="@+id/myfrag_linear_out"
231         android:layout_width="match_parent"
232         android:layout_height="wrap_content"
233         android:layout_marginTop="20dp"
234         android:layout_weight="1"
235         android:background="@drawable/riple_btn_nocorner_3"
236         android:gravity="center_vertical"
237         android:orientation="horizontal">
238 
239         <ImageView
240             android:layout_width="25dp"
241             android:layout_height="25dp"
242             android:layout_marginLeft="20dp"
243             android:src="@drawable/tuichu"></ImageView>
244 
245         <TextView
246             android:id="@+id/rd_out"
247             android:layout_width="wrap_content"
248             android:layout_height="match_parent"
249             android:layout_marginLeft="20dp"
250             android:gravity="center_vertical"
251             android:button="@null"
252             android:text="退出登录"
253             android:textColor="#212"
254             android:textSize="15dp"></TextView>
255 
256         <ImageView
257             android:layout_width="20dp"
258             android:layout_height="27dp"
259             android:layout_marginLeft="40dp"
260             android:src="@drawable/jiantou"></ImageView>
261     </LinearLayout>
262 </LinearLayout>
View Code

 一个SharePerference工具类

SPUtils.java

 1 package com.example.didida_corder.ToolClass;
 2 
 3 import android.content.Context;
 4 import android.content.SharedPreferences;
 5 
 6 import androidx.annotation.Nullable;
 7 
 8 import java.util.Map;
 9 
10 public class SPUtils {
11     /**
12      * 保存在手机里的SP文件名
13      */
14     public static final String FILE_NAME = "my_sp";
15 
16     /**
17      * 保存数据
18      */
19     public static SPUtils put(Context context, String key, Object obj) {
20         SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE);
21         SharedPreferences.Editor editor = sp.edit();
22         if (obj instanceof Boolean) {
23             editor.putBoolean(key, (Boolean) obj);
24         } else if (obj instanceof Float) {
25             editor.putFloat(key, (Float) obj);
26         } else if (obj instanceof Integer) {
27             editor.putInt(key, (Integer) obj);
28         } else if (obj instanceof Long) {
29             editor.putLong(key, (Long) obj);
30         } else {
31             editor.putString(key, (String) obj);
32         }
33         editor.commit();
34         SPUtils spUtils=new SPUtils();
35         return spUtils;
36     }
37 
38 
39     /**
40      * 获取指定数据
41      */
42     public static Object get(Context context, String key, @Nullable Object defaultObj) {
43         SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE);
44         if (defaultObj instanceof Boolean) {
45             return sp.getBoolean(key, (Boolean) defaultObj);
46         } else if (defaultObj instanceof Float) {
47             return sp.getFloat(key, (Float) defaultObj);
48         } else if (defaultObj instanceof Integer) {
49             return sp.getInt(key, (Integer) defaultObj);
50         } else if (defaultObj instanceof Long) {
51             return sp.getLong(key, (Long) defaultObj);
52         } else if (defaultObj instanceof String) {
53             return sp.getString(key, (String) defaultObj);
54         }
55         return null;
56     }
57 
58     /**
59      * 删除指定数据
60      */
61     public static void remove(Context context, String key) {
62         SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE);
63         SharedPreferences.Editor editor = sp.edit();
64         editor.remove(key);
65         editor.commit();
66     }
67 
68 
69     /**
70      * 返回所有键值对
71      */
72     public static Map<String, ?> getAll(Context context) {
73         SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE);
74         Map<String, ?> map = sp.getAll();
75         return map;
76     }
77 
78     /**
79      * 删除所有数据
80      */
81     public static void clear(Context context) {
82         SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE);
83         SharedPreferences.Editor editor = sp.edit();
84         editor.clear();
85         editor.commit();
86     }
87 
88     /**
89      * 检查key对应的数据是否存在
90      */
91     public static boolean contains(Context context, String key) {
92         SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE);
93         return sp.contains(key);
94     }
95 
96 }
View Code

  

 

猜你喜欢

转载自www.cnblogs.com/XiaoGao128/p/12288703.html