标签:代码 ndt this pad code back example xmlns end
今天继续研究了一下制作了一个简易小相册
代码附上
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>
效果图
标签:代码 ndt this pad code back example xmlns end
原文地址:https://www.cnblogs.com/sunhongbin/p/12303390.html