2017-07-07 164 views
0

我做了大量的研究,無論我嘗試什麼原因,我都無法在Android工作室中獲得ImageButton的可點擊。我嘗試了很多東西,但我必須錯過一些東西。我將通過下面的XML文件,然後在下面的Java。當我放置setOnClickListener方法時,我得到一個無法解析的消息,並且當我做onClickListener時也是如此。我希望按鈕鏈接到網頁。請幫忙!如何讓ImageButton在Android Studio中可點擊?

<?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); 
    } 

} 
+0

有沒有必要設置onClickListener,因爲你已經指定的onClick在XML屬性,只是實現你的活動,方法和語法onClickListener是也錯了 – maRShmallow

+0

檢查我的答案,讓我知道發生了什麼 –

回答

0

取出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

謝謝!我簡直不敢相信那麼簡單!我從字面上花了數小時研究。這是我的第一個應用程序,所以我仍然在學習東西。 – ZachGiovanelli

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); 
    } 

} 
相關問題