返回列表 發帖
  1. package com.example.plantsai.myapplicationpic;

  2. import android.media.Image;
  3. import android.support.v7.app.ActionBar;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.ImageView;
  9. import android.widget.TextView;

  10. public class MainActivity extends AppCompatActivity {
  11.     Button p;
  12.     Button n;
  13.     ImageView iv;
  14.     int num=1;
  15.     TextView tv;
  16.     int[] image = new int[6];
  17.     @Override
  18.     protected void onCreate(Bundle savedInstanceState) {
  19.         super.onCreate(savedInstanceState);
  20.         setContentView(R.layout.activity_main);
  21.         init();



  22.     }
  23.     public void init()
  24.     {
  25.         n = findViewById(R.id.button2);
  26.         p = findViewById(R.id.button);
  27.         iv = findViewById(R.id.imageView);
  28.         tv = findViewById(R.id.textView);
  29.         image[0] = R.drawable.img01;
  30.         image[1] = R.drawable.img02;
  31.         image[2] = R.drawable.img03;
  32.         image[3] = R.drawable.img04;
  33.         image[4] = R.drawable.img05;
  34.         image[5] = R.drawable.img06;
  35.         iv.setImageResource(image[0]);
  36.         rename();
  37.         n.setOnClickListener(new View.OnClickListener(){
  38.             @Override
  39.             public void onClick(View v) {
  40.                 if(num != 6)
  41.                 {
  42.                     num++;
  43.                     rename();
  44.                     iv.setImageResource(image[num-1]);
  45.                 }
  46.             }
  47.         });
  48.         p.setOnClickListener(new View.OnClickListener(){
  49.             @Override
  50.             public void onClick(View v) {
  51.                 if(num != 0)
  52.                 {
  53.                     num--;
  54.                     rename();
  55.                     iv.setImageResource(image[num-1]);
  56.                 }
  57.             }
  58.         });
  59.     }
  60.     public void rename()
  61.     {
  62.         tv.setText("第"+num+"/6張");
  63.     }


  64. }
複製代碼
  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="289dp"
  11.         android:layout_height="219dp"
  12.         android:layout_x="50dp"
  13.         android:layout_y="136dp"
  14.         app:srcCompat="@drawable/img01"
  15.         tools:layout_editor_absoluteX="174dp"
  16.         tools:layout_editor_absoluteY="187dp"
  17.         android:contentDescription="@string/app_name"/>

  18.     <Button
  19.         android:id="@+id/button"
  20.         android:layout_width="127dp"
  21.         android:layout_height="wrap_content"
  22.         android:layout_x="63dp"
  23.         android:layout_y="437dp"
  24.         android:text="Previous"
  25.         tools:layout_editor_absoluteX="80dp"
  26.         tools:layout_editor_absoluteY="429dp" />

  27.     <Button
  28.         android:id="@+id/button2"
  29.         android:layout_width="125dp"
  30.         android:layout_height="wrap_content"
  31.         android:layout_x="206dp"
  32.         android:layout_y="439dp"
  33.         android:text="Next"
  34.         tools:layout_editor_absoluteX="285dp"
  35.         tools:layout_editor_absoluteY="227dp" />

  36.     <TextView
  37.         android:id="@+id/textView"
  38.         android:layout_width="174dp"
  39.         android:layout_height="44dp"
  40.         android:layout_x="0dp"
  41.         android:layout_y="0dp"
  42.         android:text="TextView"
  43.         tools:layout_editor_absoluteX="59dp"
  44.         tools:layout_editor_absoluteY="29dp" />
複製代碼

TOP

返回列表