標籤:listener image 映像 extc src new utf-8 fill near
本案例,使用Eclipse來開發Android2.1版本號碼的圖片查看器。
1)首先,開啟Eclipse。建立一個Android2.1版本號碼的項目ShowTu,開啟res/values中檔案夾下的strings.xml,將當中代碼替換成一下代碼:
路徑:ShowTu/res/valus/string.xml
<?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">Hello World, ShowTuActivity!</string> <string name="app_name">ShowTu</string><string name="pre">上一副</string><string name="after">下一副</string></resources>
2)開啟res/layout檔案下的main.xml,布局檔案裡有兩個button控制項和一個ImageView控制項。當中的代碼例如以下:
路徑:ShowTu/res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" ><!-- 水平線性布局 --><LinearLayout android:id="@+id/LinearLayout01" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_gravity="center" android:layout_height="wrap_content"> <!-- button控制項 --><Button android:text="@string/pre" android:id="@+id/Button01" android:textColor="#000000" android:layout_width="wrap_content" android:layout_height="wrap_content" ></Button><Button android:text="@string/after" android:id="@+id/Button02" android:textColor="#000000" android:layout_width="wrap_content" android:layout_height="wrap_content" ></Button></LinearLayout><ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"> </ImageView> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>
3)接著。主Activity檔案ShowTuActivity.java,代碼例如以下:
路徑:src/com.tu.ui/ShowTuActivity.java
package com.tu.ui;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.widget.Toast;public class ShowTuActivity extends Activity { /** Called when the activity is first created. */ImageView iv;//聲明ImageView控制項int count;//計數器int drawableIds[]={//圖片Id數組R.drawable.pic0,R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4,R.drawable.pic5,R.drawable.pic6,R.drawable.pic7,R.drawable.pic8,R.drawable.pic9}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);//設定布局 iv=(ImageView)findViewById(R.id.ImageView01);//擷取ImageView的引用 iv.setImageResource(R.drawable.pic0);//設定映像 Button pre_btn=(Button)findViewById(R.id.Button01);//擷取前一個button的引用 pre_btn.setOnClickListener(//加入監聽器 new OnClickListener() {@Overridepublic void onClick(View v){if(count>0){iv.setImageResource(drawableIds[--count]);//設定映像}else{Toast.makeText(ShowTuActivity.this,"這是第一幅圖片",Toast.LENGTH_SHORT).show();}} } ); Button after_btn=(Button)findViewById(R.id.Button02);//擷取下一個button的引用 after_btn.setOnClickListener(//加入監聽器 new OnClickListener() {@Overridepublic void onClick(View v){if(count<drawableIds.length-1){iv.setImageResource(drawableIds[++count]);//設定映像}else{Toast.makeText(ShowTuActivity.this,"這是最後一幅圖片",Toast.LENGTH_SHORT).show();}} } ); }}
效果例如以下:
圖(1)
圖(2)
Android中的圖片查看器