返回列表 發帖
  1. import android.os.Bundle;
  2. import android.support.v7.app.AppCompatActivity;
  3. import android.view.View;
  4. import android.widget.ImageView;

  5. public class MainActivity extends AppCompatActivity {
  6.     ImageView imageView;
  7.     int image[]=new int[6];
  8.     int index=0;

  9.     @Override
  10.     protected void onCreate(Bundle savedInstanceState) {
  11.         super.onCreate(savedInstanceState);
  12.         setContentView(R.layout.activity_main);
  13.         imageView=(ImageView)findViewById(R.id.imageView);
  14.         this.setTitle("第"+(index+1)+"/6張");
  15.         image[0]=R.drawable.img01;
  16.         image[1]=R.drawable.img02;
  17.         image[2]=R.drawable.img03;
  18.         image[3]=R.drawable.img04;
  19.         image[4]=R.drawable.img05;
  20.         image[5]=R.drawable.img06;
  21.         imageView.setImageResource(image[0]);
  22.     }

  23.     public void previous(View view) {
  24.         if(index>0){
  25.             imageView.setImageResource(image[--index]);
  26.             this.setTitle("第"+(index+1)+"/6張");
  27.         }
  28.     }

  29.     public void next(View view) {
  30.         if(index<image.length - 1){
  31.             imageView.setImageResource(image[++index]);
  32.             this.setTitle("第"+(index+1)+"/6張");
  33.         }
  34.     }
  35. }
複製代碼

TOP

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:id="@+id/activity_main"
  6.     android:layout_width="match_parent"
  7.     android:layout_height="match_parent"
  8.     tools:context="ray.ap.hw1071021.MainActivity"
  9.     android:orientation="vertical">

  10.     <ImageView
  11.         android:layout_width="match_parent"
  12.         android:layout_height="300dp"
  13.         app:srcCompat="@drawable/img01"
  14.         android:id="@+id/imageView"
  15.         />

  16.     <TableLayout
  17.         android:layout_width="match_parent"
  18.         android:layout_height="match_parent"
  19.         android:stretchColumns="0,1" >
  20.         <TableRow>
  21.         <Button
  22.             android:text="上一頁"
  23.             android:layout_width="match_parent"
  24.             android:layout_height="wrap_content"
  25.             android:id="@+id/button"
  26.             android:onClick="previous" />
  27.         <Button
  28.             android:text="下一頁"
  29.             android:layout_width="match_parent"
  30.             android:layout_height="wrap_content"
  31.             android:id="@+id/button2"
  32.             android:layout_alignTop="@+id/button"
  33.             android:onClick="next" />
  34.         </TableRow>
  35.     </TableLayout>



  36. </LinearLayout>
複製代碼

TOP

返回列表