无法用图像按钮交换 2 个图像

我尝试当我单击 ImageButton 时将视图和按钮交换为我在可绘制对象上的其他视图和按钮


我可以看到 TextView、ImageButton 和 Image View 的主要布局。当我单击按钮时,我的应用程序就会关闭。我尝试不同的方法,一切都有效。我应该在 xml 文件上声明 onClick 。


MAINACTIVITY.JAVA


package com.example.practica_walter_sin_layouts;


import androidx.appcompat.app.AppCompatActivity;


import android.os.Bundle;

import android.view.View;

import android.widget.ImageButton;

import android.widget.ImageView;


public class MainActivity extends AppCompatActivity {


    private ImageView walter;

    private ImageButton telefono;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }



    private void llamando(View vista){

        walter=findViewById(R.id.walter) ;

        telefono =findViewById(R.id.llamar) ;

        walter.setImageResource(R.drawable.walterwhite2);

        telefono.setImageResource(R.drawable.colgar);



    }


}


ACTIVITY_MAIN.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_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity">


    <TextView

        android:id="@+id/encabezado"

        android:layout_width="207dp"

        android:layout_height="47dp"

        android:text="Puedes llamar a walter white"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintLeft_toLeftOf="parent"

        app:layout_constraintRight_toRightOf="parent"

        app:layout_constraintTop_toTopOf="parent"

        app:layout_constraintVertical_bias="0.096" />


        />


</androidx.constraintlayout.widget.ConstraintLayout>

应用程序应将视图和按钮交换为另一个视图和另一个按钮,但始终关闭。没有编译错误


收到一只叮咚
浏览 69回答 1
1回答

繁星点点滴滴

你的方法llamando需要是public
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java