android 开发热点代码
时间: 2023-07-09 08:47:05 浏览: 58
以下是一些Android开发中常用的热点代码:
1. 创建新的Activity:
```
Intent intent = new Intent(this, NewActivity.class);
startActivity(intent);
```
其中,`this`指当前Activity的上下文,`NewActivity.class`是要启动的新Activity的类名。
2. 在Activity中获取控件:
```
TextView textView = findViewById(R.id.text_view);
```
其中,`text_view`是在布局文件中定义的控件ID。
3. 在Activity中设置点击事件:
```
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 点击事件处理逻辑
}
});
```
其中,`button`是在布局文件中定义的按钮控件,`setOnClickListener()`方法接收一个`OnClickListener`对象,用于处理按钮点击事件。
4. 在Activity中启动服务:
```
Intent intent = new Intent(this, MyService.class);
startService(intent);
```
其中,`MyService.class`是要启动的服务的类名。
5. 发送广播:
```
Intent intent = new Intent("com.example.MY_ACTION");
sendBroadcast(intent);
```
其中,`com.example.MY_ACTION`是广播的名称。
6. 使用SharedPreferences存储数据:
```
SharedPreferences preferences = getSharedPreferences("my_prefs", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("name", "John Doe");
editor.apply();
```
其中,`my_prefs`是SharedPreferences的名称,`name`是要存储的数据的键,`John Doe`是要存储的数据的值。
以上是一些常用的Android开发热点代码,希望对你有所帮助。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)