团队冲刺第六天-KeepRunningAPP
摘要:今天完成了闹钟的添加功能,过程还算顺利,但是在最后实现长按删除闹钟事件的使用遇到了问题,无法实现长按删除,由于在闹钟列表里使用了Switch开关控件,所以我感觉和这个有关系,于是拿掉了Switch控件测试了一下,果然实现了长按删除,具体什么原因还不清楚,打算明天解决,然后再美化一下界面啥的,然后学习闹钟的触发事件。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.example.naozhong; import android.app.TimePickerDialog; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.TimePicker; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import java.util.Calendar; public class AddAlarm extends AppCompatActivity implements View.OnClickListener { private TextView hour; private TextView minute1; private Button setTime; private EditText content; private Button save; private Calendar calendar; private AlarmOperator alarmOperator; private AlarmAdapter alarmAdapter; String hourformat; String minuteformat; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.addalarm); alarmOperator=new AlarmOperator(this); hour = findViewById(R.id.hour); minute1 = findViewById(R.id.minute); setTime = findViewById(R.id.set_time); setTime.setOnClickListener(this); content = findViewById(R.id.content); save = findViewById(R.id.save); save.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.set_time: addtime(); break; case R.id.save: addalarm(); break; } } private void addalarm() { String userhour=hour.getText().toString().trim(); String userminute=minute1.getText().toString().trim(); String usercontent=content.getText().toString().trim(); Alarm bean=new Alarm("2",userhour,userminute,usercontent,0); Log.d("volley", bean.hour); alarmOperator.insert(bean); Intent intent2 = new Intent(this, AlarmActivity.class); startActivity(intent2); } private void addtime() { calendar = Calendar.getInstance(); int mhour = calendar.get(Calendar.HOUR_OF_DAY); int mminute = calendar.get(Calendar.MINUTE); new TimePickerDialog(AddAlarm.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { calendar.set(Calendar.HOUR_OF_DAY, hourOfDay); calendar.set(Calendar.MINUTE, minute); Calendar currentTime=Calendar.getInstance(); if(calendar.getTimeInMillis()<=currentTime.getTimeInMillis()){ calendar.setTimeInMillis(calendar.getTimeInMillis()+24*60*60*1000); } hourformat = String.format(String.valueOf(hourOfDay)); minuteformat = String.format(String.valueOf(minute)); Toast.makeText(AddAlarm.this, "" + hourformat + ":" + minuteformat, Toast.LENGTH_SHORT).show(); hour.setText(hourformat); minute1.setText(minuteformat); } }, mhour, mminute, true).show(); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.alarm); addalarm=findViewById(R.id.addalarm); list=new ArrayList<>(); String username="2"; alarmOperator =new AlarmOperator(this); ListView alarmList=(ListView) findViewById(R.id.clock_list); alarmList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) { AlertDialog.Builder builder=new AlertDialog.Builder(view.getContext()); builder.setMessage("确定删除?"); builder.setTitle("提示"); final Alarm[] alarms = {null}; builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { alarms[0]=list.get(position); alarmOperator.deletebyuser("1",alarms[0].hour,alarms[0].minute); list.remove(position); alarmAdapter.notifyDataSetChanged(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.create().show(); return false; } }); init(username); alarmAdapter=new AlarmAdapter(this,list); alarmList.setAdapter(alarmAdapter); addalarm.setOnClickListener(l); }