Android应用程序开发期末大作业(2)

2023-05-16

(3)用imageView和imagebutton及activity在界面上实现图片浏览

新建android项目如AI03,在项目的/AI03/src/com/example/ai03/MainActivity.java文件写下如下代码,注意包名和图片存放的位置!

图片存放目录位置为/AI03/res/drawable-hdpi


package com.example.ai03;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

	//声明Image对象与ImageBoutton对象  
    private ImageView ivwPicture=null;  
    private ImageButton ibtnProv=null;  
    private ImageButton ibtnNext=null;  
    //声明5个图像  
    private Integer[] iImages = {R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e};
    
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        //获取ImageView对象ivwPicture  
        ivwPicture=(ImageView)super.findViewById(R.id.picture);  
        //获取两个按钮对象ImageButton  
        ibtnProv=(ImageButton)super.findViewById(R.id.prov);  
        ibtnNext=(ImageButton)super.findViewById(R.id.next);  
        //注册OnClick监听器  
        ibtnProv.setOnClickListener(new ProvOnClickListener());  
        ibtnNext.setOnClickListener(new NextOnClickListener());  
        //注册OnlongClick监听器  
        ivwPicture.setOnLongClickListener(new PicOnLongClick());  
    }  
    //单击“上一幅”按钮显示前一张图片  
    private class ProvOnClickListener  implements OnClickListener{  
        private int i=5;  
        public void onClick(View view){           
            if(i > 0){  
                ivwPicture.setImageResource(iImages[--i]);  
            }  
            else if(i == 0){  
                i =5;  
                ivwPicture.setImageResource(iImages[4]);  
            }  
        }  
    }  
    //单击“下一幅”按钮显示后一张图片  
    private class NextOnClickListener implements OnClickListener{  
        private int i=0;  
        public void onClick(View view){           
            if(i < 5)  
                ivwPicture.setImageResource(iImages[i++]);  
            else if(i == 5){  
                i = 0;  
                ivwPicture.setImageResource(iImages[0]);  
            }  
        }  
    }  
    //长按图片设置为桌面墙纸  
    private class PicOnLongClick implements OnLongClickListener{  
        @Override  
        public boolean onLongClick(View view){  
            try{  
                //清空当前墙纸  
                MainActivity.this.clearWallpaper();  
                //当前view转换为ImageView对象  
                ImageView iv=(ImageView)view;  
                //启用图形缓冲  
                iv.setDrawingCacheEnabled(true);  
                //使用当前缓冲图形创建Bitmap  
                Bitmap bmp=Bitmap.createBitmap(iv.getDrawingCache());  
                //当前图形设置为墙纸  
                MainActivity.this.setWallpaper(bmp);  
                //清理图形缓冲  
                iv.setDrawingCacheEnabled(false);  
                Toast.makeText(getApplicationContext(), "背景设置成功!",Toast.LENGTH_LONG).show();  
            }  
            catch(Exception e){  
                Toast.makeText(getApplicationContext(), "背景设置失败!",Toast.LENGTH_LONG).show();  
            }  
            return true;  
        }  
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
在项目的/AI03/res/layout/activity_main.xml文件写下如下代码。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.ai03.MainActivity" >

    <ImageView  
        android:id="@+id/picture"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentTop="true"  
        android:layout_centerHorizontal="true"  
        android:layout_marginTop="0dp"  
        android:src="@drawable/a"  
        tools:ignore="ContentDescription" />

    <ImageButton
        android:id="@+id/prov"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:src="@drawable/prov" />

    <ImageButton
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/picture"
        android:src="@drawable/next" />

</RelativeLayout>
在项目的 /AI03/AndroidManifest.xml 文件添加如下代码。

<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.ai03"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="17" />
    
    <uses-permission android:name="android.permission.SET_WALLPAPER"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
运行效果如下。



转自:http://blog.csdn.net/jianghuiquan/article/details/8348680

(4)利用RadioButton、CheckBox、Activity等实现如图的功能及效果

新建android项目如AI04,在项目的/AI01/src/com/example/ai04/MainActivity.java文件写下如下代码,注意包名!

package com.example.ai04;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {

	RadioGroup rg;
    TextView show;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//获取界面上rg、show两个组件
        rg=(RadioGroup)findViewById(R.id.rg);
        show=(TextView)findViewById(R.id.show);
        //为RadioGroup组件的OnCheck事件绑定事件监听器
        rg.setOnCheckedChangeListener(new OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                //根据用户勾选的单选按钮来动态改变tip字符串的值
                String tip=checkedId==R.id.male?"您的性别是男人":"您的性别是女人";
                  //修改show组件中的文本
                show.setText(tip);
            }
            
        });
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
在项目的 /AI04/res/layout/activity_main.xml 文件写下如下代码。
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.ai04.MainActivity" >

    <TableRow >
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="性别:"
        android:textSize="16dp"/>
    <!-- 定义一组单选按钮 -->
    <RadioGroup android:id="@+id/rg"
        android:orientation="horizontal"
        android:layout_gravity="center_horizontal">
        <!-- 定义两个单选按钮 -->
        <RadioButton android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/male"
            android:text="男"
            android:checked="true" />
        <RadioButton android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/female"
            android:text="女"/>
    </RadioGroup>
</TableRow>
   <TableRow >
       <TextView android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="喜欢的颜色:"
           android:textSize="16dp"/>
       <!-- 定义一个垂直的线性布局 -->
       <LinearLayout
           android:layout_gravity="center_horizontal"
           android:orientation="vertical"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" >
           <!-- 定义三个复选框 -->
           <CheckBox android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="红色"
               android:checked="true" />
           <CheckBox android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="蓝色"/>
           <CheckBox android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="绿色"/>
       </LinearLayout>
   </TableRow>
   <TextView android:id="@+id/show"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />

</TableLayout>
运行效果如下。


转自:https://www.cnblogs.com/wolipengbo/p/3343443.html
















本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android应用程序开发期末大作业(2) 的相关文章

  • STM32 F4串口空闲中断 + DMA实现数据发送

    STM32 F4串口空闲中断 43 DMA实现数据发送 前言文章目录一 空闲中断二 DMA三 代码部分1 串口配置2 DMA配置 前言 最近在做 STM32 43 ROS车的项目 xff0c STM32与ROS之间通信由于数据量大 xff0
  • Darknet YoloV4编译+训练(避免踩坑)

    AlexAB darknet yolov4编译 43 训练 时间间隔好几天今天来更新一下yolov4的训练 训练篇 在训练之前需要对大佬的源码进行编译本本机编译 xff0c 编译过程可查看下述链接 xff1a https blog csdn
  • ubuntu下如何创建ros工作空间、创建ros功能包、创建ros节点

    1 打开终端进入存放ros工作空间的目录 xff08 比如我这里将它放在home目录下的test文件夹中 xff09 cd test 2 开始创建ros工作空间 mkdir p catkin ws src cd catkin ws src
  • STM32串口通信 (采用链表接收不定长数据帧)

    STM32串口通信 链表接收不定长数据帧 数据帧说明不太恰当的比方 数据缓冲链表结构效果展示工程文件 数据帧说明 STM32数据寄存器为USARTx gt DR寄存器 可以看到DR寄存器只有 8 0 位可以使用 xff0c 第8位用于奇偶校
  • A*寻路算法

    目录 1 动画演示2 游戏中的自动寻路A 算法3 A 寻路算法原理4 调试代码分析代码5 代码 1 动画演示 2 游戏中的自动寻路A 算法 随着3D游戏的日趋流行 在复杂的3D游戏环境中如何能使非玩家控制角色准确实现自动寻路功能成为了3D游
  • 2022数学建模国赛B题和C题高质量论文代码数据

    目录 B题论文 5 1 问题一的建模与求解 5 1 1 使用极坐标求解具体位置 C题论文 1 1 研究背景 1 2 问题的提 5 1 问题一的建模与求解 5 1 1 数据的预处理 B题论文 5 1 问题一的建模与求解 5 1 1 使用极坐标
  • stm32小白学习之寄存器名称

    IDR输入只读寄存器 xff0c ODR输出可读可写寄存器 BSRR xff08 置位寄存器 xff09 与BRR xff08 复位寄存器 xff09 CRL xff08 端口配置低位寄存器 xff09 与CRH xff08 端口配置高位寄
  • 使用Vite创建Vue3+TS项目并整合Element Plus框架等一条龙服务

    记录一下使用Vite创建Vue3 43 TS项目以及整合Element Plus框架 xff0c 还有Less Pinia Vue router monaco editor等插件或组件 一 使用Vite创建Vue3 43 TS项目 第一步
  • Qt学习 第37节:QString

    在阅读QString文档时 xff0c 出了一个词 implicit sharing copy on write xff0c 不是很懂 xff0c 下面链接解释的表明白 QT的隐式共享 Implicit Sharing 道路与梦想 CSDN
  • 下载Postman并且汉化使用

    下载Postman并且汉化使用 一 下载postman postman有不同的版本 xff0c 如果要汉化就要下载的版本与汉化包一致 下载地址 xff1a postman官网下载地址 xff1a https www postman com
  • 【Vue】postman汉化教程 保姆级教程 包教会

    下载链接 xff1a Win64 Win32 历史版本下载 请把下面链接的 34 版本号 34 替换为指定的版本号 xff0c 例如 xff1a 8 8 0 版本链接Windows32位https dl pstmn io download
  • 操作系统实验——进程与线程

    目录 1 使用GCC xff08 1 xff09 参数 xff08 2 xff09 自定义头文件 xff08 3 xff09 makefile脚本 xff08 4 xff09 gdb调试 2 进程 xff08 1 xff09 新建进程 xf
  • 串口应用(USART)

    串行口应用 1 USART介绍 通用同步异步收发器 USART 提供了一种灵活的方法与使用工业标准NRZ异步串行数据格式的 外部设备之间进行全双工数据交换 USART利用分数波特率发生器提供宽范围的波特率选择 它支持同步单向通信和半双工单线
  • cpp-httplib 避免阻塞主线程, c++封装httplib,httplib面向对象开发

    目录 说明 前言原生的httplib会阻塞你的主线程解决httplib阻塞主线程的问题BashController 面向对象风格使用httplib自定义controller MyController h文件自定义controller Tes
  • 数据结构——结构体的5种定义方式及对比

    以下仅为定义结构体的方式 xff0c 具体使用在后续的文章中介绍 span class token macro property span class token directive hash span span class token d
  • 【C++】STL应用(详解)

    一 泛型程序与STL1 泛型程序设计的基本概念2 STL简介 二 迭代器1 输入流迭代器2 输出流迭代器 三 STL应用1 撰写自己的算法和函数 xff0c 结合容器和迭代器解决序列变换 xff08 如取反 平方 立方 xff09 xff0
  • 模块学习(二)——MPU6050

    去年电赛备赛期间 xff0c 学的STM32标准库 xff0c 那一整个繁琐直接给我劝退了 xff0c 当时学习MPU6050时就非常痛苦 xff0c 代码也看不懂 xff0c 无非抄来抄去 xff0c 然后就是编译 xff0c 改错 xf
  • 【嵌入式面试题】常见的面试题梳理一

    注 xff1a 看面试题时 xff0c 主要应该以学习为主 xff0c 面试题有些基本上是我们编程时会遇上的问题 xff0c 通过学习面试题会提升我们的编程意识和解决一些日常我们编程所遇到的问题 xff0c 看完这篇面试题后 xff0c 希
  • 使用Python+openpyxl实现导出自定义样式的Excel文件

    之前项目中的导出Excel文件操作都是在前端完成的 xff0c 项目是由vue 43 vite构建的 xff0c 效果还不错的 xff0c 所需依赖包如下所示 npm i xlsx 64 0 18 5 npm i xlsx style vi
  • 【嵌入式面试题】常见面试题梳理二

    注 xff1a 看面试题时 xff0c 主要应该以学习为主 xff0c 面试题有些基本上是我们编程时会遇上的问题 xff0c 通过学习面试题会提升我们的编程意识和解决一些日常我们编程所遇到的问题 xff0c 看完这篇面试题后 xff0c 希

随机推荐