安卓学习 Day11安卓应用程序资源
目录
案例演示
1、创建安卓应用
- 选择模板
- 完善项目信息
2、添加图片资源
3、添加音频资源
- 新建raw目录
- 拷贝音频资源到raw目录
4、编写字符串资源文件
5、编写主布局资源文件
- 源代码
-
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:id="@ id/root"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:background="@color/black"
-
android:gravity="center"
-
android:orientation="vertical">
-
-
-
<Button
-
android:id="@ id/btnAccessString"
-
android:layout_width="200dp"
-
android:layout_height="wrap_content"
-
android:onClick="doAccessString"
-
android:text="@string/access_string"
-
android:textColor="#0000ff"
-
android:textSize="20sp" />
-
-
<Button
-
android:id="@ id/btnAccessStringArray"
-
android:layout_width="200dp"
-
android:layout_height="wrap_content"
-
android:onClick="doAccessStringArray"
-
android:text="@string/access_string_array"
-
android:textColor="#0000ff"
-
android:textSize="20sp" />
-
-
<Button
-
android:id="@ id/btnChangeBackColor"
-
android:layout_width="200dp"
-
android:layout_height="wrap_content"
-
android:onClick="doChangeBackColor"
-
android:text="@string/change_backcolor"
-
android:textColor="#0000ff"
-
android:textSize="20sp" />
-
-
<Button
-
android:id="@ id/btnChangeBackground"
-
android:layout_width="200dp"
-
android:layout_height="wrap_content"
-
android:onClick="doChangeBackground"
-
android:text="@string/change_background"
-
android:textColor="#0000ff"
-
android:textSize="20sp" />
-
-
<Button
-
android:id="@ id/btnPlayMusic"
-
android:layout_width="200dp"
-
android:layout_height="wrap_content"
-
android:onClick="doPlayMusic"
-
android:text="@string/play_music"
-
android:textColor="#0000ff"
-
android:textSize="20sp" />
-
-
</LinearLayout>
6、编写颜色资源文件
7、编写主界面类代码
- 源代码
-
package net.zs.shiyongziyuan;
-
-
import androidx.appcompat.app.AppCompatActivity;
-
-
import android.media.MediaPlayer;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.widget.LinearLayout;
-
import android.widget.Toast;
-
-
public class MainActivity extends AppCompatActivity {
-
private LinearLayout root; // 线性根布局
-
private MediaPlayer mp; // 媒体播放器
-
private int colorClickCount; // 【改变背景色】按钮单击次数
-
private int pictureClickCount; // 【改变背景图片】按钮单击次数
-
-
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
// 利用布局资源文件设置用户界面
-
setContentView(R.layout.activity_main);
-
-
// 通过资源标识符获取控件实例
-
root = findViewById(R.id.root);
-
}
-
-
/**
-
* 访问字符串资源按钮单击事件处理方法
-
*/
-
public void doAccessString(View view) {
-
// 访问字符串资源
-
String strCollege = getResources().getString(R.string.college);
-
// 通过吐司来显示字符串数据
-
Toast.makeText(this,"你的学校:" strCollege, Toast.LENGTH_LONG).show();
-
}
-
-
/**
-
* 访问字符串数组资源按钮单击事件处理方法
-
*/
-
public void doAccessStringArray(View view) {
-
// 获得字符串数组资源,保存在字符串数组变量里
-
String[] strMajors = getResources().getStringArray(R.array.majors);
-
StringBuffer buffer = new StringBuffer();
-
// 遍历字符串数组,将每个元素拼接起来
-
buffer.append("人工智能与大数据学院\n\n");
-
// 传统for循环
-
for (int i = 0; i < strMajors.length; i ) {
-
buffer.append(strMajors[i] "\n");
-
}
-
// 通过吐司来显示字符串数组
-
Toast.makeText(MainActivity.this, buffer.toString(), Toast.LENGTH_LONG).show();
-
}
-
-
/**
-
* 改变背景色按钮单击事件处理方法
-
*/
-
public void doChangeBackColor(View view) {
-
// 获得颜色数组
-
int[] colors = new int[7];
-
colors[0] = getResources().getColor(R.color.black);
-
colors[1] = getResources().getColor(R.color.green);
-
colors[2] = getResources().getColor(R.color.blue);
-
colors[3] = getResources().getColor(R.color.red);
-
colors[4] = getResources().getColor(R.color.white);
-
colors[5] = getResources().getColor(R.color.yellow);
-
colors[6] = getResources().getColor(R.color.purple);
-
// 获取颜色索引
-
int index = colorClickCount % colors.length;
-
// 设置屏幕背景色
-
root.setBackgroundColor(colors[index]);
-
}
-
-
/**
-
* 改变背景图片按钮单击事件处理方法
-
*/
-
public void doChangeBackground(View view) {
-
// 初始化背景图片标识符数组
-
int[] bgPics = new int[7];
-
for (int i = 0; i < bgPics.length; i ) {
-
bgPics[i] = getResources().getIdentifier("bg" (i 1), "drawable", "net.zs.shiyongziyuan");
-
}
-
// 获取图片索引
-
int index = pictureClickCount % bgPics.length;
-
// 设置屏幕背景图片
-
root.setBackgroundResource(bgPics[index]);
-
}
-
-
/**
-
* 播放音乐按钮单击事件处理方法
-
*/
-
public void doPlayMusic(View view) {
-
if (mp == null) {
-
// 创建媒体播放器,指定播放源
-
mp = MediaPlayer.create(MainActivity.this, R.raw.music);
-
// 设置循环播放方式
-
mp.setLooping(true);
-
// 播放指定音频文件
-
mp.start();
-
// 设置完成监听器
-
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
-
-
public void onCompletion(MediaPlayer mp) {
-
Toast.makeText(MainActivity.this, "音乐播放完毕!", Toast.LENGTH_LONG).show();
-
}
-
});
-
}
-
}
-
}
8、运行程序,查看效果
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgfagab
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01