代码改变世界

安卓作业

2017-03-21 15:28  路人甲i  阅读(156)  评论(0编辑  收藏  举报

 第一个作业:图片选择器
在Textview中添加了一段关于跑马灯效果的代码:
 android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true"
        android:singleLine="true"
    android:scrollbars="horizontal

 1 package com.example.zhoushasha.myapplication6;
 2 
 3 import android.support.v7.app.AppCompatActivity;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.widget.ImageView;
 7 import android.widget.RadioButton;
 8 import android.widget.RadioGroup;
 9 import android.widget.Toast;
10 
11 public class MainActivity extends AppCompatActivity {
12     //获取控件
13     private ImageView page;
14     private RadioGroup rg1;
15     private RadioGroup rg2;
16     private RadioButton rb5;
17     private RadioButton rb6;
18     private RadioButton rb7;
19     private RadioButton rb8;
20     private RadioButton rb9;
21     private RadioButton rb10;
22 
23     @Override
24     protected void onCreate(Bundle savedInstanceState) {
25         super.onCreate(savedInstanceState);
26         setContentView(R.layout.activity_main);
27         //
28         page = (ImageView) findViewById(R.id.page);
29         rg1 = (RadioGroup) findViewById(R.id.rg1);
30         rg2 = (RadioGroup) findViewById(R.id.rg2);
31         rb5 = (RadioButton) findViewById(R.id.rb5);
32         rb6 = (RadioButton) findViewById(R.id.rb6);
33         rb7 = (RadioButton) findViewById(R.id.rb7);
34         rb8 = (RadioButton) findViewById(R.id.rb8);
35         rb9 = (RadioButton) findViewById(R.id.rb9);
36         rb10 = (RadioButton) findViewById(R.id.rb10);
37         //用setOnCheckedChangeListener添加监听对象
38         rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {   //监听rgrg RadioGroup组
39             @Override
40             public void onCheckedChanged(RadioGroup radioGroup, int i) {
41                 if (rb8.isChecked()) {
42                     page.setImageResource(R.drawable.map);//用于显示自己添加的图片
43                     rg2.clearCheck();// 用于显示单选,不出现多选的情况
44                 }
45                 if (rb9.isChecked()) {
46                     page.setImageResource(R.drawable.map1);
47                     rg2.clearCheck();
48                 }
49                 if (rb10.isChecked()) {
50                     page.setImageResource(R.drawable.map2);
51                     rg2.clearCheck();
52                 }
53 
54             }
55         });
56         rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {   //监听rgrg RadioGroup组
57             @Override
58             public void onCheckedChanged(RadioGroup radioGroup, int i) {
59                 if (rb5.isChecked()) {
60                     page.setImageResource(R.drawable.map3);//用于显示自己添加的图片
61                     rg1.clearCheck();// ,用于显示单选,不出现多选的情况
62                 }
63                 if (rb6.isChecked()) {
64                     page.setImageResource(R.drawable.map4);
65                     rg1.clearCheck();
66                 }
67                 if (rb7.isChecked()) {
68                     page.setImageResource(R.drawable.map5);
69                     rg1.clearCheck();
70                 }
71 
72             }
73         });
74     }
75 }
第一个布局文件刚开始使用图形化界面布局,后来就有点搞乱了,又使用直接写代码的方法又改了一下

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.zhoushasha.myapplication6.MainActivity"
    android:weightSum="1"
    android:baselineAligned="false"
    android:orientation="vertical">

    <TextView
       android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="please you choose a flower you like:"
        android:id="@+id/text"
        android:layout_gravity="center"
        android:textSize="30dp"
        android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true"
        android:singleLine="true"
    android:scrollbars="horizontal" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="100sp"
        android:id="@+id/page"
        android:scaleType="center"
        android:adjustViewBounds="false"
        android:cropToPadding="false"
        android:src="@mipmap/map"
        android:layout_marginTop="50dp" />

 <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="75dp">
     //使用两个RadioGroup可以实现两行,三列的布局

        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="58dp"
            android:id="@+id/rg1"
            android:orientation="horizontal" >

            <RadioButton
                android:text="梅花"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/rb8"
                android:textSize="25dp" />

            <RadioButton
                android:text="石楠花"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/rb10"
                android:textSize="25dp" />

            <RadioButton
                android:text="象牙花"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/rb9"
                android:textSize="25dp"
                android:layout_weight="1" />
        </RadioGroup>
   </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.10">
    <RadioGroup
        android:layout_width="match_parent"
        android:id="@+id/rg2"
        android:orientation="horizontal"
        android:layout_height="wrap_content"
        android:layout_weight="0.90" />

        <RadioButton
            android:text="牡丹花"
            android:layout_width="wrap_content"
            android:id="@+id/rb7"
            android:textSize="25dp"
            android:layout_height="wrap_content" />

        <RadioButton
            android:id="@+id/rb5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="绣球花"
        android:textSize="25dp"/>

        <RadioButton
            android:text="玉兰花"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/rb6"
            android:textSize="25dp"/>
    </LinearLayout>
   </LinearLayout>

 

 第二个作业

1.使图片随着鼠标移动,显示坐标信息

2.点击按钮,给出提示信息退出

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     xmlns:app="http://schemas.android.com/apk/res-auto"
 5     xmlns:tools="http://schemas.android.com/tools"
 6     android:id="@+id/activity_main"
 7     android:layout_width="match_parent"
 8     android:layout_height="match_parent"
 9     tools:context="com.example.zhoushasha.myapplication7.MainActivity"
10     android:weightSum="1">
11 //简单的设置两个控件,以及几个属性
12  <ImageView
13      android:id="@+id/mp"
14      android:layout_width="96dp"
15      android:layout_height="wrap_content"
16      android:layout_alignParentTop="true"
17      app:srcCompat="@drawable/map"
18      android:layout_alignParentLeft="true"
19      android:layout_alignParentStart="true"
20      android:layout_weight="0.14" />
21  <LinearLayout
22      android:layout_width="wrap_content"
23      android:layout_height="wrap_content"/>
24  <Button
25     android:id="@+id/bt"
26     android:layout_width="wrap_content"
27    android:layout_height="wrap_content"
28    android:text="退出"
29    android:layout_gravity="bottom"
30  android:textSize="30dp" />
31 </LinearLayout>

 

 1 package com.example.zhoushasha.myapplication7;
 2 
 3 import android.support.v7.app.AppCompatActivity;
 4 import android.os.Bundle;
 5 import android.view.MotionEvent;
 6 import android.view.View;
 7 import android.widget.Button;
 8 import android.widget.ImageView;
 9 import android.widget.Toast;
10 
11 public class MainActivity extends AppCompatActivity {
12     //获取控件
13     private ImageView mp;
14     private Button bt;
15 
16 
17     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_main);
21 
22         mp = (ImageView) findViewById(R.id.mp);
23         bt = (Button) findViewById(R.id.bt);
24         //用setOnClickListenne添加监控对象bt
25       bt.setOnClickListener(new View.OnClickListener() {
26           @Override
27           public void onClick(View v) {
28               Toast.makeText(MainActivity.this, "再按一次退出按钮",
29                       Toast.LENGTH_LONG).show();
30               bt.setOnClickListener(new View.OnClickListener() {
31                   @Override
32                   public void onClick(View v) {
33                       MainActivity.this.finish();
34                   }
35               });
36           }
37       });
38     }
39 
40         public boolean onTouchEvent(MotionEvent event) {
41             //区分触屏事件的方式
42             if (event.getAction() ==MotionEvent.ACTION_MOVE) {
43                 String pos="";
44                 float x =event.getX();//得到x轴的坐标
45                 float y =event.getY();//得到Y轴的坐标
46                  pos ="X坐标:"+x+",Y坐标"+y;
47                 mp.setX(x-200);
48                 mp.setY(y-400);
49                 Toast.makeText(this,pos,Toast.LENGTH_LONG).show();
50             }
51             return super.onTouchEvent(event);
52         }
53 
54  }

        

以上部分方法,和代码使用来自于百度。