本帖最後由 李知易 於 2018-11-9 21:12 編輯
- package com.example.user.hw20181021;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.ImageView;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- ImageView imageView;
- int image[]=new int[6];
- int index=0;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- imageView=(ImageView)findViewById(R.id.imageView);
- this.setTitle("第"+(index+1)+"/6張");
- 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;
- imageView.setImageResource(image[0]);
- }
- public void previous(View view) {
- if (index > 0) {
- imageView.setImageResource(image[--index]);
- this.setTitle("第" + (index + 1) + "/6張");
- }else{
- final AlertDialog dialog =new AlertDialog.Builder(MainActivity.this)
- .setTitle("Error")
- .setMessage("This is the first page!")
- .setCancelable(false)
- .setPositiveButton("Okay", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(getApplicationContext(),"Okay", Toast.LENGTH_SHORT).show();
- }
- })
- .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(getApplicationContext(),"Cancel", Toast.LENGTH_SHORT).show();
- }
- })
- .show();
- }
- }
- public void next(View view) {
- if (index < image.length - 1) {
- imageView.setImageResource(image[++index]);
- this.setTitle("第" + (index + 1) + "/6張");
- }else{
- final AlertDialog dialog =new AlertDialog.Builder(MainActivity.this)
- .setTitle("Error")
- .setMessage("It is already the last page!")
- .setCancelable(false)
- .setPositiveButton("Okay", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(getApplicationContext(), "Okay", Toast.LENGTH_SHORT).show();
- }
- })
- .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(getApplicationContext(), "Cancel", Toast.LENGTH_SHORT).show();
- }
- })
- .setCancelable(false)
- .show();
- }
- }
- public void left(View view) {
- }
- }
複製代碼 |