返回列表 發帖
  1. package com.smcs.yingwu.imageview;

  2. import android.os.Bundle;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.ImageView;

  7. public class MainActivity extends AppCompatActivity {
  8.     Button b;
  9.     Button n;
  10.     ImageView imageV;
  11.     int num=1;
  12.     int[] image = new int[6];
  13.    
  14.     @Override
  15.     protected void onCreate(Bundle savedInstanceState) {
  16.         super.onCreate(savedInstanceState);
  17.         setContentView(R.layout.activity_main);
  18.         init();



  19.     }
  20.     public void init()
  21.     {
  22.         n = findViewById(R.id.back);
  23.         b = findViewById(R.id.next);
  24.         imageV = findViewById(R.id.imageView);
  25.         image[0] = R.drawable.img01;
  26.         image[1] = R.drawable.img02;
  27.         image[2] = R.drawable.img03;
  28.         image[3] = R.drawable.img04;
  29.         image[4] = R.drawable.img05;
  30.         image[5] = R.drawable.img06;
  31.         imageV.setImageResource(image[0]);

  32.         n.setOnClickListener(new View.OnClickListener(){
  33.             @Override
  34.             public void onClick(View v) {
  35.                 if(num != 6)
  36.                 {
  37.                     num++;
  38.                     imageV.setImageResource(image[num-1]);
  39.                 }
  40.             }
  41.         });
  42.         b.setOnClickListener(new View.OnClickListener(){
  43.             @Override
  44.             public void onClick(View v) {
  45.                 if(num != 0)
  46.                 {
  47.                     num--;
  48.                     imageV.setImageResource(image[num-1]);
  49.                 }
  50.             }
  51.         });
  52.     }


  53. }
複製代碼
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.widget.AbsoluteLayout 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:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     tools:context=".MainActivity">

  8.     <ImageView
  9.         android:id="@+id/imageView"
  10.         android:layout_width="match_parent"
  11.         android:layout_height="219dp"
  12.         android:layout_x="0dp"
  13.         android:layout_y="47dp"
  14.         android:contentDescription="@string/app_name"
  15.         app:srcCompat="@drawable/img01"
  16.         tools:layout_editor_absoluteX="174dp"
  17.         tools:layout_editor_absoluteY="187dp" />

  18.     <Button
  19.         android:id="@+id/back"
  20.         android:layout_width="146dp"
  21.         android:layout_height="125dp"
  22.         android:layout_x="29dp"
  23.         android:layout_y="338dp"
  24.         android:text="@string/back"
  25.         tools:layout_editor_absoluteX="16dp"
  26.         tools:layout_editor_absoluteY="340dp" />

  27.     <Button
  28.         android:id="@+id/next"
  29.         android:layout_width="155dp"
  30.         android:layout_height="126dp"
  31.         android:layout_x="192dp"
  32.         android:layout_y="338dp"
  33.         android:text="@string/next"
  34.         tools:layout_editor_absoluteX="252dp"
  35.         tools:layout_editor_absoluteY="412dp" />
複製代碼

TOP

返回列表