android studio闹钟
时间: 2023-12-11 20:33:22 浏览: 105
以下是使用Android Studio实现闹钟的步骤:
1.在AndroidManifest.xml文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.SET_ALARM"/>
```
2.在布局文件中添加一个TimePicker和一个Button:
```xml
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/buttonSetAlarm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set Alarm" />
```
3.在MainActivity.java文件中添加以下代码:
```java
public class MainActivity extends AppCompatActivity {
TimePicker timePicker;
Button buttonSetAlarm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timePicker = findViewById(R.id.timePicker);
buttonSetAlarm = findViewById(R.id.buttonSetAlarm);
buttonSetAlarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int hour = timePicker.getCurrentHour();
int minute = timePicker.getCurrentMinute();
Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM);
intent.putExtra(AlarmClock.EXTRA_HOUR, hour);
intent.putExtra(AlarmClock.EXTRA_MINUTES, minute);
startActivity(intent);
}
});
}
}
```
4.运行应用程序并设置闹钟时间,然后单击“Set Alarm”按钮即可设置闹钟。
阅读全文