返回列表 發帖
  1. package com.smcs.yingwu.newalertdialog;

  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. public class MainActivity extends AppCompatActivity {
  11.     Button back;
  12.     Button next;
  13.     ImageView ImageView;
  14.     int num = 1;
  15.     TextView textView;
  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.         next = findViewById(R.id.button2);
  26.         back = findViewById(R.id.button);
  27.         ImageView = findViewById(R.id.imageView);
  28.         textView = 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.         ImageView.setImageResource(image[0]);
  36.         rename();
  37.         next.setOnClickListener(new View.OnClickListener(){
  38.             @Override
  39.             public void onClick(View v) {
  40.                 if(num != 6)
  41.                 {
  42.                     num++;
  43.                     rename();
  44.                     ImageView.setImageResource(image[num-1]);
  45.                 }else{
  46.                     rename();
  47.                     new AlertDialog.Builder(MainActivity.this)
  48.                             .setTitle("錯誤訊息")
  49.                             .setMessage("已經到底了!")
  50.                             .setPositiveButton("確定", new DialogInterface.OnClickListener() {
  51.                                 @Override
  52.                                 public void onClick(DialogInterface dialog, int which) {

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

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

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

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


  98. }
複製代碼

TOP

返回列表