android开发实战-记账本APP(二)
<p><span style="font-family: 楷体; font-size: 18px">继昨天的开发,继续完成今天的内容。</span></p><p><span style="font-family: 楷体; font-size: 18px">(一)开始构建一些业务逻辑,开始构建记账本的添加一笔记账的功能。</span></p>
<p><span style="font-family: 楷体; font-size: 18px">①对fab按钮的click时间进行修改,创建一个</span><span style="font-family: 楷体; font-size: 18px">AlertDialog.Builder对象,因此我们给dialog添加一个布局,命名为new_cost_data.xml,位于他的布局很简单,我们只需要设置两个编辑框,和一个时间选择框即可。</span></p>
<p><span style="font-family: 楷体; font-size: 18px">new_cost_data.xml</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)"><?</span><span style="color: rgba(255, 0, 255, 1)">xml version="1.0" encoding="utf-8"</span><span style="color: rgba(0, 0, 255, 1)">?></span>
<span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">LinearLayout </span><span style="color: rgba(255, 0, 0, 1)">xmlns:android</span><span style="color: rgba(0, 0, 255, 1)">="http://schemas.android.com/apk/res/android"</span><span style="color: rgba(255, 0, 0, 1)">
android:orientation</span><span style="color: rgba(0, 0, 255, 1)">="vertical"</span><span style="color: rgba(255, 0, 0, 1)"> android:layout_width</span><span style="color: rgba(0, 0, 255, 1)">="match_parent"</span><span style="color: rgba(255, 0, 0, 1)">
android:layout_height</span><span style="color: rgba(0, 0, 255, 1)">="match_parent"</span><span style="color: rgba(255, 0, 0, 1)">
android:gravity</span><span style="color: rgba(0, 0, 255, 1)">="center"</span><span style="color: rgba(0, 0, 255, 1)">></span>
<span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">EditText
</span><span style="color: rgba(255, 0, 0, 1)">android:id</span><span style="color: rgba(0, 0, 255, 1)">="@+id/et_cost_title"</span><span style="color: rgba(255, 0, 0, 1)">
android:layout_width</span><span style="color: rgba(0, 0, 255, 1)">="match_parent"</span><span style="color: rgba(255, 0, 0, 1)">
android:layout_height</span><span style="color: rgba(0, 0, 255, 1)">="wrap_content"</span><span style="color: rgba(255, 0, 0, 1)">
android:layout_margin</span><span style="color: rgba(0, 0, 255, 1)">="4dp"</span><span style="color: rgba(255, 0, 0, 1)">
android:hint</span><span style="color: rgba(0, 0, 255, 1)">="Cost Title"</span><span style="color: rgba(0, 0, 255, 1)">/></span>
<span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">EditText
</span><span style="color: rgba(255, 0, 0, 1)">android:id</span><span style="color: rgba(0, 0, 255, 1)">="@+id/et_cost_money"</span><span style="color: rgba(255, 0, 0, 1)">
android:layout_width</span><span style="color: rgba(0, 0, 255, 1)">="match_parent"</span><span style="color: rgba(255, 0, 0, 1)">
android:layout_height</span><span style="color: rgba(0, 0, 255, 1)">="wrap_content"</span><span style="color: rgba(255, 0, 0, 1)">
android:layout_margin</span><span style="color: rgba(0, 0, 255, 1)">="4dp"</span><span style="color: rgba(255, 0, 0, 1)">
android:hint</span><span style="color: rgba(0, 0, 255, 1)">="Cost Money"</span><span style="color: rgba(0, 0, 255, 1)">/></span>
<span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">DatePicker
</span><span style="color: rgba(255, 0, 0, 1)">android:id</span><span style="color: rgba(0, 0, 255, 1)">="@+id/dp_cost_data"</span><span style="color: rgba(255, 0, 0, 1)">
android:layout_width</span><span style="color: rgba(0, 0, 255, 1)">="wrap_content"</span><span style="color: rgba(255, 0, 0, 1)">
android:layout_height</span><span style="color: rgba(0, 0, 255, 1)">="wrap_content"</span><span style="color: rgba(255, 0, 0, 1)">
android:layout_margin</span><span style="color: rgba(0, 0, 255, 1)">="4dp"</span><span style="color: rgba(255, 0, 0, 1)">
android:datePickerMode</span><span style="color: rgba(0, 0, 255, 1)">="spinner"</span><span style="color: rgba(255, 0, 0, 1)">
android:calendarViewShown</span><span style="color: rgba(0, 0, 255, 1)">="false"</span><span style="color: rgba(0, 0, 255, 1)">/></span>
<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">LinearLayout</span><span style="color: rgba(0, 0, 255, 1)">></span></pre>
</div>
<p><img src="https://img2018.cnblogs.com/i-beta/1717524/202002/1717524-20200204114326443-149495134.png" alt=""></p>
<p> </p>
<p> </p>
<p><span style="font-family: 楷体; font-size: 18px"> ②然后回到主程序,在fab点击事件里创建一个 </span><span style="font-family: 楷体; font-size: 18px">LayoutInflater用他来创建view,将新布局传到新建立的View对象,这样就可以获取到布局文件的内容,获取后将该布局设置给dialog对象,然后需要给dialog设置监听事件,当点击ok的监听事件,就将从布局中获取的信息传给一个CostBean对象,在用数据库进行添加,收集数据的</span><span style="font-family: 楷体; font-size: 18px">mCostBeanList对象也要进行添加,最后再进行刷新(</span><span style="font-family: 楷体; font-size: 18px">mAdapter.notifyDataSetChanged();</span><span style="font-family: 楷体; font-size: 18px">),点击cancel就会取消该view界面,最后不要忘了显示该dialog。</span></p>
<p><span style="font-family: 楷体"><span style="font-size: 18px">MainActivity代码:</span></span></p>
<p> </p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> com.example.xlfbook;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.content.DialogInterface;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.content.Intent;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.database.Cursor;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.os.Bundle;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.google.android.material.floatingactionbutton.FloatingActionButton;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.google.android.material.snackbar.Snackbar;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> androidx.appcompat.app.AlertDialog;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> androidx.appcompat.app.AppCompatActivity;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> androidx.appcompat.widget.Toolbar;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.os.Parcelable;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.view.LayoutInflater;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.view.View;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.view.Menu;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.view.MenuItem;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.widget.DatePicker;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.widget.EditText;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.widget.ListView;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.Serializable;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.ArrayList;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.List;
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> MainActivity <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> AppCompatActivity {
</span><span style="color: rgba(0, 0, 255, 1)">private</span> List<CostBean><span style="color: rgba(0, 0, 0, 1)"> mCostBeanList;
</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> DatabaseHelper mdatabaseHelper;
</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> CostListAdapter mAdapter;
@Override
</span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onCreate(Bundle savedInstanceState) {
</span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar </span>=<span style="color: rgba(0, 0, 0, 1)"> findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mdatabaseHelper</span>=<span style="color: rgba(0, 0, 255, 1)">new</span> DatabaseHelper(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">);
mCostBeanList</span>=<span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<><span style="color: rgba(0, 0, 0, 1)">();
ListView costList</span>=<span style="color: rgba(0, 0, 0, 1)">(ListView)findViewById(R.id.lv_main);
initCostData();
mAdapter </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> CostListAdapter(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">, mCostBeanList);
costList.setAdapter(mAdapter);
FloatingActionButton fab </span>=<span style="color: rgba(0, 0, 0, 1)"> findViewById(R.id.fab);
fab.setOnClickListener(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> View.OnClickListener() {
@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onClick(View view) {
AlertDialog.Builder builder</span>=<span style="color: rgba(0, 0, 255, 1)">new</span> AlertDialog.Builder(MainActivity.<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">);
LayoutInflater inflater</span>=LayoutInflater.from(MainActivity.<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">);
View viewDialog</span>=inflater.inflate(R.layout.new_cost_data,<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">final</span> EditText title=<span style="color: rgba(0, 0, 0, 1)">(EditText) viewDialog.findViewById(R.id.et_cost_title);
</span><span style="color: rgba(0, 0, 255, 1)">final</span> EditText money=<span style="color: rgba(0, 0, 0, 1)">(EditText)viewDialog.findViewById(R.id.et_cost_money);
</span><span style="color: rgba(0, 0, 255, 1)">final</span> DatePicker date=<span style="color: rgba(0, 0, 0, 1)">(DatePicker) viewDialog.findViewById(R.id.dp_cost_data);
builder.setView(viewDialog);
builder.setTitle(</span>"New Cost"<span style="color: rgba(0, 0, 0, 1)">);
builder.setPositiveButton(</span>"OK", <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> DialogInterface.OnClickListener() {
@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onClick(DialogInterface dialog, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> which) {
CostBean costBean</span>=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> CostBean();
costBean.costTitle</span>=<span style="color: rgba(0, 0, 0, 1)">title.getText().toString();
costBean.costMoney</span>=<span style="color: rgba(0, 0, 0, 1)">money.getText().toString();
costBean.costDate</span>=date.getYear()+"-"+(date.getMonth()+1)+"-"+<span style="color: rgba(0, 0, 0, 1)">date.getDayOfMonth();
mdatabaseHelper.insertCost(costBean);
mCostBeanList.add(costBean);
mAdapter.notifyDataSetChanged();
}
});
builder.setNegativeButton(</span>"Cancel",<span style="color: rgba(0, 0, 255, 1)">null</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">点击取消</span>
builder.create().show();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">显示dialog的布局</span>
<span style="color: rgba(0, 0, 0, 1)"> }
});
}
</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> initCostData() {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> mdatabaseHelper.deleteAllData();</span>
<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> CostBean costBean=new CostBean();
for (int i=0;i<6;i++) {
costBean.costTitle=i+"imooc";
costBean.costDate="11-11";
costBean.costMoney="20";
mdatabaseHelper.insertCost(costBean);
}</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
Cursor cursor</span>=<span style="color: rgba(0, 0, 0, 1)">mdatabaseHelper.getAllCostData();
</span><span style="color: rgba(0, 0, 255, 1)">if</span>(cursor!=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){
</span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)">(cursor.moveToNext()){
CostBean costBean1</span>=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> CostBean();
costBean1.costTitle</span>=cursor.getString(cursor.getColumnIndex("cost_title"<span style="color: rgba(0, 0, 0, 1)">));
costBean1.costDate</span>=cursor.getString(cursor.getColumnIndex("cost_date"<span style="color: rgba(0, 0, 0, 1)">));
costBean1.costMoney</span>=cursor.getString(cursor.getColumnIndex("cost_money"<span style="color: rgba(0, 0, 0, 1)">));
mCostBeanList.add(costBean1);
}
cursor.close();
}
}
@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> onCreateOptionsMenu(Menu menu) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Inflate the menu; this adds items to the action bar if it is present.</span>
<span style="color: rgba(0, 0, 0, 1)"> getMenuInflater().inflate(R.menu.menu_main, menu);
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
}
@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> onOptionsItemSelected(MenuItem item) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Handle action bar item clicks here. The action bar will
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> automatically handle clicks on the Home/Up button, so long
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> as you specify a parent activity in AndroidManifest.xml.</span>
<span style="color: rgba(0, 0, 255, 1)">int</span> id =<span style="color: rgba(0, 0, 0, 1)"> item.getItemId();
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">noinspection SimplifiableIfStatement</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> (id ==<span style="color: rgba(0, 0, 0, 1)"> R.id.action_chart) {
Intent intent</span>=<span style="color: rgba(0, 0, 255, 1)">new</span> Intent(MainActivity.<span style="color: rgba(0, 0, 255, 1)">this</span>,ChartsActivity.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
intent.putExtra(</span>"cost_list"<span style="color: rgba(0, 0, 0, 1)">, (Serializable) mCostBeanList);
startActivity(intent);
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onOptionsItemSelected(item);
}
}</span></pre>
</div>
<p> </p>
<p><span style="font-family: 楷体; font-size: 18px">接下来简单的添加一笔帐单的功能已经可以实现,我们来添加一笔账单试试效果。</span></p>
<p><span style="font-family: 楷体; font-size: 18px"><img src="https://img2018.cnblogs.com/i-beta/1717524/202002/1717524-20200204120342408-1894695316.png" alt="" width="329" height="587"></span></p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> <img src="https://img2018.cnblogs.com/i-beta/1717524/202002/1717524-20200204120414693-958018429.png" alt="" width="324" height="578"></p>
<p> </p>
<p> </p>
<p><span style="font-family: 楷体; font-size: 18px"> (二)开始制作图表</span></p>
<p><span style="font-family: 楷体; font-size: 18px">图表的制作我们会用到第三方库hellocharts,具体可以登录该网址查看————https://github.com/lecho/hellocharts-android</span></p>
<p><span style="font-family: 楷体; font-size: 18px">接下来在Android Studio中导入该库,在build中的dependencies中导入该github就可以了</span></p>
<p><span style="font-family: 楷体; font-size: 18px"><img src="https://img2018.cnblogs.com/i-beta/1717524/202002/1717524-20200204122403859-1847256474.png" alt="" width="933" height="303"></span></p>
<p> </p>
<p> </p>
<p><span style="font-family: 楷体; font-size: 18px"> 接下来首先创建一个布局文件,来展示我们的chart图表。</span></p>
<p><span style="font-family: 楷体; font-size: 18px">chart_view</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)"><?</span><span style="color: rgba(255, 0, 255, 1)">xml version="1.0" encoding="utf-8"</span><span style="color: rgba(0, 0, 255, 1)">?></span>
<span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">LinearLayout </span><span style="color: rgba(255, 0, 0, 1)">xmlns:android</span><span style="color: rgba(0, 0, 255, 1)">="http://schemas.android.com/apk/res/android"</span><span style="color: rgba(255, 0, 0, 1)">
android:orientation</span><span style="color: rgba(0, 0, 255, 1)">="vertical"</span><span style="color: rgba(255, 0, 0, 1)"> android:layout_width</span><span style="color: rgba(0, 0, 255, 1)">="match_parent"</span><span style="color: rgba(255, 0, 0, 1)">
android:layout_height</span><span style="color: rgba(0, 0, 255, 1)">="match_parent"</span><span style="color: rgba(255, 0, 0, 1)">
android:padding</span><span style="color: rgba(0, 0, 255, 1)">="10dp"</span><span style="color: rgba(0, 0, 255, 1)">></span>
<span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">lecho.lib.hellocharts.view.LineChartView
</span><span style="color: rgba(255, 0, 0, 1)">android:id</span><span style="color: rgba(0, 0, 255, 1)">="@+id/chart"</span><span style="color: rgba(255, 0, 0, 1)">
android:padding</span><span style="color: rgba(0, 0, 255, 1)">="20dp"</span><span style="color: rgba(255, 0, 0, 1)">
android:layout_width</span><span style="color: rgba(0, 0, 255, 1)">="match_parent"</span><span style="color: rgba(255, 0, 0, 1)">
android:layout_height</span><span style="color: rgba(0, 0, 255, 1)">="match_parent"</span> <span style="color: rgba(0, 0, 255, 1)">/></span>
<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">LinearLayout</span><span style="color: rgba(0, 0, 255, 1)">></span></pre>
</div>
<p><span style="font-family: 楷体; font-size: 18px">然后再创建一个ChartsActivity.java,再修改主程序中的右上角的内容,在上面主程序的代码中可以看到,就是下面代码,把之前的setting改为chart</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> onOptionsItemSelected(MenuItem item) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Handle action bar item clicks here. The action bar will
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> automatically handle clicks on the Home/Up button, so long
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> as you specify a parent activity in AndroidManifest.xml.</span>
<span style="color: rgba(0, 0, 255, 1)">int</span> id =<span style="color: rgba(0, 0, 0, 1)"> item.getItemId();
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">noinspection SimplifiableIfStatement</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> (id ==<span style="color: rgba(0, 0, 0, 1)"> R.id.action_chart) {
Intent intent</span>=<span style="color: rgba(0, 0, 255, 1)">new</span> Intent(MainActivity.<span style="color: rgba(0, 0, 255, 1)">this</span>,ChartsActivity.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
intent.putExtra(</span>"cost_list"<span style="color: rgba(0, 0, 0, 1)">, (Serializable) mCostBeanList);
startActivity(intent);
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onOptionsItemSelected(item);
}</span></pre>
</div>
<p><span style="font-family: 楷体; font-size: 18px">更改menu_main中的信息</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">menu </span><span style="color: rgba(255, 0, 0, 1)">xmlns:android</span><span style="color: rgba(0, 0, 255, 1)">="http://schemas.android.com/apk/res/android"</span><span style="color: rgba(255, 0, 0, 1)">
xmlns:app</span><span style="color: rgba(0, 0, 255, 1)">="http://schemas.android.com/apk/res-auto"</span><span style="color: rgba(255, 0, 0, 1)">
xmlns:tools</span><span style="color: rgba(0, 0, 255, 1)">="http://schemas.android.com/tools"</span><span style="color: rgba(255, 0, 0, 1)">
tools:context</span><span style="color: rgba(0, 0, 255, 1)">="com.example.xlfbook.MainActivity"</span><span style="color: rgba(0, 0, 255, 1)">></span>
<span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">item
</span><span style="color: rgba(255, 0, 0, 1)">android:id</span><span style="color: rgba(0, 0, 255, 1)">="@+id/action_chart"</span><span style="color: rgba(255, 0, 0, 1)">
android:orderInCategory</span><span style="color: rgba(0, 0, 255, 1)">="100"</span><span style="color: rgba(255, 0, 0, 1)">
android:title</span><span style="color: rgba(0, 0, 255, 1)">="Charts"</span><span style="color: rgba(255, 0, 0, 1)">
app:showAsAction</span><span style="color: rgba(0, 0, 255, 1)">="never"</span> <span style="color: rgba(0, 0, 255, 1)">/></span>
<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">menu</span><span style="color: rgba(0, 0, 255, 1)">></span></pre>
</div>
<p><span style="font-family: 楷体; font-size: 18px">ChartsActivity.java</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> com.example.xlfbook;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.app.Activity;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.content.Intent;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.graphics.Color;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.os.Bundle;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> androidx.annotation.Nullable;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.ArrayList;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.HashMap;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.List;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.Map;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.TreeMap;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> lecho.lib.hellocharts.model.Axis;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> lecho.lib.hellocharts.model.AxisValue;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> lecho.lib.hellocharts.model.ChartData;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> lecho.lib.hellocharts.model.Line;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> lecho.lib.hellocharts.model.LineChartData;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> lecho.lib.hellocharts.model.PointValue;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> lecho.lib.hellocharts.model.ValueShape;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> lecho.lib.hellocharts.util.ChartUtils;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> lecho.lib.hellocharts.view.Chart;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> lecho.lib.hellocharts.view.LineChartView;
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> ChartsActivity <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> Activity {
</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> LineChartView mChart;
</span><span style="color: rgba(0, 0, 255, 1)">private</span> Map<String,Integer> table = <span style="color: rgba(0, 0, 255, 1)">new</span> TreeMap<><span style="color: rgba(0, 0, 0, 1)">();
</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> LineChartData mData;
@Override
</span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onCreate(@Nullable Bundle savedInstanceState) {
</span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onCreate(savedInstanceState);
setContentView(R.layout.chart_view);
mChart </span>=<span style="color: rgba(0, 0, 0, 1)"> (LineChartView) findViewById(R.id.chart);
mData </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> LineChartData();
List</span><CostBean> allDate = (List<CostBean>) getIntent().getSerializableExtra("cost_list"<span style="color: rgba(0, 0, 0, 1)">);
generateValues(allDate);
generateData();
}
</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> generateData() {
List</span><Line> lines = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<><span style="color: rgba(0, 0, 0, 1)">();
List</span><PointValue> values = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<><span style="color: rgba(0, 0, 0, 1)">();
</span><span style="color: rgba(0, 0, 255, 1)">int</span> indexX = 0<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)">(Integer value : table.values()){
values.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> PointValue(indexX, value));
indexX</span>++<span style="color: rgba(0, 0, 0, 1)">;
}
Line line </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Line(values);
line.setColor(ChartUtils.COLORS[</span>0<span style="color: rgba(0, 0, 0, 1)">]);
line.setShape(ValueShape.CIRCLE);
line.setPointColor(ChartUtils.COLORS[</span>1<span style="color: rgba(0, 0, 0, 1)">]);
lines.add(line);
mData.setLines(lines);
mChart.setLineChartData(mData);
}
</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span> generateValues(List<CostBean><span style="color: rgba(0, 0, 0, 1)"> allDate) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span>(allDate != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i < allDate.size(); i++<span style="color: rgba(0, 0, 0, 1)">) {
CostBean costBean </span>=<span style="color: rgba(0, 0, 0, 1)"> allDate.get(i);
String costDate </span>=<span style="color: rgba(0, 0, 0, 1)"> costBean.costDate;
</span><span style="color: rgba(0, 0, 255, 1)">int</span> costMoney =<span style="color: rgba(0, 0, 0, 1)"> Integer.parseInt(costBean.costMoney);
</span><span style="color: rgba(0, 0, 255, 1)">if</span>(!<span style="color: rgba(0, 0, 0, 1)">table.containsKey(costDate)){
table.put(costDate,costMoney);
}</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">int</span> originMoney =<span style="color: rgba(0, 0, 0, 1)"> table.get(costDate);
table.put(costDate,originMoney </span>+<span style="color: rgba(0, 0, 0, 1)"> costMoney);
}
}
}
}
}</span></pre>
</div>
<p>这就是由我们的数据生成的折线图,还有待完善(坐标线还没搞明白怎么添加)</p>
<p><img src="https://img2018.cnblogs.com/i-beta/1717524/202002/1717524-20200204124400574-897397153.png" alt="" width="358" height="638"></p>
<p> </p>
<p> </p>
<p> </p>
<p>至此,该记账本的简单记账功能以及图表显示功能得以实现。在真机上经过测试,可以使用。</p>
<p><img src="https://img2018.cnblogs.com/common/1717524/202002/1717524-20200204124817731-253360363.jpg" alt="" width="667" height="1408"></p>
<p> </p>
<p> <img src="https://img2018.cnblogs.com/common/1717524/202002/1717524-20200204124851925-1751838011.jpg" alt="" width="666" height="1406"></p>
<p> </p>
<p><span style="font-family: 楷体; font-size: 18px">github:https://github.com/happy-xlf/NoteBook</span></p>
<p> </p><br><br>
来源:https://www.cnblogs.com/xiaofengzai/p/12259109.html
頁:
[1]