- package com.example.plantsai.myapplicationpic;
- import android.media.Image;
- import android.support.v7.app.ActionBar;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.TextView;
- public class MainActivity extends AppCompatActivity {
- Button p;
- Button n;
- ImageView iv;
- int num=1;
- TextView tv;
- 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.button2);
- p = findViewById(R.id.button);
- iv = findViewById(R.id.imageView);
- tv = findViewById(R.id.textView);
- 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;
- iv.setImageResource(image[0]);
- rename();
- n.setOnClickListener(new View.OnClickListener(){
- @Override
- public void onClick(View v) {
- if(num != 6)
- {
- num++;
- rename();
- iv.setImageResource(image[num-1]);
- }
- }
- });
- p.setOnClickListener(new View.OnClickListener(){
- @Override
- public void onClick(View v) {
- if(num != 0)
- {
- num--;
- rename();
- iv.setImageResource(image[num-1]);
- }
- }
- });
- }
- public void rename()
- {
- tv.setText("第"+num+"/6張");
- }
- }
複製代碼- <?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="289dp"
- android:layout_height="219dp"
- android:layout_x="50dp"
- android:layout_y="136dp"
- app:srcCompat="@drawable/img01"
- tools:layout_editor_absoluteX="174dp"
- tools:layout_editor_absoluteY="187dp"
- android:contentDescription="@string/app_name"/>
- <Button
- android:id="@+id/button"
- android:layout_width="127dp"
- android:layout_height="wrap_content"
- android:layout_x="63dp"
- android:layout_y="437dp"
- android:text="Previous"
- tools:layout_editor_absoluteX="80dp"
- tools:layout_editor_absoluteY="429dp" />
- <Button
- android:id="@+id/button2"
- android:layout_width="125dp"
- android:layout_height="wrap_content"
- android:layout_x="206dp"
- android:layout_y="439dp"
- android:text="Next"
- tools:layout_editor_absoluteX="285dp"
- tools:layout_editor_absoluteY="227dp" />
- <TextView
- android:id="@+id/textView"
- android:layout_width="174dp"
- android:layout_height="44dp"
- android:layout_x="0dp"
- android:layout_y="0dp"
- android:text="TextView"
- tools:layout_editor_absoluteX="59dp"
- tools:layout_editor_absoluteY="29dp" />
複製代碼 |