我有此代码,我制作了2个按钮,一个设置了一个警报,一个用于检查剩下的时间直到警报响起。目前,我正在使用检查计时器的第二个按钮。有人可以帮我吗?
这是我的代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
Button button2 = (Button) findViewById(R.id.button2);
final TextView lefty = (TextView)findViewById(R.id.left);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
Long preTime = calendar.getTimeInMillis();
calendar.add(Calendar.HOUR_OF_DAY,8);
Long postTime = calendar.getTimeInMillis();
Long delay = postTime - preTime;
Intent intent = new Intent(getApplicationContext(),NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
CountDownTimer timer = new CountDownTimer(delay, 1) {
@Override
public void onTick(long millisUntilFinished) {
final int seconds = (int) (millisUntilFinished / 1000) % 60;
final int minutes = (int) ((millisUntilFinished / (1000 * 60)) % 60);
final int hour = (int) ((millisUntilFinished / (1000 * 60 * 60)) % 24);
runOnUiThread(new Runnable() {
@Override
public void run() {
lefty.setText( hour + " : " + minutes + " : " + seconds);
}
});
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
}
};
timer.start();
}
});
button2.setOnClickListener(new View.OnClickListener() {
"some code"
}
}
为做我的答案并帮助您,我将完成您的代码,并添加一个示例的文本视图。
public class MainActivity extends AppCompatActivity {
// modify here
private Calendar myTimer = null;
private TextView myTextView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
Button button2 = (Button) findViewById(R.id.button2);
final TextView lefty = (TextView)findViewById(R.id.left);
// modify here
myTextView = (TextView) findViewById(R.id.my_text_view);
myTimer = Calendar.getInstance();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
Long preTime = calendar.getTimeInMillis();
calendar.add(Calendar.HOUR_OF_DAY, 8);
Long postTime = calendar.getTimeInMillis();
Long delay = postTime - preTime;
Intent intent = new Intent(getApplicationContext(), NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
CountDownTimer timer = new CountDownTimer(delay, 1) {
@Override
public void onTick(long millisUntilFinished) {
final int seconds = (int) (millisUntilFinished / 1000) % 60;
final int minutes = (int) ((millisUntilFinished / (1000 * 60)) % 60);
final int hour = (int) ((millisUntilFinished / (1000 * 60 * 60)) % 24);
// modify here
myTimer.set(Calendar.SECOND, seconds);
myTimer.set(Calendar.MINUTE, minutes);
myTimer.set(Calendar.HOUR, hour);
runOnUiThread(new Runnable() {
@Override
public void run() {
lefty.setText(hour + " : " + minutes + " : " + seconds);
}
});
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
}
};
timer.start();
}
});
// modify here
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String myString = new SimpleDateFormat("hh : mm : ss").format(myTimer);
myTextView.setText(myString);
}
});
}
}