安卓作业

任务一

综合使用TextView,ImageView,RadioButton控件实现一个图片选择器,通过勾选花朵的名称显示相应的图片,界面如下图,具体要求如下:

a)使用滚动字幕显示标题“请选择你喜欢的花”;

b)使用RadioGroup和RadioButton创建两行三列的单选按钮;

c)当用户选中某一花名,在页面上显示该种花的图片。

页面布局代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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"
android:orientation="vertical"
tools:context="cn.edu.niit.imageselector1.MainActivity">


<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="60sp"
android:singleLine="true"
android:textColor="@android:color/holo_blue_dark"
android:textSize="30dp"
android:ellipsize="marquee"
android:focusable="true"
android:marqueeRepeatLimit="marquee_forever"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:text="Please choose a flower you like!" />

<ImageView
android:id="@+id/tupian"
android:layout_marginTop="40dp"
android:layout_width="160dp"
android:layout_height="90dp"
android:layout_gravity="center" />(做的时候没有设置高度,图片超出范围,运行之后就变成了只能选一次,限制高度就行了)

<RadioGroup
android:id="@+id/huahua"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="60dp"
android:orientation="vertical">

<RadioGroup
android:id="@+id/rg1"
android:layout_marginTop="60dp"
android:layout_width="match_parent"
android:layout_height="80dp"
android:gravity="center"
android:orientation="horizontal" >

<RadioButton

android:id="@+id/mantianxing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/activity_vertical_margin"
android:checked="true"
android:text="满天星"
android:textSize="20sp" />

<RadioButton

android:id="@+id/xiaomoli"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小茉莉"
android:textSize="20sp"/>
<RadioButton

android:id="@+id/pingpanghua"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="乒乓花"
android:textSize="20sp"/>
</RadioGroup>

<RadioGroup

android:id="@+id/rg2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">

<RadioButton

android:id="@+id/meigui"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="玫瑰"
android:textSize="20sp" />

<RadioButton

android:id="@+id/xiuqiuhua"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绣球花"
android:textSize="20sp"/>

<RadioButton

android:id="@+id/meihua"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="梅花"
android:textSize="20sp"/>
</RadioGroup>
</RadioGroup>
</LinearLayout>
java部分的代码
package cn.edu.niit.imageselector1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
private TextView text;
private ImageView iv_tupian;
private RadioGroup huahua;
private RadioGroup rg_rg1;;
private RadioButton rbt_mantianxing;
private RadioButton rbt_xiaomoli;
private RadioButton rbt_pingpanghua;
private RadioGroup rg_rg2;
private RadioButton rbt_meigui;
private RadioButton rbt_xiuqiuhua;
private RadioButton rbt_meihua;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.text);
rbt_mantianxing = (RadioButton) findViewById(R.id.mantianxing);
rbt_xiaomoli = (RadioButton) findViewById(R.id.xiaomoli);
rbt_pingpanghua = (RadioButton) findViewById(R.id.pingpanghua);
rbt_meigui = (RadioButton) findViewById(R.id.meigui);
rbt_xiuqiuhua= (RadioButton) findViewById(R.id.xiuqiuhua);
rbt_meihua = (RadioButton) findViewById(R.id.meihua);
rg_rg1 = (RadioGroup) findViewById(R.id.rg1);
rg_rg2 = (RadioGroup) findViewById(R.id.rg2);
huahua = (RadioGroup) findViewById(R.id.huahua);
iv_tupian=(ImageView)findViewById(R.id.tupian);

rg_rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if(rbt_mantianxing.isChecked()){
iv_tupian.setImageResource(R.drawable.mantiannxing);
rg_rg2.clearCheck();
}
if(rbt_xiaomoli.isChecked()){
iv_tupian.setImageResource(R.drawable.xiaomoli);
rg_rg2.clearCheck();
}
if(rbt_pingpanghua.isChecked()){
iv_tupian.setImageResource(R.drawable.pinhpamhhua);
rg_rg2.clearCheck();
}

}
});
rg_rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {

if(rbt_meigui.isChecked()){
iv_tupian.setImageResource(R.drawable.meigui);
rg_rg1.clearCheck();
}
if(rbt_xiuqiuhua.isChecked()){
iv_tupian.setImageResource(R.drawable.xiuqiuhua);
rg_rg1.clearCheck();
}
if(rbt_meihua.isChecked()){
iv_tupian.setImageResource(R.drawable.meihua);
rg_rg1.clearCheck();
}

}
});

}
}
做的时候R显红色,后来多出了一个R.java,选择了重新编译
最终页面如下:

任务二:完成如下图所示任务

a)图片随着鼠标移动位置,并显示出当前位置的坐标信息;

b)当用户点击退出按钮,给提示信息:“再按一次退出程序”。

页面代码:

<?xml version="1.0" encoding="utf-8"?>

<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">
<ImageView
android:id="@+id/bear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bear"/>
</LinearLayout>
java代码:
package cn.edu.niit.nexus;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

private ImageView bear;

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);

bear = (ImageView) findViewById(R.id.bear);


DisplayMetrics dm = getResources().getDisplayMetrics();
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels - 50;

bear.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;
}
};
}
最终页面:

但是这个代码有问题的,不对的,绝望

posted on 2017-03-21 22:06  晞晞  阅读(251)  评论(0编辑  收藏  举报