安卓作业
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>