标签:creat ima ini sig pos rgs activity splay package
今天在昨天的基础上,增加了打卡的功能实现,点击打卡,显示打卡成功。根据不同时间段的日期,会有返回当前日期提醒。遇到的问题:当天打卡后,不可以时时更新显示在日历上,只有返回别的界面或者重选日期再次回到当前日期时才会显示打卡标志。计划明天完成这个功能。
相关代码:
package com.example.rili.sign; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import java.util.ArrayList; import java.util.List; public class SignInHelper { private static final String TAG="SignInHelper--->>>"; private SQLiteDatabase db; public SignInHelper(Context context){ db=new LocalDbOpenHelper(context).getReadableDatabase(); } public void insert(String signIn){ String sql = "insert into signIn values(‘"+signIn+"‘)"; db.execSQL(sql); } public List<String> query(int year,int month) { String mid = month > 9 ? "-" : "-0"; String selection = "date like ?"; String[] selectArgs = new String[]{"%" + year + mid + month + "%"}; Cursor cursor = db.query("signIn",null,selection,selectArgs,null,null,null); List<String> result=new ArrayList<>(); while (cursor.moveToNext()){ String date=cursor.getString(cursor.getColumnIndex("date")); result.add(date); } cursor.close(); return result; } }
package com.example.rili; import androidx.appcompat.app.AppCompatActivity; import android.app.DatePickerDialog; import android.content.Context; import android.content.DialogInterface; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.DatePicker; import android.widget.TextView; import android.widget.Toast; import com.example.rili.view.MonPickerDialog; import com.example.rili.view.SignView; import java.util.Calendar; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity--->>>"; private SignView signDate; private TextView tvYear; private Button sign; private Context context = this; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { sign = findViewById(R.id.btn_sign); signDate = findViewById(R.id.signDate); tvYear = signDate.findViewById(R.id.tvYear); signDate.init(); if(signDate.isSign()) { sign.setBackgroundColor(Color.GRAY); sign.setText("今日已打卡"); sign.setClickable(false); }else { sign.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { handle(); } }); } tvYear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showMonPicker(); } }); } private void handle() { signDate.signIn(new DateAdapter.OnSignListener() { @Override public void OnSignedSucceed() { showToast("打卡成功"); sign.setBackgroundColor(Color.GRAY); sign.setText("今日已打卡"); sign.setClickable(false); } @Override public void OnSignedFail() { showToast("打卡失败"); } }); } private void showMonPicker() { final Calendar localCalendar = Calendar.getInstance(); localCalendar.setTime(DateUtil.strToDate("yyyy-MM", tvYear.getText().toString())); MonPickerDialog dialog = new MonPickerDialog(context,new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { localCalendar.set(Calendar.YEAR, year); localCalendar.set(Calendar.MONTH, monthOfYear); localCalendar.set(Calendar.DATE, dayOfMonth); } }, localCalendar.get(Calendar.YEAR), localCalendar.get(Calendar.MONTH),localCalendar.get(Calendar.DATE)); dialog.show(); dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { tvYear.setText(DateUtil.calendarToDateTime(localCalendar, "yyyy-MM")); refresh(localCalendar.get(Calendar.YEAR),localCalendar.get(Calendar.MONTH)+1); } }); } private void refresh(int year,int month) { if(year!=DateUtil.year || month!=DateUtil.month){ signDate.init(year,month); sign.setBackgroundColor(getResources().getColor(R.color.sky_blue)); sign.setText("返回当前日期"); sign.setClickable(true); sign.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { initView(); } }); } } private void showToast(String msg){ Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); } }
运行截图:
标签:creat ima ini sig pos rgs activity splay package
原文地址:https://www.cnblogs.com/hhjing/p/12770062.html