如何在Android Studio中使ImageButton可点击?

时间:2017-07-07 03:46:10

标签: java android methods imagebutton clickable

我已经做了大量的研究,无论出于什么原因,无论我尝试什么,我都无法在Android工作室中获得可点击的ImageButton。我尝试了很多东西,但我必须遗漏一些东西。我将通过下面的XML文件,然后是Java。当我使用setOnClickListener方法时,我得到一个无法解析的消息,当我执行onClickListener时也是如此。我想按钮链接到网页。请帮忙!

   <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.autismacademyed.www.autismacademy.AutismAcademy">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:textColor="@android:color/black"
        android:textSize="36sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.173" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/logo"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintHorizontal_bias="0.0" />

    <ImageButton
        android:id="@+id/imageButtonYellow"
        android:layout_width="109dp"
        android:layout_height="125dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:background="@null"
        android:scaleType="centerCrop"
        android:visibility="visible"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView2"
        app:srcCompat="@mipmap/ic_yellowpuzzlepiece"
        android:onClick="onClick"/>

</android.support.constraint.ConstraintLayout>

这是Java:

package com.autismacademyed.www.autismacademy;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;

public class AutismAcademy extends AppCompatActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_autism_academy);
    }

    ImageButton imageButtonYellow = (ImageButton)findViewById(R.id.imageButtonYellow);
   imageButtonYellow.setOnClickListener(new View.onClickListener()

    public void onClick (View v) {
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.aaed.org"));
        startActivity(browserIntent);
    }

}

2 个答案:

答案 0 :(得分:1)

删除setOnClickListener,因为您已在视图中指定按钮的onclick功能为onClick。为避免混淆,请重命名按钮android:onClick="onClick",例如android:onClick="imageButtonOnClick"

在你的java代码中你可以使用这个

package com.autismacademyed.www.autismacademy;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;

public class AutismAcademy extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_autism_academy);
    }

    public void imageButtonOnClick(View v) {
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.aaed.org"));
        startActivity(browserIntent);
    }

}

答案 1 :(得分:0)

只需复制并粘贴这两个文件即可。您声明了两个单击侦听器,这就是它无法正常工作的原因。

xml文件

  <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.autismacademyed.www.autismacademy.AutismAcademy">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:textColor="@android:color/black"
        android:textSize="36sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.173" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/logo"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintHorizontal_bias="0.0" />

    <ImageButton
        android:id="@+id/imageButtonYellow"
        android:layout_width="109dp"
        android:layout_height="125dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:background="@null"
        android:scaleType="centerCrop"
        android:visibility="visible"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView2"
        app:srcCompat="@mipmap/ic_yellowpuzzlepiece"
        android:onClick="buttonClick"/>

</android.support.constraint.ConstraintLayout>

<强> jAVAFILE

public class AutismAcademy extends AppCompatActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_autism_academy);
    }


    public void buttonClick(View v) {
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.aaed.org"));
        startActivity(browserIntent);
    }

}