返回列表 發帖
  1. package com.example.student.myapplication;

  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. public class MainActivity extends AppCompatActivity {

  10.     private int imgId[] = {R.drawable.img01, R.drawable.img02, R.drawable.img03, R.drawable.img04, R.drawable.img05, R.drawable.img06};
  11.     private Button btn1, btn2, end;
  12.     private ImageView imageView;
  13.     private int p = 0, count = imgId.length;


  14.     @Override
  15.     protected void onCreate(Bundle savedInstanceState) {
  16.         super.onCreate(savedInstanceState);
  17.         setContentView(R.layout.activity_main);
  18.         setTitle("第 " + (p + 1) + " / " + count + " 張");
  19.         btn2 = (Button) findViewById(R.id.button2);
  20.         btn1 = (Button) findViewById(R.id.button1);
  21.         end = (Button) findViewById(R.id.end);
  22.         imageView = (ImageView) findViewById(R.id.imageView);
  23.         btn1.setOnClickListener(myListener);
  24.         btn2.setOnClickListener(myListener);
  25.         end.setOnClickListener(myListener);
  26.     }

  27.     public View.OnClickListener myListener = new View.OnClickListener() {
  28.         @Override
  29.         public void onClick(View v) {
  30.             switch (v.getId()) {
  31.                 case R.id.button1:
  32.                     p--;
  33.                     if (p < 0)
  34.                         p = count - 1;
  35.                     imageView.setImageResource(imgId[p]);
  36.                     setTitle("第 " + (p + 1) + " / " + count + " 張");
  37.                     break;
  38.                 case R.id.button2:
  39.                     p++;
  40.                     if (p == count)
  41.                         p = 0;
  42.                     imageView.setImageResource(imgId[p]);
  43.                     setTitle("第 " + (p + 1) + " / " + count + " 張");
  44.                     break;
  45.                 case R.id.end:
  46.                     AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
  47.                     builder.setTitle("確認視窗");
  48.                     builder.setIcon(R.mipmap.ic_launcher);
  49.                     builder.setMessage("即將離開應用程式!");
  50.                     builder.setPositiveButton("確認", new DialogInterface.OnClickListener() {
  51.                         @Override
  52.                         public void onClick(DialogInterface dialog, int which) {
  53.                             finish();
  54.                         }
  55.                     });
  56.                     builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
  57.                         @Override
  58.                         public void onClick(DialogInterface dialog, int which) {


  59.                         }
  60.                     });
  61.                     builder.show();
  62.                     builder.setCancelable(false);
  63.                     break;
  64.             }
  65.         }
  66.     };
  67. }
複製代碼
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  4.     android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
  5.     android:paddingRight="@dimen/activity_horizontal_margin"
  6.     android:paddingTop="@dimen/activity_vertical_margin"
  7.     android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
  8.     android:background="#000000">



  9.     <Button
  10.         android:layout_width="wrap_content"
  11.         android:layout_height="wrap_content"
  12.         android:text="下一頁"
  13.         android:id="@+id/button2"
  14.         android:layout_alignParentBottom="true"
  15.         android:layout_alignParentEnd="true"
  16.         android:layout_marginBottom="172dp" />

  17.     <Button
  18.         android:layout_width="wrap_content"
  19.         android:layout_height="wrap_content"
  20.         android:text="上一頁"
  21.         android:id="@+id/button1"
  22.         android:layout_alignTop="@+id/button2"
  23.         android:layout_alignParentStart="true" />

  24.     <ImageView
  25.         android:layout_width="wrap_content"
  26.         android:layout_height="wrap_content"
  27.         android:id="@+id/imageView"
  28.         android:layout_alignParentTop="true"
  29.         android:layout_alignEnd="@+id/button2"
  30.         android:layout_alignParentStart="true"
  31.         android:layout_above="@+id/button2" />

  32.     <Button
  33.         android:layout_width="wrap_content"
  34.         android:layout_height="wrap_content"
  35.         android:text="結束"
  36.         android:id="@+id/end"
  37.         android:layout_below="@+id/button1"
  38.         android:layout_alignParentStart="true"
  39.         android:layout_marginTop="46dp" />

  40. </RelativeLayout>
複製代碼

TOP

返回列表