• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

安卓学习 Day11安卓应用程序资源

武飞扬头像
越来越不懂!
帮助1

目录

案例演示

1、创建安卓应用

 2、添加图片资源

 3、添加音频资源

4、编写字符串资源文件

5、编写主布局资源文件

6、编写颜色资源文件

7、编写主界面类代码 

8、运行程序,查看效果


案例演示

1、创建安卓应用

  • 选择模板

学新通

  •  完善项目信息

学新通

 2、添加图片资源

学新通

 3、添加音频资源

  • 新建raw目录

学新通

  • 拷贝音频资源到raw目录

学新通 

4、编写字符串资源文件

学新通

5、编写主布局资源文件

  • 源代码
  1.  
    <?xml version="1.0" encoding="utf-8"?>
  2.  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.  
    android:id="@ id/root"
  4.  
    android:layout_width="match_parent"
  5.  
    android:layout_height="match_parent"
  6.  
    android:background="@color/black"
  7.  
    android:gravity="center"
  8.  
    android:orientation="vertical">
  9.  
     
  10.  
     
  11.  
    <Button
  12.  
    android:id="@ id/btnAccessString"
  13.  
    android:layout_width="200dp"
  14.  
    android:layout_height="wrap_content"
  15.  
    android:onClick="doAccessString"
  16.  
    android:text="@string/access_string"
  17.  
    android:textColor="#0000ff"
  18.  
    android:textSize="20sp" />
  19.  
     
  20.  
    <Button
  21.  
    android:id="@ id/btnAccessStringArray"
  22.  
    android:layout_width="200dp"
  23.  
    android:layout_height="wrap_content"
  24.  
    android:onClick="doAccessStringArray"
  25.  
    android:text="@string/access_string_array"
  26.  
    android:textColor="#0000ff"
  27.  
    android:textSize="20sp" />
  28.  
     
  29.  
    <Button
  30.  
    android:id="@ id/btnChangeBackColor"
  31.  
    android:layout_width="200dp"
  32.  
    android:layout_height="wrap_content"
  33.  
    android:onClick="doChangeBackColor"
  34.  
    android:text="@string/change_backcolor"
  35.  
    android:textColor="#0000ff"
  36.  
    android:textSize="20sp" />
  37.  
     
  38.  
    <Button
  39.  
    android:id="@ id/btnChangeBackground"
  40.  
    android:layout_width="200dp"
  41.  
    android:layout_height="wrap_content"
  42.  
    android:onClick="doChangeBackground"
  43.  
    android:text="@string/change_background"
  44.  
    android:textColor="#0000ff"
  45.  
    android:textSize="20sp" />
  46.  
     
  47.  
    <Button
  48.  
    android:id="@ id/btnPlayMusic"
  49.  
    android:layout_width="200dp"
  50.  
    android:layout_height="wrap_content"
  51.  
    android:onClick="doPlayMusic"
  52.  
    android:text="@string/play_music"
  53.  
    android:textColor="#0000ff"
  54.  
    android:textSize="20sp" />
  55.  
     
  56.  
    </LinearLayout>
学新通

6、编写颜色资源文件

学新通

7、编写主界面类代码 

  • 源代码
  1.  
    package net.zs.shiyongziyuan;
  2.  
     
  3.  
    import androidx.appcompat.app.AppCompatActivity;
  4.  
     
  5.  
    import android.media.MediaPlayer;
  6.  
    import android.os.Bundle;
  7.  
    import android.view.View;
  8.  
    import android.widget.LinearLayout;
  9.  
    import android.widget.Toast;
  10.  
     
  11.  
    public class MainActivity extends AppCompatActivity {
  12.  
    private LinearLayout root; // 线性根布局
  13.  
    private MediaPlayer mp; // 媒体播放器
  14.  
    private int colorClickCount; // 【改变背景色】按钮单击次数
  15.  
    private int pictureClickCount; // 【改变背景图片】按钮单击次数
  16.  
     
  17.  
    @Override
  18.  
    protected void onCreate(Bundle savedInstanceState) {
  19.  
    super.onCreate(savedInstanceState);
  20.  
    // 利用布局资源文件设置用户界面
  21.  
    setContentView(R.layout.activity_main);
  22.  
     
  23.  
    // 通过资源标识符获取控件实例
  24.  
    root = findViewById(R.id.root);
  25.  
    }
  26.  
     
  27.  
    /**
  28.  
    * 访问字符串资源按钮单击事件处理方法
  29.  
    */
  30.  
    public void doAccessString(View view) {
  31.  
    // 访问字符串资源
  32.  
    String strCollege = getResources().getString(R.string.college);
  33.  
    // 通过吐司来显示字符串数据
  34.  
    Toast.makeText(this,"你的学校:" strCollege, Toast.LENGTH_LONG).show();
  35.  
    }
  36.  
     
  37.  
    /**
  38.  
    * 访问字符串数组资源按钮单击事件处理方法
  39.  
    */
  40.  
    public void doAccessStringArray(View view) {
  41.  
    // 获得字符串数组资源,保存在字符串数组变量里
  42.  
    String[] strMajors = getResources().getStringArray(R.array.majors);
  43.  
    StringBuffer buffer = new StringBuffer();
  44.  
    // 遍历字符串数组,将每个元素拼接起来
  45.  
    buffer.append("人工智能与大数据学院\n\n");
  46.  
    // 传统for循环
  47.  
    for (int i = 0; i < strMajors.length; i ) {
  48.  
    buffer.append(strMajors[i] "\n");
  49.  
    }
  50.  
    // 通过吐司来显示字符串数组
  51.  
    Toast.makeText(MainActivity.this, buffer.toString(), Toast.LENGTH_LONG).show();
  52.  
    }
  53.  
     
  54.  
    /**
  55.  
    * 改变背景色按钮单击事件处理方法
  56.  
    */
  57.  
    public void doChangeBackColor(View view) {
  58.  
    // 获得颜色数组
  59.  
    int[] colors = new int[7];
  60.  
    colors[0] = getResources().getColor(R.color.black);
  61.  
    colors[1] = getResources().getColor(R.color.green);
  62.  
    colors[2] = getResources().getColor(R.color.blue);
  63.  
    colors[3] = getResources().getColor(R.color.red);
  64.  
    colors[4] = getResources().getColor(R.color.white);
  65.  
    colors[5] = getResources().getColor(R.color.yellow);
  66.  
    colors[6] = getResources().getColor(R.color.purple);
  67.  
    // 获取颜色索引
  68.  
    int index = colorClickCount % colors.length;
  69.  
    // 设置屏幕背景色
  70.  
    root.setBackgroundColor(colors[index]);
  71.  
    }
  72.  
     
  73.  
    /**
  74.  
    * 改变背景图片按钮单击事件处理方法
  75.  
    */
  76.  
    public void doChangeBackground(View view) {
  77.  
    // 初始化背景图片标识符数组
  78.  
    int[] bgPics = new int[7];
  79.  
    for (int i = 0; i < bgPics.length; i ) {
  80.  
    bgPics[i] = getResources().getIdentifier("bg" (i 1), "drawable", "net.zs.shiyongziyuan");
  81.  
    }
  82.  
    // 获取图片索引
  83.  
    int index = pictureClickCount % bgPics.length;
  84.  
    // 设置屏幕背景图片
  85.  
    root.setBackgroundResource(bgPics[index]);
  86.  
    }
  87.  
     
  88.  
    /**
  89.  
    * 播放音乐按钮单击事件处理方法
  90.  
    */
  91.  
    public void doPlayMusic(View view) {
  92.  
    if (mp == null) {
  93.  
    // 创建媒体播放器,指定播放源
  94.  
    mp = MediaPlayer.create(MainActivity.this, R.raw.music);
  95.  
    // 设置循环播放方式
  96.  
    mp.setLooping(true);
  97.  
    // 播放指定音频文件
  98.  
    mp.start();
  99.  
    // 设置完成监听器
  100.  
    mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
  101.  
    @Override
  102.  
    public void onCompletion(MediaPlayer mp) {
  103.  
    Toast.makeText(MainActivity.this, "音乐播放完毕!", Toast.LENGTH_LONG).show();
  104.  
    }
  105.  
    });
  106.  
    }
  107.  
    }
  108.  
    }
学新通

8、运行程序,查看效果

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfagab
系列文章
更多 icon
同类精品
更多 icon
继续加载