每日总结 3.5
今天学习了页面显示和闹钟的设定。
使用list view显示数据。闹钟仍在研究中。
public class MainActivity extends AppCompatActivity { @SuppressLint("MissingInflatedId") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RadioButton rbYes = findViewById(R.id.rb_yes); RadioButton rbNo = findViewById(R.id.rb_no); rbYes.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startRemind(); } }); rbNo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { stopRemind(); } }); } /** * 开启提醒 */ private void startRemind(){ //得到日历实例,主要是为了下面的获取时间 Calendar mCalendar = Calendar.getInstance(); mCalendar.setTimeInMillis(System.currentTimeMillis()); //获取当前毫秒值 long systemTime = System.currentTimeMillis(); //是设置日历的时间,主要是让日历的年月日和当前同步 mCalendar.setTimeInMillis(System.currentTimeMillis()); // 这里时区需要设置一下,不然可能个别手机会有8个小时的时间差 mCalendar.setTimeZone(TimeZone.getTimeZone("GMT+8")); //设置在几点提醒 设置的为点 mCalendar.set(Calendar.HOUR_OF_DAY, 14); //设置在几分提醒 设置的为分 mCalendar.set(Calendar.MINUTE, 46); //下面这两个看字面意思也知道 mCalendar.set(Calendar.SECOND, 0); mCalendar.set(Calendar.MILLISECOND, 0); //上面设置的就是13点25分的时间点 //获取上面设置的13点25分的毫秒值 long selectTime = mCalendar.getTimeInMillis(); // 如果当前时间大于设置的时间,那么就从第二天的设定时间开始 if(systemTime > selectTime) { mCalendar.add(Calendar.DAY_OF_MONTH, 1); } //AlarmReceiver.class为广播接受者 Intent intent = new Intent(MainActivity.this, AlarmReceiver.class); PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0); //得到AlarmManager实例 AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); //**********注意!!下面的两个根据实际需求任选其一即可********* /** * 单次提醒 * mCalendar.getTimeInMillis() 上面设置的13点25分的时间点毫秒值 */ am.set(AlarmManager.RTC_WAKEUP, mCalendar.getTimeInMillis(), pi); /** * 重复提醒 * 第一个参数是警报类型;下面有介绍 * 第二个参数网上说法不一,很多都是说的是延迟多少毫秒执行这个闹钟,但是我用的刷了MIUI的三星手机的实际效果是与单次提醒的参数一样,即设置的13点25分的时间点毫秒值 * 第三个参数是重复周期,也就是下次提醒的间隔 毫秒值 我这里是一天后提醒 */ am.setRepeating(AlarmManager.RTC_WAKEUP, mCalendar.getTimeInMillis(), (1000 * 60 ), pi); } /** * 关闭提醒 */ private void stopRemind(){ Intent intent = new Intent(MainActivity.this, AlarmReceiver.class); PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0); AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); //取消警报 am.cancel(pi); Toast.makeText(this, "关闭了提醒", Toast.LENGTH_SHORT).show(); }
页面显示:
public class Seesome extends AppCompatActivity { private SimpleCursorAdapter mAdapter; private ListView listView; private Button btn_add; private Button btn_delete; private Button btn_back; private EditText et_date; private EditText et_gj; private EditText et_zj; private EditText et_jc; private EditText et_max; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_seesome); btn_add = (Button) findViewById(R.id.btn_add); btn_delete = (Button) findViewById(R.id.btn_delete); listView = (ListView) findViewById(R.id.list); btn_add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addData(); refleshListView(); } }); btn_delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { deleteData(); refleshListView(); } }); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // Toast.makeText(MainActivity.this,""+position,Toast.LENGTH_SHORT).show(); int positions = position + 1; Help db = new Help(Seesome.this); SQLiteDatabase dbWrite = db.getWritableDatabase(); dbWrite.delete("daka","_id=?",new String[]{""+positions}); dbWrite.close(); refleshListView(); } }); }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/btn_add" android:text="显示信息" android:textSize="20sp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_delete" android:text="删除所有" android:textSize="20sp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/main_top" android:padding="10dp" android:divider="@null" android:dividerHeight="6dp" android:scrollbars="none" android:background="@color/hui"/> <Button android:id="@+id/btn_back" android:text="确定" android:textSize="20sp" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <TextView android:id="@+id/tv_expense_date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="20sp" android:padding="10dp"></TextView> <TextView android:id="@+id/tv_expense_gj" android:layout_width="70dp" android:layout_height="35dp" android:textSize="20sp"></TextView> </LinearLayout>