关于Android Studio中简单的图片切换

Posted yansans

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于Android Studio中简单的图片切换相关的知识,希望对你有一定的参考价值。

真的太简单了,甚至没有使用到Adaper

 

activity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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_
android:layout_
tools:context=".ImageActivity">

<ImageView
android:id="@+id/iv_image"
android:layout_
android:layout_
android:layout_margin="10dp"
android:layout_marginTop="64dp"
android:foregroundGravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.49"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_title"
app:layout_constraintVertical_bias="0.617"
tools:ignore="MissingConstraints" />

<EditText
android:id="@+id/et_image"
android:layout_
android:layout_
android:hint="请输入"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.411"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_title"
app:layout_constraintVertical_bias="0.027"
tools:ignore="MissingConstraints" />

<Button
android:id="@+id/button_image"
android:layout_
android:layout_
android:backgroundTint="#00000000"
android:text="查询"
android:textColor="#888888"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.423"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_title"
app:layout_constraintVertical_bias="0.162"
tools:ignore="MissingConstraints" />

</androidx.constraintlayout.widget.ConstraintLayout>

.java

package com.example.metro;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class ImageActivity extends AppCompatActivity implements View.OnClickListener

private ImageView iv_image;
private EditText et_image;
private Button button_imag;
private int id = R.drawable.beijing;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);

initView();


private void initView()
iv_image = findViewById(R.id.iv_image);
et_image = findViewById(R.id.et_image);

button_imag = findViewById(R.id.button_image);
button_imag.setOnClickListener(this);


@Override
public void onClick(View view)
String item = et_image.getText().toString();
if("a".equals(item))
id = R.drawable.a;
else if("b".equals(item))
id = R.drawable.b;
else
id = R.drawable.c;

iv_image.setImageResource(id);

图片放在drawable

图片的命名是 a b c 

以上是关于关于Android Studio中简单的图片切换的主要内容,如果未能解决你的问题,请参考以下文章

关于android studio几种常见的错误解决

Android Studio之多个Activity的滑动切换

在 Android Studio 的默认导航抽屉模板中使用按钮在片段之间切换

android studio中drawable怎么用

关于Android Studio中点9图的编译错误问题

关于Android studio调用高德地图的简单流程和要点