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


  2. import android.app.AlertDialog;
  3. import android.content.DialogInterface;
  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. import android.widget.Toast;

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



  23.     }
  24.     public void init()
  25.     {
  26.         n = findViewById(R.id.button2);
  27.         p = findViewById(R.id.button);
  28.         iv = findViewById(R.id.imageView);
  29.         tv = findViewById(R.id.textView);
  30.         image[0] = R.drawable.img01;
  31.         image[1] = R.drawable.img02;
  32.         image[2] = R.drawable.img03;
  33.         image[3] = R.drawable.img04;
  34.         image[4] = R.drawable.img05;
  35.         image[5] = R.drawable.img06;
  36.         iv.setImageResource(image[0]);
  37.         rename();
  38.         n.setOnClickListener(new View.OnClickListener(){
  39.             @Override
  40.             public void onClick(View v) {
  41.                 if(num != 6)
  42.                 {
  43.                     num++;
  44.                     rename();
  45.                     iv.setImageResource(image[num-1]);
  46.                 }else{
  47.                     rename();
  48.                     new AlertDialog.Builder(MainActivity.this)
  49.                             .setTitle("錯誤訊息")
  50.                             .setMessage("已經到底了!")
  51.                             .setPositiveButton("確定", new DialogInterface.OnClickListener() {
  52.                                 @Override
  53.                                 public void onClick(DialogInterface dialog, int which) {

  54.                                 }
  55.                             })
  56.                             .setNegativeButton("取消", new DialogInterface.OnClickListener() {
  57.                                 @Override
  58.                                 public void onClick(DialogInterface dialog, int which) {

  59.                                 }
  60.                             })
  61.                             .show();
  62.                 }
  63.             }
  64.         });
  65.         p.setOnClickListener(new View.OnClickListener(){
  66.             @Override
  67.             public void onClick(View v) {
  68.                 if(num != 1)
  69.                 {
  70.                     num--;
  71.                     rename();
  72.                     iv.setImageResource(image[num-1]);
  73.                 }else{
  74.                     rename();
  75.                     new AlertDialog.Builder(MainActivity.this)
  76.                             .setTitle("錯誤訊息")
  77.                             .setMessage("已經到底了!")
  78.                             .setCancelable(false)
  79.                             .setPositiveButton("確定", new DialogInterface.OnClickListener() {
  80.                                 @Override
  81.                                 public void onClick(DialogInterface dialog, int which) {

  82.                                 }
  83.                             })
  84.                             .setNegativeButton("取消", new DialogInterface.OnClickListener() {
  85.                                 @Override
  86.                                 public void onClick(DialogInterface dialog, int which) {

  87.                                 }
  88.                             })
  89.                             .show();
  90.                 }
  91.             }
  92.         });
  93.     }
  94.     public void rename()
  95.     {
  96.         tv.setText("第"+num+"/6張");
  97.     }


  98. }
複製代碼
  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" />

  45. </android.widget.AbsoluteLayout>
複製代碼

TOP

返回列表