zoukankan      html  css  js  c++  java
  • 2.13日自学成果

    今天继续研究了一下制作了一个简易小相册

    代码附上

    mainActivity.java

    package com.example.xiangce;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
        private ImageView mImage;
        private TextView mText;
        private int num;
        private int index;
        private String[] title;
        private int[] images;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //初始化
            initView();
            //初始化构建
            initData();
        }
    
        private void initData() {
            title = new String[]{"第1张","第2张","第3张","第4张","第5张"};
            images = new int[]{R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e};
            mImage.setImageResource(images[0]);
            mText.setText(title[0]);
            num = title.length;//图片张数
            index = 0;//当前图片页数
        }
    
        private void initView() {
            mImage = findViewById(R.id.iv_show);
            mText = findViewById(R.id.tv_show);
            findViewById(R.id.btn_previous).setOnClickListener(this);
            findViewById(R.id.btn_next).setOnClickListener(this);
        }
    
        @Override
        public void onClick(View view) {
         //切换张数
            switch(view.getId()){
                case R.id.btn_previous:
                    if(index==0)
                    {
                        index=title.length-1;
                    }
                    else{
                    index--;}
                    break;
                case R.id.btn_next:
                    if(index==4){
                        index=0;
                    }else{
                        index++;
                    }
                    break;
            }
            updateImageAndTitle();
        }
    
        private void updateImageAndTitle() {
    
            mImage.setImageResource(images[index]);
            mText.setText(title[index]);
        }
    }

    main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
       <ImageView
           android:id="@+id/iv_show"
           android:layout_weight="1"
           android:background="@android:color/background_dark"
           android:layout_width="match_parent"
           android:layout_height="50dp"
           />
        <TextView
            android:id="@+id/tv_show"
            android:text="图片信息"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="10dp"
    />
        <LinearLayout
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
        <Button
            android:id="@+id/btn_previous"
            android:text="上一张"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    
        <Button
            android:id="@+id/btn_next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="下一张" />
    </LinearLayo>
    效果图
     
  • 相关阅读:
    java 异常 检查型和非检查型
    【jdbc】spring
    [事务] spring
    【事务】spring transaction 注解
    【线程同步】 Java 同步块(synchronized)详细说明
    【图片二进制接受】php
    C# 读取EXCEL文件的三种经典方法
    RS232 3线制与7线制的区别
    修改android 开机画面
    win7 vs2012+wdk8.0 搭建wdf驱动开发环境
  • 原文地址:https://www.cnblogs.com/sunhongbin/p/12303390.html
Copyright © 2011-2022 走看看