首页 > 代码库 > 第四周安卓作业

第四周安卓作业

任务一

1、图片选择器之跑马灯

<?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_flower"
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="com.example.flower.flowerActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/holo_red_dark"
android:layout_gravity="center"
android:scrollHorizontally="true"
android:scrollbars="horizontal"
android:text="请选择你喜欢的小花花"
android:singleLine="true"
android:ellipsize="marquee"
android:focusableInTouchMode="true"
android:focusable="true"
android:marqueeRepeatLimit="marquee_forever"
android:textSize="90sp"
android:id="@+id/biaoti" />
以上内容通过百度搜索安卓横向跑马灯
遇到的问题:标题不滚动 后来发现是字体太小了==
2、按钮
<ImageView
android:layout_marginTop="30dp"
android:layout_width="200dp"
android:layout_height="140dp"
android:layout_gravity="center"
android:id="@+id/iv_tupian"/>

<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical"
android:id="@+id/hualei"
>

<RadioGroup
android:layout_marginTop="40dp"
android:id="@+id/rg_rg1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/rbt_mei"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginRight="@dimen/activity_vertical_margin"
android:checked="true"
android:text="梅花"/>
<RadioButton
android:id="@+id/rbt_shinan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="石楠花"/>
<RadioButton
android:id="@+id/rbt_xiangya"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="象牙花"/>
</RadioGroup>
<RadioGroup
android:layout_marginTop="40dp"
android:id="@+id/rg_rg2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/rbt_xiuqiu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="绣球花"/>
<RadioButton
android:id="@+id/rbt_yulan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="玉兰花"/>
<RadioButton
android:id="@+id/rbt_mudan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="牡丹花"/>
</RadioGroup>
</RadioGroup>

</LinearLayout>
以上是添加按钮
java部分
package com.example.flower;

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 flowerActivity extends AppCompatActivity {
private TextView biaoti;
private ImageView iv_tupian;
private RadioGroup rg_rg1;
private RadioGroup hualei;
private RadioButton rbt_mei;
private RadioButton rbt_shinan;
private RadioButton rbt_xiangya;
private RadioGroup rg_rg2;
private RadioButton rbt_xiuqiu;
private RadioButton rbt_yulan;
private RadioButton rbt_mudan;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flower);
biaoti = (TextView) findViewById(R.id.biaoti);
rbt_mei = (RadioButton) findViewById(R.id.rbt_mei);
rbt_shinan = (RadioButton) findViewById(R.id.rbt_shinan);
rbt_xiangya = (RadioButton) findViewById(R.id.rbt_xiangya);
rbt_xiuqiu = (RadioButton) findViewById(R.id.rbt_xiuqiu);
rbt_yulan = (RadioButton) findViewById(R.id.rbt_yulan);
rbt_mudan = (RadioButton) findViewById(R.id.rbt_mudan);
rg_rg1 = (RadioGroup) findViewById(R.id.rg_rg1);
rg_rg2 = (RadioGroup) findViewById(R.id.rg_rg2);
hualei = (RadioGroup) findViewById(R.id.hualei);
iv_tupian=(ImageView)findViewById(R.id.iv_tupian);
建立响应事件
 rg_rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if(rbt_mei.isChecked()){
iv_tupian.setImageResource(R.mipmap.mei);
rg_rg2.clearCheck();
}
if(rbt_shinan.isChecked()){
iv_tupian.setImageResource(R.mipmap.shinan);
rg_rg2.clearCheck();
}
if(rbt_xiangya.isChecked()){
iv_tupian.setImageResource(R.mipmap.xiangya);
rg_rg2.clearCheck();
}

}
});
rg_rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if(rbt_xiuqiu.isChecked()){
iv_tupian.setImageResource(R.mipmap.xiuqiu);
rg_rg1.clearCheck();
}
if(rbt_yulan.isChecked()){
iv_tupian.setImageResource(R.mipmap.yulan);
rg_rg1.clearCheck();
}
if(rbt_mudan.isChecked()){
iv_tupian.setImageResource(R.mipmap.mudan);
rg_rg1.clearCheck();
}

}
});

}
}
用if语句解决非单选问题 因为发现有两个RadioGroup事件所以每一行都可以选择一种花类。在参考了部分资料之后选择用if语句如果rg1有选择时自动清除rg2的按钮选项 以此类推。
运行结果如下

技术分享

技术分享

任务二
xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main_xy"
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.myapplication4.MainActivityXY">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center_horizontal"
android:id="@+id/iv_tupian" />

<Button
android:layout_marginTop="300dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="退出"
android:id="@+id/bt_tuichu" />


</RelativeLayout>
以上是xml的内容
java
package com.example.myapplication4;

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

import static com.example.myapplication4.R.id.time;

public class MainActivityXY extends AppCompatActivity {
private ImageView iv_tupian;
private Button bt_tuichu;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_xy);
bt_tuichu = (Button) findViewById(R.id.bt_tuichu);
iv_tupian = (ImageView) findViewById(R.id.iv_tupian);
bt_tuichu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (bt_tuichu.isClickable()) {
Toast.makeText(MainActivityXY.this, "再按一次退出按钮",
Toast.LENGTH_LONG).show();
bt_tuichu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (bt_tuichu.isClickable()) {
MainActivityXY.this.finish();
}
}
});
}
}
});
}


public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
float x = event.getX();
float y = event.getY();
String pos = "x坐标" + x + "y坐标" + y;
Toast.makeText(this, pos, Toast.LENGTH_LONG).show();
iv_tupian.setImageResource(R.mipmap.chu);
iv_tupian.setX(x - 100);
iv_tupian.setY(y - 150);
}
return super.onTouchEvent(event);
}


private long exitTime = 0;

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
if ((System.currentTimeMillis() - exitTime) > 2000) {
Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
} else {
finish();
System.exit(0);
}
return true;
}
return super.onKeyDown(keyCode, event);
}
}
成果如下

技术分享


 
 
 


第四周安卓作业