Android应用程序开发期末大作业(2)
(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; } }@Overridepublic 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;}@Overridepublic 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" /><ImageButtonandroid: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" /><ImageButtonandroid: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-sdkandroid:minSdkVersion="17"android:targetSdkVersion="17" /><uses-permission android:name="android.permission.SET_WALLPAPER"/><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid: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;@Overrideprotected 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(){@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stub//根据用户勾选的单选按钮来动态改变tip字符串的值String tip=checkedId==R.id.male?"您的性别是男人":"您的性别是女人";//修改show组件中的文本show.setText(tip);}});}@Overridepublic 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;}@Overridepublic 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"/><!-- 定义一个垂直的线性布局 --><LinearLayoutandroid: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
Android应用程序开发期末大作业(2)相关推荐
- Android应用程序开发期末大作业(1)
一.简答题 (每小题5分,4小题,共20分) 1.(1) android大众常用的五种布局(5分) 答:FrameLayout(框架布局),LinearLayout (线性布局),AbsoluteLa ...
- Android期末大作业、移动应用开发期末大作业(教练预约APP)
移动应用开发期末大作业(教练预约APP) 运动健身APP 用户:小型健身房和健身者 功能:注册登录,预约教练,发布讨论,删除讨论 页面展示: 欢迎页面:(三秒自动跳转到主页面) 首页: 我的: 登录注 ...
- Android app开发期末大作业“快乐七巧板”开发总结
写在前面 大二的暑假过去一半了,我终于想起来把期末大作业写出来了.本文的主要目的就是总结一学期安卓开发入门和最后期末大作业相关的一些收获和感受. 给自己一个总结和给以后再去学习的相关内容的小伙伴们一咪 ...
- Web程序设计基础期末大作业——模仿QQ飞车手游S联赛官网编写的网页
QQ飞车手游是我非常喜欢的游戏,也是我现在为数不多的常在玩的游戏,刚好我Web程序设计基础的大作业是要做一套网站,我就借此机会模仿飞车S联赛官网的页面自己做了一个网页,又加了一些自己的元素,由于我做这 ...
- php网站开发期末大作业,网页设计期末大作业报告..doc
网页设计期末大作业报告. 南开大学滨海学院 本 科 生 论 文(设 计) 中文题目:外文题目:Website design and implementation based on Web develo ...
- 【安卓开发】简单记账app功能实现开发-期末大作业个人总结
说在前面: 由于这一次的大作业涉及到的代码部分过于长,所以博客里不放相关代码: 工程&apk&记账app原型&素材资源链接:https://download.csdn.net/ ...
- php网站开发期末大作业,大学生期末网页大作业
[实例简介] 6页面的网页设计,是个人主页类型.包含了6个页面,包含视频.脚本等元素.水平不高,但交选修作业就足够了. [实例截图] [核心代码] 为ai而狂 └── 为ai而狂 ├── FLVPla ...
- 【项目】游戏开发期末大作业 之 基于Java的小游戏 “大鱼吃小鱼“ (代码素材齐全)
1.EatFish游戏开发过程 1.游戏窗口创建 2.添加背景图片 3.启动封面 4.启动页面的点击事件 5.游戏开始的背景添加 6.双缓存解决闪屏问题 7.敌方鱼类的生成 8.我方鱼的生成 9.我方 ...
- web前端开发——期末大作业网页制作——web网页设计期末课程大作业 HTML+CSS+JS网页设计期末课程大作业 web前端开发技术 web课程设计 网页规划与设计
HTML实例网页代码, 本实例适合于初学HTML的同学.该实例里面有设置了css的样式设置,有div的样式格局,这个实例比较全面,有助于同学的学习,本文将介绍如何通过从头开始设计个人网站并将其转换为代 ...
- 安卓期末大作业——Android水果连连看
详情介绍 功能描述: 该连连看实现了基本的游戏功能,还实现了重开.提醒.背景音乐等功能,适合新手学习.搭建方法请看教菜单中的androidstudio项目搭建教程. 开发语言: java 技术框架: ...
最新文章
- 同一个Spring-AOP的坑,我一天踩了两次,深坑啊
- ubuntu更换阿里源
- CentOS7.0 OpenWrt环境搭建
- mysql主从复制不同步案例_Mysql主从不同步问题处理案例
- 信息学奥赛一本通 2025:【例4.11】体操队
- java并发容器(Map、List、BlockingQueue)
- 【实践】因果推断在快手推荐场景的应用探索.pdf(附下载链接)
- SAS数据挖掘实战篇【五】
- 蒙特卡洛模型——有约束的非线性规划问题
- cocos2dx基础篇(20)——基本动画CCAnimation/CCAnimate
- 刚体运动学公式_经典运动学公式汇总
- Invalid bound statement (not found): com.java.mapper.UserMapper.queryAll错误解决方案
- 东电计算机专业好就业吗,东北电力大学就业前景怎样好不好
- PaddleNLP新增AutoPrompt自动化提示功能,登顶FewCLUE小样本学习榜单
- 非法指令(Illegal Instruction)问题定位
- spark shuffle 内幕彻底解密
- 小程序源码:全新趣味测试-多玩法安装简单
- 使用css和js实现刮刮乐(简单易懂还有趣)
- Error could not open `Ejdklibamd64jvm.cfg‘问题解决
- 前端下载xlsx文件