首个写博客的Android任务
小白一个,首次写博客,些许错误勿见怪,哈哈哈哈。
任务1 单击按钮图片选择器
*使用TextView,RadioGroup,RadioButton完成。
*设置单击按钮选择显示花朵。
首先设置了页面布局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.bwqpzy.flower.MainActivity"
android:weightSum="1">
*1.在TextView中遇到了滚动文字显示问题,之后从网上找到了跑马灯显示的方法 即 android:ellipsize="marquee"
代码具体如下
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="#cc0000"
android:textSize="29sp"
android:ellipsize="marquee"//跑马灯
android:focusable="true"
android:marqueeRepeatLimit="marquee_forever"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"/>
在Java中定义如下代码
TextView text = (TextView) findViewById(R.id.text);
text.setText("Please choose a flower you like!");
text.setFocusable(true);
这样就完成滚动文字的显示效果
*2.RadioGroup可单选,但两组group的话,会有两个选项,之后在mainactivity需要设置代码实现功能。在group中也可定义linearlayout,我试过之后发现group中可以被多选,感觉很麻烦,所以我直接选择了在layout中定义组件,只需一组RadioGroup可实现要求,代码如下:
<RadioGroup
android:id="@+id/choose1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/cmh"
android:layout_width="80dip"
android:layout_marginTop="-6dip"
android:layout_height="wrap_content"
android:text="梅花"
android:textSize="20dp"/>
<RadioButton
android:id="@+id/cxqh"
android:layout_width="113dp"
android:layout_height="wrap_content"
android:text="绣球花"
android:textSize="20dp"/>
<RadioButton
android:id="@+id/csnh"
android:layout_width="105dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginLeft="150dip"
android:layout_marginTop="-64dip"
android:text="石楠花"
android:textSize="20dp"/>
<RadioButton
android:id="@+id/cylh"
android:layout_width="106dp"
android:layout_height="wrap_content"
android:layout_marginLeft="150dip"
android:orientation="horizontal"
android:text="玉兰花"
android:textSize="20dp"/>
<RadioButton
android:id="@+id/cxyh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="290dip"
android:layout_marginTop="-64dip"
android:text="象牙花"
android:textSize="20dp"/>
<RadioButton
android:text="牡丹花"
android:id="@+id/cmdh"
android:layout_width="wrap_content"
android:layout_marginLeft="290dip"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:textSize="20dp"
/>
</RadioGroup>
*3.用一个ImageView来显示图片
其中我在drawable中新建了一个xml文件
代码如下
<level-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/mh"
android:maxLevel="0"/>
<item
android:drawable="@drawable/snh"
android:maxLevel="1"/>
<item
android:drawable="@drawable/xyh"
android:maxLevel="2"/>
<item
android:drawable="@drawable/xqh"
android:maxLevel="3"/>
<item
android:drawable="@drawable/ylh"
android:maxLevel="4"/>
<item
android:drawable="@drawable/mdh"
android:maxLevel="5"/>
</level-list>
整个layout布局完毕
具体效果如图


其中主要部分代码如下
public class MainActivity extends AppCompatActivity {
//1.定义组件
private ImageView image;
private RadioGroup choose1;
private RadioButton cmh;
private RadioButton csnh;
private RadioButton cxyh;
private RadioButton cxqh;
private RadioButton cylh;
private RadioButton cmdh;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text = (TextView) findViewById(R.id.text);
text.setText("Please choose a flower you like!");
text.setFocusable(true);
//2.获取组件
choose1 = (RadioGroup) findViewById(R.id.choose1);
cmh = (RadioButton) findViewById(R.id.cmh);
csnh = (RadioButton) findViewById(R.id.csnh);
cxyh = (RadioButton) findViewById(R.id.cxyh);
cxqh = (RadioButton) findViewById(R.id.cxqh);
cylh = (RadioButton) findViewById(R.id.cylh);
cmdh = (RadioButton) findViewById(R.id.cmdh);
image = (ImageView) findViewById(R.id.image);
//3.设置监听并获取图片
cmh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (cmh.isChecked()) {
image.setImageResource(R.drawable.mh);
}
}
});
csnh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (csnh.isChecked()) {
image.setImageResource(R.drawable.snh);
}
}
});
cxyh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (cxyh.isChecked()) {
image.setImageResource(R.drawable.xyh);
}
}
});
cxqh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (cxqh.isChecked()) {
image.setImageResource(R.drawable.xqh);
}
}
});
cylh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (cylh.isChecked()) {
image.setImageResource(R.drawable.ylh);
}
}
});
cmdh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (cmdh.isChecked()) {
image.setImageResource(R.drawable.mdh);
}
}
});
}
}
通过这些代码的编写,逐渐熟悉了Android的流程以及运用所学知识来解决遇到的问题,通过网络搜索,查询来扩充知识储备。
任务2 图片随鼠标移动来显示坐标
*图片随着鼠标移动位置,并显示出当前位置的坐标信息。
*当用户点击退出按钮,给出提示信息:“再按一次退出程序”。
页面布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bwqpzy.dog.MainActivity">
<ImageView
android:id="@+id/dog"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:src="@drawable/er"
/>
</LinearLayout>
mainactivity部分的代码
public class MainActivity extends AppCompatActivity {
private ImageView dog;
private int screenWidth;
private int screenHeight;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dog = (ImageView) findViewById(R.id.dog);
DisplayMetrics dm = getResources().getDisplayMetrics();
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels - 50;
dog.setOnTouchListener(movingEventListener);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_DOWN) {
float x=event.getX();
float y=event.getY();
String pos="x坐标:"+x+",y坐标:"+y;
Toast.makeText(this,pos,Toast.LENGTH_LONG).show();
}
return super.onTouchEvent(event);
}
private View.OnTouchListener movingEventListener = new View.OnTouchListener() {
int lastX, lastY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
int dx = (int) event.getRawX() - lastX;
int dy = (int) event.getRawY() - lastY;
int left = v.getLeft() + dx;
int top = v.getTop() + dy;
int right = v.getRight() + dx;
int bottom = v.getBottom() + dy;
// 设置不能出界
if (left < 0) {
left = 0;
right = left + v.getWidth();
}
if (right > screenWidth) {
right = screenWidth;
left = right - v.getWidth();
}
if (top < 0) {
top = 0;
bottom = top + v.getHeight();
}
if (bottom > screenHeight) {
bottom = screenHeight;
top = bottom - v.getHeight();
}
v.layout(left, top, right, bottom);
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
};
}
效果图

这次主要有这几个问题:
1.RadioGroup下只有一个单选按钮,设置新的布局会破坏一个按钮的限制。
2.一个imageview怎么储存多张图,通过建文件,关联id解决。
首个写博客的Android任务的更多相关文章
- 《REWORK》启示录 招聘笔杆子——程序员为什么值得写博客
Hire Great Writers 仿佛这是写给自己看的,不过这在其中也有着相当有趣的意义 .虽然自己算是一个能写的人,或许这算是一种不算才华的才华,写博文的意义通常不会在于去描述自己怎样,怎样.通 ...
- 云开发中的战斗机 Laf,让你像写博客一样写代码
各位云原生搬砖师 and PPT 架构师,你们有没有想过像写文章一样方便地写代码呢? 怎样才能像写文章一样写代码? 理想的需求应该是可以在线编写.调试函数,不用重启服务,随时随地在 Web 上查看函数 ...
- 新功能发布!Markdown写博客!
有一种神奇的语言,它比html还简单,它巧妙地将内容与格式整合在一起--它就是Markdown. 现在我们实现了博客对Markdown的内置支持,可以让您轻松地在园子里用这个神奇的语言写博客! &qu ...
- 第一次写博客Poj1044
Date bugs Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3005 Accepted: 889 Descript ...
- (转)[BetterExplained]为什么你应该(从现在开始就)写博客
(一)为什么你应该(从现在开始就)写博客 用一句话来说就是,写一个博客有很多好处,却没有任何明显的坏处.(阿灵顿的情况属于例外,而非常态,就像不能拿抽烟活到一百岁的英国老太太的个例来反驳抽烟对健康的极 ...
- 推荐几款自己写博客使用的Ubuntu软件
使用Ubuntu桌面有段时间,到现在也写过几篇博客了,期间用到的几款好用的软件推荐给大家.1. 图片简单编辑软件gthumbubuntu默认提供shotwell查看图片,类似与windows的图片查看 ...
- QQ表情动图,增加写博客的乐趣
QQ表情动图,增加写博客的乐趣 body{margin:0px;}
- 如果简单的记录,就可以为这个世界创造更多的财富,那么还有什么理由不去写博客呢? — 读<<黑客与画家>> 有感
上一次博文发文时间是2016.1.15,7个月已经过去了.最近读了一本<>的书,对我触动挺大的!里面有关于技术趋势的探讨,也有关于人生和财富的思考! 开始更新iOS122的文章的初衷是,聚 ...
- 好久没有写博客了,发现Live Writer也更新了
最近由于工作变动,工作内容和心态也有所变化,所以很久没有写博客了,而且我的开源项目深蓝词库转换也很近没有更新了.今天打开LiveWriter发现居然有新版本,于是果断更新.现在新的LiveWriter ...
随机推荐
- easyui datagrid的json格式
easyui datagrid的json格式: {"columns":[[{"field":"one","title": ...
- Android客户端连接服务器端,向服务器端发送请求HttpURLConnection
在Java中想后台服务器发送请求一般都直接使用了Java的网络编程,或者使用HttpClient向后台服务器端发送HTTP请求.虽然在安卓中,所有Java的API都可以使用,而却使用其并不会出现什么问 ...
- Tomcat使用常见问题
1,启动服务器,闪退问题 原因:tomcat软件是用java语言开发的,软件启动时,会默认到系统环境变量中查找一个名叫JAVA_HOME的变量.这个变量的作用是找到tomcat启动所需的JVM. ...
- C++的输入和输出
C++是一种常用的编程语言.一个完整的程序至少要有一个输出,而我们也经常需要在程序内进行大量输入和输出.所以今天,我和大家谈一谈输入和输出. 1.cin和cout.可以连续输入,使用流(>> ...
- [nodejs] day1-创建服务器
一.使用匿名函数(新建文件service.js)创建一个服务器: var http = require("http"); //Node.js自带的 http 模块,并且把它赋值给 ...
- ajax入门之建立XHR对象 (1)
ajax入门之建立XHR对象 今天帮朋友写了一个简单的ajax的demo,发现了一些东西,决定写一篇文章记录一下,避免以后挖坑. 创建XMLHttpRequest 通常 function create ...
- Linux实战教学笔记12:linux三剑客之sed命令精讲
第十二节 linux三剑客之sed命令精讲 标签(空格分隔): Linux实战教学笔记-陈思齐 ---更多资料点我查看 1,前言 我们都知道,在Linux中一切皆文件,比如配置文件,日志文件,启动文件 ...
- array_count_values:返回数组中所有值出现的次数
$arr1 = ['a','b','c','d','e','e','a','a']; $arr = array_count_values($arr1); echo '<pre>'; p ...
- PHP递归遍历指定文件夹内的文件
今天早上在地铁上看了关于文件和文件夹的一章,正好最近刚搞懂linux的文件系统,觉得对文件属性的访问跟Shell命令很像,所以想晚上来实践一下. 发现php的文件夹函数好像没有提供遍历文件夹下的所有文 ...
- [Usaco2014 Open]Gold Fair Photography(hash)
最近做了usaco2014 open的金组,果然美帝的题还是没有太简单啊QAQ,被每年的月赛骗了QAQ 不过话说官方题解真心棒(虽然英文的啃得好艰难,我英语渣你们别鄙视我= =),标程超级优美QAQ ...