- package com.smcs.yingwu.imageview;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.Button;
- import android.widget.ImageView;
- public class MainActivity extends AppCompatActivity {
- Button b;
- Button n;
- ImageView imageV;
- int num=1;
- int[] image = new int[6];
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- init();
- }
- public void init()
- {
- n = findViewById(R.id.back);
- b = findViewById(R.id.next);
- imageV = findViewById(R.id.imageView);
- image[0] = R.drawable.img01;
- image[1] = R.drawable.img02;
- image[2] = R.drawable.img03;
- image[3] = R.drawable.img04;
- image[4] = R.drawable.img05;
- image[5] = R.drawable.img06;
- imageV.setImageResource(image[0]);
- n.setOnClickListener(new View.OnClickListener(){
- @Override
- public void onClick(View v) {
- if(num != 6)
- {
- num++;
- imageV.setImageResource(image[num-1]);
- }
- }
- });
- b.setOnClickListener(new View.OnClickListener(){
- @Override
- public void onClick(View v) {
- if(num != 0)
- {
- num--;
- imageV.setImageResource(image[num-1]);
- }
- }
- });
- }
- }
複製代碼- <?xml version="1.0" encoding="utf-8"?>
- <android.widget.AbsoluteLayout 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:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <ImageView
- android:id="@+id/imageView"
- android:layout_width="match_parent"
- android:layout_height="219dp"
- android:layout_x="0dp"
- android:layout_y="47dp"
- android:contentDescription="@string/app_name"
- app:srcCompat="@drawable/img01"
- tools:layout_editor_absoluteX="174dp"
- tools:layout_editor_absoluteY="187dp" />
- <Button
- android:id="@+id/back"
- android:layout_width="146dp"
- android:layout_height="125dp"
- android:layout_x="29dp"
- android:layout_y="338dp"
- android:text="@string/back"
- tools:layout_editor_absoluteX="16dp"
- tools:layout_editor_absoluteY="340dp" />
- <Button
- android:id="@+id/next"
- android:layout_width="155dp"
- android:layout_height="126dp"
- android:layout_x="192dp"
- android:layout_y="338dp"
- android:text="@string/next"
- tools:layout_editor_absoluteX="252dp"
- tools:layout_editor_absoluteY="412dp" />
複製代碼 |