寒假答应给女友做一个记录月经周期的app,由于我太vegetable,写的比较辣鸡,大家凑活着看(手动笑哭)
首先要跟大家说的是,
android开发 要明白开发思想:逻辑和视图是分开的,视图就是你这个桌面上有什么组件,
逻辑就是你想要组件之间有什么联系,比如点击时间之类的。(比如网页,有前端有后台,分开的这种方式为开发提供了很大的便利)
开发app有四种方式:
利用Datepickerdialog进行获取日期(图形化的界面很舒服)
利用轻量级存储SharedPreference进行存储数据(SharedPreference可以实现永久存储,不会在内存中流失)
遇到的问题以及大家需要注意的:
在用SharedPreference进行存储数据的时候,putString方法的参数是Map类型的,也就是键值对,所以我们这里用了计数器来表示存储数据的条数,这样对于查看所有的数据提供了方便,在删除数据的时候也只需要让这个计数器-1就行了。
小插曲:
若要设置全屏可以在style文件中新建主题 如下所示:
然后并没有完成,因为你要在AndroidManifest.xml文件中配置这个主题,如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/NoActivityFULLscreen">//不同的是这里,这里的名字改成刚才设定的主题名称
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
intent-filter>
activity>
<activity android:name=".Main2Activity">activity>
application>
manifest>
就可以啦~
app功能说明:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.test.MainActivity"
android:background="@mipmap/timg">//这里是设置背景的,提前把名为timg的图片放到mipmap中
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
android:id="@+id/shanchu"
android:text="删除"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
android:id="@+id/look"
android:text="查看记录"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
android:id="@+id/insert"
android:text="插入"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:textSize="30sp"
android:typeface="sans"
android:scrollbars="vertical"
/>
RelativeLayout>
java代码如下:
package com.example.test;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button shanchu ;
private Button insert;
private Button look;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*****与xml配置文件中的组件进行绑定*****/
shanchu =findViewById(R.id.shanchu);
insert = findViewById(R.id.insert);
look = findViewById(R.id.look);
textView = findViewById(R.id.textview);
/********设置监听器**********/
shanchu.setOnClickListener(this);
insert.setOnClickListener(this);
look.setOnClickListener(this);
}
@Override/****重写OnClick方法******/
public void onClick(View v) {
switch (v.getId())
{
case R.id.shanchu:
{
delete();
break;
}
case R.id.look:
{
showinTextView();
break;
}
case R.id.insert:
{
showDatePickDlg();
break;
}
}
}
protected void showDatePickDlg()//展现日期选择框,并将日期存储到SharePreference中
{
Calendar calendar = Calendar.getInstance();
DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
String string = year + " " + (monthOfYear+1) + " " + dayOfMonth;//将日期字符串用Strig你先存储进来
SharedPreferences sharedPreferences=getSharedPreferences("123",MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit(); //获得editor对象
int i =sharedPreferences.getInt("index",0);//这个是计数的变量,方便取存
editor.putString("Calander"+(++i),string);//由于putstring的参数是map,所以必须要解决key键值的问题,因为要存储多条信息。
editor.putInt("index",i);
editor.commit();//更改sharedpreference的相关信息
}
}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show();
}
protected void delete()//通过让计数器减1达到删除的目的
{
SharedPreferences sharedPreferences=getSharedPreferences("123",MODE_PRIVATE);
int Count = sharedPreferences.getInt("index",0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("index",Count-1);
editor.commit();
}
/*****通过stringbuffer字符串构造器将sharedpreference中的数据放到一个字符串中,然后再setText到Textview中***/
protected void showinTextView()
{
//获得SharedPreference对象
SharedPreferences sharedPreferences=getSharedPreferences("123",MODE_PRIVATE);
//获得editor对象
int count = sharedPreferences.getInt("index",0);
StringBuffer stringBuffer = new StringBuffer();//字符串构造器主要是把每一条数据集中到一块
int j=0;
while(count>=j)
{
String s = sharedPreferences.getString("Calander"+j,"");
stringBuffer.append(s+" ");
j++;
}
textView.setText(stringBuffer.toString());
}
}