今日总结-安卓日期控件
今天主要学习了,安卓日期控件的使用。
日期控件数据库的获取。
代码如下:
package com.example.dailyclokingapp; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.app.AlarmManager; import android.app.AlertDialog; import android.app.DatePickerDialog; import android.app.PendingIntent; import android.app.TimePickerDialog; import android.content.ContentValues; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.TextView; import android.widget.TimePicker; import android.widget.Toast; import java.util.Calendar; public class dakaActivity extends AppCompatActivity implements View.OnClickListener { private Button btn; private AlarmManager alarmManager; //闹钟管理器 private int year1; private int month1; private int day1; private String userid; private Button btnjiao; private EditText zongjie, guanjian; private TextView showInfo, changtian, shangda, jiantian; private MyDbHelper myDbHelper;//全局变量 private SQLiteDatabase db; private int hourOfDay1 = 0; private int minute1 = 0; @SuppressLint("MissingInflatedId") @Override protected void onCreate(Bundle savedInstanceState) { Calendar calendar = Calendar.getInstance(); int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); super.onCreate(savedInstanceState); setContentView(R.layout.activity_daka); initView(); alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); btn = (Button) findViewById(R.id.set_clock); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { setClock(view); } }); myDbHelper = new MyDbHelper(dakaActivity.this, "infor", null, 666); //获取按钮 Button buttonOk = findViewById(R.id.buttonOk); //设置按钮点击监听器 buttonOk.setOnClickListener(new MyOnClickListener()); btnjiao.setOnClickListener(this); Intent intent = this.getIntent(); userid = intent.getStringExtra("userid"); lastdaka(); changdaka(); liandaka(); } public void setClock(View view) { //获取当前系统时间 Calendar calendar = Calendar.getInstance(); int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); //弹出闹钟框 TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { Calendar c = Calendar.getInstance(); //获取日期对象 c.set(Calendar.HOUR_OF_DAY, hourOfDay); //设置闹钟小时数 c.set(Calendar.MINUTE, minute); //设置闹钟分钟数 Intent intent = new Intent(dakaActivity.this, AlarmReceiver.class); //创建pendingIntent PendingIntent pendingIntent = PendingIntent.getBroadcast(dakaActivity.this, 0X102, intent, 0); //设置闹钟 alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent); Toast.makeText(dakaActivity.this, "闹钟设置成功", Toast.LENGTH_SHORT).show(); } }, hour, minute, true); timePickerDialog.show(); } private void initView() { btnjiao = findViewById(R.id.btnjiao); zongjie = findViewById(R.id.zongjie); guanjian = findViewById(R.id.guanjian); changtian = findViewById(R.id.changtian); shangda = findViewById(R.id.shangda); jiantian = findViewById(R.id.jiantian); //showInfo = findViewById(R.id.showInfo); } //showInfo.setText("查询结果如下:"); //while (cursor1.moveToNext()) { //showInfo.append("\n" + "用户名:" + cursor1.getString(0) + ",密码:" + cursor1.getString(1)); // } public void lastdaka() { int year2 = 0; int month2 = 0; int day2 = 0; db = myDbHelper.getWritableDatabase(); Cursor cursor = db.rawQuery("select year,month,day from infor where userid=?", new String[]{userid}); while (cursor.moveToNext()) { year2 = cursor.getInt(0); month2 = cursor.getInt(1); day2 = cursor.getInt(2); } shangda.append(year2 + "-" + month2 + "-" + day2); cursor.close(); db.close(); } public void changdaka() { String userid1 = ""; int num = 0; db = myDbHelper.getWritableDatabase(); Cursor cursor = db.rawQuery("select userid from infor where userid=?", new String[]{userid}); while (cursor.moveToNext()) { userid1 = cursor.getString(0); if (userid1.equals(userid)) { num++; } } String num1 = String.valueOf(num); changtian.setText(num1); cursor.close(); db.close(); } public void liandaka() { String userid1 = ""; int num = 0; db = myDbHelper.getWritableDatabase(); Cursor cursor = db.rawQuery("select userid from infor where userid=?", new String[]{userid}); while (cursor.moveToNext()) { userid1 = cursor.getString(0); if (userid1.equals(userid)) { num++; } } String num1 = String.valueOf(num); jiantian.setText(num1); cursor.close(); db.close(); } public void onClick(View v) { switch (v.getId()) { case R.id.btnjiao: db = myDbHelper.getWritableDatabase(); String zongjie1 = zongjie.getText().toString(); String keyword1 = guanjian.getText().toString(); ContentValues contentValues = new ContentValues(); contentValues.put("userid", userid); contentValues.put("zongjie", zongjie1); contentValues.put("keyword", keyword1); contentValues.put("year", year1); contentValues.put("month", month1); contentValues.put("day", day1); long num = db.insert("infor", null, contentValues); if (num > 0) { Toast.makeText(dakaActivity.this, "打卡成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(dakaActivity.this, "打卡失败", Toast.LENGTH_SHORT).show(); } db.close(); shangda.setText(" "); lastdaka(); liandaka(); changdaka(); break; } } //定义按钮点击监听器 class MyOnClickListener implements View.OnClickListener { //按钮点击 @Override public void onClick(View view) { if (view.getId() == R.id.buttonOk) {//被点击的是确认按钮 //弹窗 DatePickerDialog pickerDialog = new DatePickerDialog(dakaActivity.this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { String date = String.format("%d-%d-%d", year, month + 1, dayOfMonth); EditText editText = findViewById(R.id.editText); editText.setText(date); year1 = year; month1 = month + 1; day1 = dayOfMonth; } }, 2018, 11, 11); pickerDialog.show(); } } } class MyDbHelper extends SQLiteOpenHelper { //构造器作用,参数含义:上下文,数据库文件名称,结果集工厂,版本号 public MyDbHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); } //数据库初始化的时候,用于创建表或者视图文件 @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table infor(id integer primary key autoincrement,userid varchar(255),keyword varchar(255),zongjie varchar(255),year int,month int,day int)"); } //升级方法 @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } } }