使用Androidstudio完成简单的理财app
使用投资APP进行简单理财 #生活常识# #日常生活技巧# #理财规划小贴士#
文章目录 前言一、实现效果二、具体代码1.底部导航栏切换2.MainActivity.class3.使用内部存储实现注册4.检验账号密码登陆5.新建一个数据库6.在Secondfragment里添加数据7.在Firstfragment里查询与删除数据8.在Myfragment里新增功能选项9.查看股市功能10.日记功能11.拍照功能 三、小结前言
使用Androidstudio算是比较落后的一种技术,不过应课程要求还是得学学,期末弄了一个记账理财的app,在此分享一下。
话不多说上效果及代码。
一、实现效果
1、实现登陆注册
2、实现记账查账
3、其他功能选项
4、查看今日股市
5、写日记功能
6、拍照(但没有写访问存储权限,使之保存到手机内存里)
二、具体代码
1.底部导航栏切换
import android.os.Bundle; import android.support.v4.app.FragmentTabHost; import android.support.v7.app.AppCompatActivity; import android.widget.TabHost; import java.util.ArrayList; import java.util.List; public class MenuActivity extends AppCompatActivity { private List<TabItem> mFragmentList; private FragmentTabHost mFragmentTabHost; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.menu_layout); initTabItemData(); } private void initTabItemData() { mFragmentList = new ArrayList<>(); mFragmentList.add(new TabItem( R.drawable.icons8box64, R.drawable.icons8box2, "添加消费信息", SecondFragment.class, R.color.colorAccent)); mFragmentList.add(new TabItem( R.drawable.icons8calendar64, R.drawable.icons8calendar64, "查看消费信息", FirstFragment.class, R.color.colorAccent)); mFragmentList.add(new TabItem( R.drawable.icons8menu, R.drawable.icons8menu, "其他功能", MyFragment.class, R.color.colorAccent)); mFragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); mFragmentTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent); for (int i = 0; i < mFragmentList.size(); i++) { TabItem tabItem = mFragmentList.get(i); TabHost.TabSpec tabSpec = mFragmentTabHost.newTabSpec( tabItem.getTabText()). setIndicator(tabItem.getTabView(MenuActivity.this)); mFragmentTabHost.addTab(tabSpec, tabItem.getFragmentClass(), null); mFragmentTabHost.getTabWidget().setBackgroundResource(R.drawable.bottom_bar8); if (i == 0) { tabItem.setChecked(true); } else { tabItem.setChecked(false); } } mFragmentTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() { @Override public void onTabChanged(String tabId) { for (int i = 0; i < mFragmentList.size(); i++) { TabItem tabItem = mFragmentList.get(i); if (tabId.equals(tabItem.getTabText())) { tabItem.setChecked(true); } else { tabItem.setChecked(false); } } } }); } } //新建一个用于选中图标的类 import android.content.Context; import android.graphics.Color; import android.support.v4.app.Fragment; import android.view.View; import android.widget.ImageView; import android.widget.TextView; class TabItem { private final int mSelectTextcolor; private ImageView mIvTab; private int imageNormal; private int imageSelected; private TextView mTvTab; private String tabText; private Class<? extends Fragment> fragmentClass; private View mTabView; public TabItem(int imageNormal, int imageSelected, String text, Class<? extends Fragment> fragmentClass,int selectTextcolor) { this.imageNormal = imageNormal; this.imageSelected = imageSelected; this.tabText = text; this.fragmentClass = fragmentClass; mSelectTextcolor = selectTextcolor; } public Class<? extends Fragment> getFragmentClass() { return fragmentClass; } public String getTabText() { return tabText; } public void setChecked(boolean checked) { if (checked) { mTvTab.setTextColor(mSelectTextcolor); mIvTab.setImageResource(imageSelected); } else { mTvTab.setTextColor(Color.WHITE); mIvTab.setImageResource(imageNormal); } } public View getTabView(Context context) { mTabView = View.inflate(context, R.layout.tab, null); mIvTab = (ImageView) mTabView.findViewById(R.id.iv_tab); mTvTab = (TextView) mTabView.findViewById(R.id.tv_tab); mIvTab.setImageResource(imageNormal); mTvTab.setText(tabText); return mTabView; } }
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071082.MainActivity.class
import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends AppCompatActivity { public Button bt; Button bt1; public EditText name; public EditText password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt = (Button) findViewById(R.id.login_button); bt1 = (Button) findViewById(R.id.login_button1); bt.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { name = (EditText) findViewById(R.id.name); password = (EditText) findViewById(R.id.password); String myName = name.getText().toString(); String mypassword = password.getText().toString();// 获取文本框输入信息 Intent intent = new Intent(); intent.setClass(MainActivity.this, Checkpassword.class);// 指定接下来要启动的class Bundle bundle = new Bundle(); bundle.putString("name", myName); bundle.putString("password", mypassword); intent.putExtras(bundle);// 封装数据准备传递 startActivity(intent);// 调用一个新的页面 } }); bt1.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(); intent.setClass(MainActivity.this, Register.class);// 指定接下来要启动的class Bundle bundle = new Bundle(); startActivity(intent);// 调用一个新的页面 } }); } }
12345678910111213141516171819202122232425262728293031323334353637383940414243443.使用内部存储实现注册
```cpp import android.content.SharedPreferences; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class Register extends AppCompatActivity { EditText myUsername, myPassword; static final String KEY1 = "userName"; static final String KEY2 = "userPass"; SharedPreferences preferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.register); myUsername = (EditText)findViewById(R.id.et_name); myPassword = (EditText)findViewById(R.id.et_password); preferences =getSharedPreferences("REGISTER",MODE_PRIVATE); final SharedPreferences.Editor editor = preferences.edit(); findViewById(R.id.btn_register).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { editor.putString(KEY1, myUsername.getText().toString());//写入用户名 editor.putString(KEY2, myPassword.getText().toString());//写入密码 if (editor.commit()){ Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_SHORT).show(); Register.this.finish(); } } }); } }
1234567891011121314151617181920212223242526272829303132333435364.检验账号密码登陆
import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; public class Checkpassword extends Activity { static final String KEY1 = "userName"; static final String KEY2 = "userPass"; SharedPreferences preferences; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);// 保存当前页面状态信息 Bundle bundle = this.getIntent().getExtras();// 获取前一个页面传递来的数据 String myName = bundle.getString("name"); String mypassword = bundle.getString("password"); preferences =getSharedPreferences("REGISTER",MODE_PRIVATE); String name = preferences.getString(KEY1, "大人"); String pass = preferences.getString(KEY2, "111111"); if ((myName.equals(name) & mypassword.equals(pass))) {//密码规则在这里面设置 setContentView(R.layout.menu_layout); Intent intent = new Intent(); intent.setClass(Checkpassword.this, MenuActivity.class);//指定接下来要启动的class startActivity(intent); } else { setContentView(R.layout.failloginlayout);// 显示下一跳页面 } } }
1234567891011121314151617181920212223242526275.新建一个数据库
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class MyDBOpenHelper extends SQLiteOpenHelper { public MyDBOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context,name ,factory, version); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE account(" + "_id INTEGER PRIMARY KEY AUTOINCREMENT," + "title TEXT ," + "money TEXT," + "date TEXT);");//创建数据表名为account及相关列名; } @Override public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) { _db.execSQL("DROP TABLE IF EXISTS account"); onCreate(_db); } }
123456789101112131415161718192021226.在Secondfragment里添加数据
import android.app.Notification; import android.app.NotificationManager; import android.content.ContentValues; import android.database.sqlite.SQLiteDatabase; import android.graphics.BitmapFactory; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.NotificationCompat; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; import static android.content.Context.NOTIFICATION_SERVICE; public class SecondFragment extends Fragment { private EditText et_cost_title,et_cost_money; private DatePicker dp_cost_date; private SQLiteDatabase dbWriter; private MyDBOpenHelper dbOpenHelper; private NotificationManager manager; private int SIMPLE_NOTFICATION_ID=1600; NotificationCompat.Builder builder; public SecondFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_second, container, false); et_cost_title = view.findViewById(R.id.et_cost_title); et_cost_money = view.findViewById(R.id.et_cost_money); dp_cost_date = view.findViewById(R.id.dp_cost_date); Button btn_add = view.findViewById(R.id.btn_Add); dbOpenHelper = new MyDBOpenHelper(getActivity(), "account.db", null, 1); dbWriter = dbOpenHelper.getWritableDatabase(); builder=new NotificationCompat.Builder(getActivity()); manager = (NotificationManager)getActivity(). getSystemService(NOTIFICATION_SERVICE); btn_add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String aaaa=et_cost_title.getText().toString(); int bbbb=Integer.parseInt(et_cost_money.getText().toString()); String dateStr = dp_cost_date.getYear() + "-" + (dp_cost_date.getMonth() + 1) + "-" + dp_cost_date.getDayOfMonth(); ContentValues cv = new ContentValues(); cv.put("title", aaaa); cv.put("money", bbbb); cv.put("date", dateStr); dbWriter.insert("account", null, cv); builder.setSmallIcon(R.mipmap.ic_launcher); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); builder.setContentTitle("有新的消费内容"); builder.setContentText("建议开源节流,好好过日子!"); builder.setShowWhen(true); builder.setAutoCancel(true); Notification notification = builder.build(); manager.notify(SIMPLE_NOTFICATION_ID, notification); }}); return view; } }
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162637.在Firstfragment里查询与删除数据
import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.widget.CursorAdapter; import android.support.v4.widget.SimpleCursorAdapter; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class FirstFragment extends Fragment { TextView tv_id,tv_title,tv_date,tv_money,textView2,tv_output,tv_month; ListView listview; EditText id_et; Button chaxun; private MyDBOpenHelper dbOpenHelper; private SQLiteDatabase dbWriter,dbReader; private SimpleCursorAdapter adapter; public FirstFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_first, container, false); Button personal=view.findViewById(R.id.chaxun); Button delete=view.findViewById(R.id.btn_delete); id_et=view.findViewById(R.id.id_et); tv_id= view.findViewById(R.id.tv_id); tv_title = view.findViewById(R.id.tv_title); tv_date = view.findViewById(R.id.tv_date); tv_money = view.findViewById(R.id.tv_money); textView2=view.findViewById(R.id.textView2); listview = view.findViewById(R.id.listview); tv_output=view.findViewById(R.id.tv_output); dbOpenHelper = new MyDBOpenHelper(getActivity(), "account.db", null, 1); dbReader = dbOpenHelper.getReadableDatabase(); dbWriter=dbOpenHelper.getWritableDatabase(); personal.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Cursor c=dbReader.rawQuery("SELECT sum(money) as zongjiner FROM account",null); c.moveToFirst(); tv_output.setText(c.getString(c.getColumnIndex("zongjiner"))); Cursor cursor = dbReader.rawQuery("SELECT * FROM account",null); if (!cursor.moveToFirst()) { Toast.makeText(getActivity(), "小主,您消费情况还未记录哦!!", Toast.LENGTH_LONG).show(); } adapter = new SimpleCursorAdapter(getActivity(), R.layout.list_item, cursor, new String[]{"_id", "title", "money", "date"}, new int[]{R.id.tv_id, R.id.tv_title, R.id.tv_money, R.id.tv_date}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); listview.setAdapter(adapter); } }); delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int cccc=Integer.parseInt(id_et.getText().toString()); String dddd=Integer.toString(cccc); dbWriter.delete("account", "_id=?", new String[]{dddd}); } }); return view; } }
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576778.在Myfragment里新增功能选项
import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.Button; public class MyFragment extends Fragment { public MyFragment() { // Required empty public constructor } @Override public void onCreateOptionsMenu(Menu menu ,MenuInflater inflater) { inflater.inflate(R.menu.mymenu, menu); super.onCreateOptionsMenu(menu,inflater); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()){ case R.id.exit: getActivity().finish(); return true; } return super.onOptionsItemSelected(item); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_my, container, false); Button seek=view.findViewById(R.id.btn_gushi); Button photo=view.findViewById(R.id.btn_life); Button diary1=view.findViewById(R.id.btn_book); setHasOptionsMenu(true); seek.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(); intent.setClass(getActivity(),SeekWeb.class); startActivity(intent); } }); photo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(); intent.setClass(getActivity(),TakePhoto.class); startActivity(intent); } }); diary1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(); intent.setClass(getActivity(),DiaryBook.class); startActivity(intent); } }); return view; } }
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364659.查看股市功能
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.KeyEvent; import android.view.View; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Button; public class SeekWeb extends AppCompatActivity { private WebView mywebview; private Button back; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.seek_web); mywebview = (WebView) findViewById(R.id.webview); mywebview.getSettings().setJavaScriptEnabled(true); mywebview.loadUrl("http://quote.eastmoney.com/zs000001.html?from=BaiduAladdin"); mywebview.setWebViewClient(new HelloWebViewClient ()); back=(Button)findViewById(R.id.btn_back); back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SeekWeb.this.finish(); } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && mywebview.canGoBack()) { mywebview.goBack(); return true; } return false; } private class HelloWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } } }
1234567891011121314151617181920212223242526272829303132333435363738394041424310.日记功能
import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.method.ScrollingMovementMethod; import android.view.MotionEvent; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.io.FileInputStream; import java.io.FileOutputStream; public class DiaryBook extends AppCompatActivity { EditText etFileName,etWriteText; TextView tvRead; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.diary); etWriteText = (EditText)findViewById(R.id.et_write); tvRead = (TextView)findViewById(R.id.tv_reading); findViewById(R.id.btn_write).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { tvRead.setText(""); try { FileOutputStream fos = openFileOutput("myDataFile.txt", Context.MODE_APPEND); fos.write(etWriteText.getText().toString().getBytes()); fos.close(); Toast.makeText(getApplicationContext(),"嘻嘻,再多写点呗!", Toast.LENGTH_LONG).show(); } catch (Exception e) { e.printStackTrace();}}}); findViewById(R.id.btn_look).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { FileInputStream fis = openFileInput("myDataFile.txt"); byte[] buffer = new byte[fis.available()]; fis.read(buffer); String fileContent = new String(buffer); fis.close(); tvRead.setText("\n"+ fileContent+"\n"); }catch (Exception e) { e.printStackTrace(); }}}); tvRead.setMovementMethod(new ScrollingMovementMethod()); tvRead.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { tvRead.getParent().requestDisallowInterceptTouchEvent(true); return false; } }); } }
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565711.拍照功能
import android.content.Intent; import android.graphics.Bitmap; import android.os.Bundle; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import java.util.Set; public class TakePhoto extends AppCompatActivity implements View.OnClickListener { private ImageView imageView; Button Btn1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.takephoto); imageView=(ImageView)this.findViewById(R.id.myimage); Btn1= (Button)this.findViewById(R.id.btn_capture); Btn1.setOnClickListener(this); } public void onClick(View v){ Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, 1); } public void onActivityResult(int requestCode, int resultCode, Intent intent1){ if(resultCode == RESULT_OK){ Bundle extras = intent1.getExtras(); Set<String> myset= extras.keySet(); for(String str:myset){ Log.d("ok", str.toString()); } Bitmap bmp = (Bitmap) extras.get("data"); imageView.setImageBitmap(bmp); } } }
123456789101112131415161718192021222324252627282930313233343536373839注:布局文件较多,不过挺简单的,看着效果图应该可以知道。
三、小结
在完成这个app时,总会遇到一些莫名其妙的错误,通过看报错原因,然后查资料都能解决。这个app包含的知识点皆为课堂所学,有些简单,在此记录一下,有借鉴价值会更好哦!
网址:使用Androidstudio完成简单的理财app https://www.yuejiaxmz.com/news/view/424615
相关内容
探索智能家居的未来:简易智能家居APP项目推荐好用钱,管理你的财务如此简单
三星生活助手APP的账单管理功能如何使用?
有没有什么好用的理财APP – PingCode
财务管理app哪个好
金融理财app
理财小白必读:6个简单有效的理财技巧
金融理财app大全
金融理财app下载
手机理财软件哪个好 理财APP排行榜