综合作业——APP(2)

这时,我们就可以进行主活动页面的编写:因为原思想有三类事物,所以主活动页面中添加了三个按钮并且还有一个TextView,修改代码如下:

l >这是不在当前显示的引导页面所对应的小圆点的状态:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffff99">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/textview"
        android:textSize="34dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="25dp"
        android:background="@drawable/button_1"
        android:layout_marginLeft="80dp"
        android:layout_marginRight="80dp"
        android:text="@string/button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="100dp"      
        android:layout_below="@+id/button1"
        android:background="@drawable/button_2"
        android:layout_marginTop="25dp"
        android:layout_marginLeft="80dp"
        android:layout_marginRight="80dp"
        android:text="@string/button2" />
    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_below="@+id/button2"
        android:background="@drawable/button_3"
        android:layout_marginTop="25dp"
        android:layout_marginLeft="80dp"
        android:layout_marginRight="80dp"
        android:text="@string/button3" />
    </RelativeLayout>
MainActivity.java
public class MainActivity extends ActionBarActivity {
    private Button bt1;
    private Button bt2;
    private Button bt3;
    private TextView view;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		 bt1=(Button)findViewById(R.id.button1);
		 bt1.setOnClickListener(new OnClickListener()
		{
			public void onClick(View view){
				Intent intent=new Intent(MainActivity.this,TheFirstActivity.class);
				startActivity(intent);
			}
		});
		 bt2=(Button)findViewById(R.id.button2);
		 bt2.setOnClickListener(new OnClickListener()
			{
				public void onClick(View view){
					Intent intent=new Intent(MainActivity.this,TheSecondActivity.class);
					startActivity(intent);
				}
			});

		 bt3=(Button)findViewById(R.id.button3);
		 bt3.setOnClickListener(new OnClickListener()
			{
				public void onClick(View view){
					Intent 
intent=new Intent(MainActivity.this,TheThirdActivity.class);
					startActivity(intent);
				}
			});
		 view=(TextView)findViewById(R.id.textView1);
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}


MainActivity进行活动注册,修改AndroidManifest.xml中的代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="20" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
         .......
         <activity
            android:name="com.example.app.GuideActivity"
            android:label="@string/app_name"           android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
        </activity>
        <activity
            android:name="com.example.app.MainActivity"
            android:label="@string/app_name" >
        </activity>
 </application>
</manifest>

每点击一个按钮都会跳转到与其对应的活动页面,所以这里需要建立三个活动以及其对应的布局文件,但是,这里在显示事物的时候又要一个水果适配器(因为第一个活动显示的内容是水果,所以这里就用了水果适配器)以及定义一个水果实体类(原因同上)作为ListView适配器的适配类型,新建类Fruit,代码如下:

package com.example.app;
public class Fruit
{	
	private String name;
	private int imageId;
	public Fruit(String name,int imageId){
	this.name=name;
	this.imageId=imageId;
	}
	public String getName(){
		return name;
	}
   public int getimageId(){
	   return imageId;
   }
}

Fruit类中只有两个字段,name表示水果的名字,imageId表示水果对应图片的资源id,然后需要为ListView的子项指定一个我们自定义的布局,在layout目录下新建fruit_item.xml,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/friut_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/friut_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginLeft="10dip"  />

</LinearLayout>

在这个布局中,定义了一个ImageView用于显示水果的图片,又定义了一个TextView用于显示水果的名称。

接下来需要在com.example.app.adapte中创建一个自定义的适配器,这个适配器继承自ArrayAdapter,并将泛型指定为Fruit类。新建类FruitAdapter,代码如下:

public class FruitAdapter<ViewHolder> extends ArrayAdapter<Fruit> {
	private int resourceId;
	public FruitAdapter(Context context, int textViewResourceId,
			List<Fruit> objects) {
		super(context, textViewResourceId, objects);
		resourceId = textViewResourceId;
	}
	public View getView(int position, View convertView, ViewGroup parent) {
		Fruit fruit = getItem(position);
		View view;
		ViewHolder viewHolder;
		if(convertView==null){
			view=LayoutInflater.from(getContext()).inflate(resourceId, null);
			viewHolder=new ViewHolder();
			viewHolder.fruitImage=(ImageView)view.findViewById(R.id.friut_image);
			viewHolder.fruitName=(TextView)view.findViewById(R.id.friut_name);
            view.setTag(viewHolder);
		}else{
			view=convertView;
			viewHolder=(ViewHolder)view.getTag();
		}
		viewHolder.fruitImage.setImageResource(fruit.getimageId());
		viewHolder.fruitName.setText(fruit.getName());
		return view;
	}
	class ViewHolder{
		ImageView fruitImage;
		TextView fruitName;
	}
}

FruitAdapter重写了父类的一组构造函数,用于将上下文、ListView子项布局的id和数据都传递进来。类外又重写了getView()方法,这个方法在每个子项被滚动到屏幕内的时候被调用。在getView方法中,首先通过getItem()方法得到当前项的Fruit实例,然后使用LayoutInflater来为这个子项加载我们传入的布局,接着调用ViewfindViewById()方法分别获取到ImageViewTextView的实例,并分别调用他们的setImageResource()setText()方法来设置显示的图片和文字,最后将布局返回,这样我们自定义的适配器就完成了。

接着我们要为第一个活动指定一个自定义的布局,在layout目录下新建activity_thefirst.xml,代码如下所示:

第一个活动的布局文件:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/antiquewhite" 
    android:orientation="vertical" >
    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
 
  <Button 
      android:id="@+id/button_1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/button" /> 
  </LinearLayout>
   <ListView 
      android:id="@+id/list_view1" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:divider="@color/whitesmoke" /> 
</LinearLayout>

建立TheFirstActivity类,并修改TheFirstActivity类的代码

public class TheFirstActivity extends Activity
	{   	
		private Button button_1;
	    private List<Fruit> fruitList=new ArrayList<Fruit>();
		@Override
		protected void onCreate(Bundle savedInstanceState)
		{
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_thefirst);
			initFruits();//初始化水果数据
			FruitAdapter adapter=new  FruitAdapter(TheFirstActivity.this,R.layout.fruit_item,fruitList);
			ListView listView=(ListView)findViewById(R.id.list_view1);
			listView.setAdapter(adapter);
            listView.setOnItemClickListener(new OnItemClickListener(){
            	@Override
            	public void onItemClick(AdapterView<?>parent,View view,int position,long id){
            	   Fruit fruit=fruitList.get(position);
            	   Toast.makeText(TheFirstActivity.this, fruit.getName(),
            			   Toast.LENGTH_SHORT).show();
            	}
            });		
			button_1=(Button)findViewById(R.id.button_1);
			button_1.setOnClickListener(new OnClickListener()
			{
				public void onClick(View view){
					Intent intent=new Intent(TheFirstActivity.this,MainActivity.class);
					startActivity(intent);
				}
			});		
		}		
	private void initFruits(){
		Fruit apple=new Fruit("苹果【ping guo】Apple",R.drawable.apple_pic);
		fruitList.add(apple);
		Fruit banana=new Fruit("香蕉【xiang jiao】Banana",R.drawable.banana_pic);
		fruitList.add(banana);
		Fruit orange=new Fruit("橙子【cheng zi】Orange",R.drawable.orange_pic);
		fruitList.add(orange);
		Fruit watermelon=new Fruit("西瓜【xi gua】Watermelon",R.drawable.watermelon_pic);
		fruitList.add(watermelon);
		Fruit pear=new Fruit("梨【li】Pear",R.drawable.pear_pic);
		fruitList.add(pear);
		Fruit grape=new Fruit("葡萄【pu tao】Grape",R.drawable.grape_pic);
		fruitList.add(grape);
		Fruit pineapple=new Fruit("菠萝【bo luo】Pineapple",R.drawable.pineapple_pic);
		fruitList.add(pineapple);
		Fruit strawberry=new Fruit("草莓【cao mei】Strawberry",R.drawable.strawberry_pic);
		fruitList.add(strawberry);
		Fruit cherry=new Fruit("樱桃【ying tao】Cherry",R.drawable.cherry_pic);
		fruitList.add(cherry);
		Fruit mango=new Fruit("芒果【mang guo】Mango",R.drawable.mango_pic);
		fruitList.add(mango);
         }
	}

为第二个活动指定一个自定义的布局,在layout目录下新建activity_thesecond.xml,代码如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/antiquewhite" 
    android:orientation="vertical" >
    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
 
  <Button 
      android:id="@+id/button_2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/button" /> 
  </LinearLayout>
   <ListView 
      android:id="@+id/list_view2" 
      android:layout_width="match_parent" 
      android:layout_height="0dp"
      android:layout_weight="1" 
      android:divider="@color/whitesmoke" />  
</LinearLayout>

建立TheSecondActivity类,并修改TheSecondActivity类的代码

public class TheSecondActivity extends Activity
	{   	
		private Button button_2;
	    private List<Fruit> fruitList=new ArrayList<Fruit>();
		@Override
		protected void onCreate(Bundle savedInstanceState)
		{
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_thescond);
			initFruits();//初始化数据
			FruitAdapter adapter=new  FruitAdapter(TheSecondActivity.this,R.layout.fruit_item,fruitList);
			ListView listView=(ListView)findViewById(R.id.list_view2);
			listView.setAdapter(adapter);
			listView.setOnItemClickListener(new OnItemClickListener(){
            	@Override
            	public void onItemClick(AdapterView<?>parent,View view,int position,long id){
            	   Fruit fruit=fruitList.get(position);
            	   Toast.makeText(TheSecondActivity.this, fruit.getName(),
            			   Toast.LENGTH_SHORT).show();
            	}
            });			
			button_2=(Button)findViewById(R.id.button_2);
			button_2.setOnClickListener(new OnClickListener()
			{
				public void onClick(View view){
					Intent intent=new Intent(TheSecondActivity.this,MainActivity.class);
					startActivity(intent);
				}
			});
		}
	private void initFruits(){
		Fruit mouse=new Fruit("老鼠【lao shu】mouse",R.drawable.mouse_pic);
		fruitList.add(mouse);
		Fruit cow=new Fruit("奶牛【nai niu】cow",R.drawable.cow_pic);
		fruitList.add(cow);
		Fruit bird=new Fruit("小鸟【xiao niao】bird",R.drawable.bird_pic);
		fruitList.add(bird);
		Fruit duck=new Fruit("鸭子【ya zi】duck",R.drawable.duck_pic);
		fruitList.add(duck);
		Fruit turtle=new Fruit("乌龟【wu gui】turtle",R.drawable.turtle_pic);
		fruitList.add(turtle);
		Fruit chicken=new Fruit("小鸡【xiao ji】chicken",R.drawable.chicken_pic);
		fruitList.add(chicken);
		Fruit elephant=new Fruit("大象【da xiang】elephant",R.drawable.elephant_pic);
		fruitList.add(elephant);
		Fruit pig=new Fruit("猪【zhu】pig",R.drawable.pig_pic);
		fruitList.add(pig);
		Fruit zebra=new Fruit("斑马【ban ma】zebra",R.drawable.zebra_pic);
		fruitList.add(zebra);
		Fruit fish=new Fruit("鱼【yu】fish",R.drawable.fish_pic);
		fruitList.add(fish);
		Fruit frog=new Fruit("青蛙【qing wa】frog",R.drawable.frog_pic);
		fruitList.add(frog);
		Fruit snake=new Fruit("蛇【she】snake",R.drawable.snake_pic);
		fruitList.add(snake);
		Fruit dolphin=new Fruit("海豚【hai tun】dolphin",R.drawable.dolphin_pic);
		fruitList.add(dolphin);
		Fruit lion=new Fruit("狮子【shi zi】lion",R.drawable.lion_pic);
		fruitList.add(lion);
		Fruit squirrel=new Fruit("松鼠【song shu】squirrel",R.drawable.squirrel_pic);
		fruitList.add(squirrel);
         }
	}

为第三个活动指定一个自定义的布局,在layout目录下新建activity_thethird.xml,代码如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/antiquewhite" 
    android:orientation="vertical" >
    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
 
  <Button 
      android:id="@+id/button_3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/button" /> 
  </LinearLayout>
   <ListView 
      android:id="@+id/list_view3" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:divider="@color/whitesmoke" />    
</LinearLayout>

建立TheThirdActivity类,并修改TheThirdActivity类的代码

public class TheThirdActivity extends Activity
	{   	
		private Button button_3;
	    private List<Fruit> fruitList=new ArrayList<Fruit>();
		@Override
		protected void onCreate(Bundle savedInstanceState)
		{
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_thethird);
			initFruits();//初始化数据
			FruitAdapter adapter=new  FruitAdapter(TheThirdActivity.this,R.layout.fruit_item,fruitList);
			ListView listView=(ListView)findViewById(R.id.list_view3);
			listView.setAdapter(adapter);
			listView.setOnItemClickListener(new OnItemClickListener(){
            	@Override
            	public void onItemClick(AdapterView<?>parent,View view,int position,long id){
            	   Fruit fruit=fruitList.get(position);
            	   Toast.makeText(TheThirdActivity.this, fruit.getName(),
            			   Toast.LENGTH_SHORT).show();
            	}
            });		
			button_3=(Button)findViewById(R.id.button_3);
			button_3.setOnClickListener(new OnClickListener()
			{
				public void onClick(View view){
					Intent intent=new Intent(TheThirdActivity.this,MainActivity.class);
					startActivity(intent);
				}
			});
		}
	private void initFruits(){
		Fruit cabbage=new Fruit("卷心菜【juan xin cai】cabbage",R.drawable.cabbage_pic);
		fruitList.add(cabbage);
		Fruit carrot=new Fruit("胡萝卜【hu luo bo】carrot",R.drawable.carrot_pic);
		fruitList.add(carrot);
		Fruit celery=new Fruit("西芹【xi qin】celery",R.drawable.celery_pic);
		fruitList.add(celery);
		Fruit corn=new Fruit("玉米【yu mi】corn",R.drawable.corn_pic);
		fruitList.add(corn);
		Fruit bean=new Fruit("豌豆【wan dou】bean",R.drawable.bean_pic);
		fruitList.add(bean);
		Fruit eggplant=new Fruit("茄子【qie zi】eggplant",R.drawable.eggplant_pic);
		fruitList.add(eggplant);
		Fruit onion=new Fruit("洋葱【yang cong】onion",R.drawable.onion_pic);
		fruitList.add(onion);
		Fruit pimento=new Fruit("甜椒【tian jiao】pimento",R.drawable.pimento_pic);
		fruitList.add(pimento);
		Fruit potato=new Fruit("马铃薯【ma ling shu】potato",R.drawable.potato_pic);
		fruitList.add(potato);
		Fruit pumpkin=new Fruit("南瓜【nan gua】pumpkin",R.drawable.pumpkin_pic);
		fruitList.add(pumpkin);
		Fruit radish=new Fruit("小萝卜【xiao luo bo】radish",R.drawable.radish_pic);
		fruitList.add(radish);
		Fruit tomato=new Fruit("西红柿【xi hong shi】tomato",R.drawable.tomato_pic);
		fruitList.add(tomato);
         }}

可以看到,我们在三个活动里添加了initFriuts()方法用于初始化所有的水果数据。在Fruit类的构造函数中将水果的名字和对应的图片id传入,然后将创建好的对象添加到水果列表中。接着我们在onCreate()方法中创建了FruitAdapter对象,并将它作为适配器传递给了ListView。这里还有一个Toast()方法,显示点击图片对应的文字。然后我们还添加了一个setOnClickListener()方法,每当点击Back按钮时,就会执行监听器中的onClick()Intent()方法,返回主活动页面。

这样,一个简单的APP就完成了。














  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值