renderings
1 ) You can store the id of the image in the array and use the setImageResource() or setImageDrawable() method ( put it in the array for easy looping )
2) When it is already the first image, click "Previous Page" again, and Toast prompts : it is already the first image , and will not turn forward; similarly, when it is already the last image, click "Next Page" again. One page", should be prompted by Toast : it is the last image , and will not turn back .
give source code
<?xml version="1.0" encoding="utf-8"?> <FrameLayout 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" android:orientation="horizontal" tools:context="com.example.asus.a161304049_gary03.MainActivity"> <LinearLayout android:id="@+id/Layoutt" android:layout_width="match_parent" android:layout_height="215dp" android:orientation="vertical"> <ImageView android:id="@+id/imagexianshi" android:layout_width="match_parent" android:layout_height="180dp" app:srcCompat="@drawable/qzu1" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <Button android:id="@+id/Back" android:layout_width="100dp" android:layout_height="wrap_content" android:gravity="center" android:text="上一张" /> <Button android:id="@+id/Next" android:layout_width="100dp" android:layout_height="wrap_content" android:text="下一张" /> </LinearLayout> </LinearLayout> </FrameLayout>
package com.example.asus.a161304049_gary03; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity { // Use an array to store images int [] images = new int []{ R.drawable.qzu1, R.drawable.qzu2, R.drawable.qzu3, R.drawable.qzu5 }; // As the subscript of the picture int countImg = 0 ; // Forward and backward buttons private Button B1,B2; // Use ImageView to store pictures private ImageView Xianshi; @Override protected void onCreate (Bundle savedInstanceState) { super .onCreate (savedInstanceState); setContentView(R.layout.activity_main); B1 = (Button) findViewById(R.id.Back); B2 = (Button) findViewById(R.id.Next); Xianshi = (ImageView) findViewById(R.id.imagexianshi); Xianshi.setImageResource(images[ 0 ]); B1.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { // Here is a simple logic // If the selected picture is the first one, then the previous one cannot be selected. Output Toast prompt message if (countImg>0 ){ countImg -- ; // Use setImageResource to display pictures Xianshi.setImageResource(images[countImg]); }else{ Toast.makeText(MainActivity.this , "Already the first image, and will not turn forward" ,Toast.LENGTH_SHORT).show(); } } }); B2.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { // If the selected picture is the last one, then you can't select the next one. Output the message of Toast prompt if (countImg<3 ){ countImg++; Xianshi.setImageResource(images[countImg]); }else{ Toast.makeText(MainActivity.this , "It is the last image, and will not turn back" ,Toast.LENGTH_SHORT).show(); } } }); } }
The ImageView control is used to store pictures and display them. The xml attribute is not the focus of this article, so it will not be introduced here.
Step 1: Define member variables
// Use an array to store images int [] images = new int []{ R.drawable.qzu1, R.drawable.qzu2, R.drawable.qzu3, R.drawable.qzu5 }; // As the subscript of the picture int countImg = 0 ; // Forward and backward buttons private Button B1,B2; // Use ImageView to store pictures private ImageView Xianshi;
Step 2: Implement the event response mechanism for buttons "previous" and "next"
protected void onCreate (Bundle savedInstanceState) { super .onCreate (savedInstanceState); setContentView(R.layout.activity_main); B1 = (Button) findViewById(R.id.Back); B2 = (Button) findViewById(R.id.Next); Xianshi = (ImageView) findViewById(R.id.imagexianshi); Xianshi.setImageResource(images[ 0 ]); B1.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { // Here is a simple logic // If the selected picture is the first one, then the previous one cannot be selected. Output Toast prompt message if (countImg>0 ){ countImg -- ; // Use setImageResource to display pictures Xianshi.setImageResource(images[countImg]); }else{ Toast.makeText(MainActivity.this , "Already the first image, and will not turn forward" ,Toast.LENGTH_SHORT).show(); } } }); B2.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { // If the selected picture is the last one, then you can't select the next one. Output the message of Toast prompt if (countImg<3 ){ countImg++; Xianshi.setImageResource(images[countImg]); }else{ Toast.makeText(MainActivity.this , "It is the last image, and will not turn back" ,Toast.LENGTH_SHORT).show(); } } }); }
Portal: Several common uses of message mode Toast.make Text