简体   繁体   中英

My ImageView with an onClickListener does nothing when clicked

I've set an onClickListener onto my image so that when the image is clicked, a value increases by 10. This value is displayed on-screen in what should be real-time. However, when I run the app in the emulator, and click the image, nothing happens, no matter how many times, or for how long, I click.

My relevant Java:

package com.bipbapapps.leagueclickerapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

public class MainClass extends Activity implements OnClickListener {

public float goldCount = 0.0f;
ImageView minionClick;
TextView textGoldCount;
String textTotal;

@Override
public void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Set fullscreen
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.mainlayout);



    //Linking the variables
    minionClick = (ImageView) findViewById(R.id.minioncentreid);
    textGoldCount = (TextView) findViewById(R.id.textviewtop);

    //String which will display at the top of the app
    textTotal = goldCount + " Gold";

    //Setting TextView to the String
    textGoldCount.setText(textTotal);

    //Setting onClickListener
    minionClick.setClickable(true);

    minionClick.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()){
    case R.id.minioncentreid:
    goldCount += 1.0;
    break;
    }


}

}

And the XML for the layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayoutid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/mainbackground"
android:weightSum="100"
>

<TextView
    android:id="@+id/textviewtop"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="22"
    android:textAlignment="center"
    android:textColor="#FF6600"
    android:textSize="25sp"
    android:text="@string/app_name"
    >        


</TextView>
<ImageView
    android:id="@+id/minioncentreid"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="50"
    android:contentDescription="@+string/desc"
    android:src="@drawable/minioncentrethree"
    android:clickable="true"
    />
<TextView 
    android:id="@+id/textviewbottom"
    android:layout_weight="28"
    android:layout_height="0dip"
    android:layout_width="wrap_content"
    >
    </TextView>

</LinearLayout>

Anyone have any idea how to fix this issue?

You have to update the Textfield in your onClick method:

@Override
public void onClick(View v) {

    switch (v.getId()) {

        case R.id.minioncentreid:
            goldCount += 1.0;
            textTotal = goldCount + " Gold";
            textGoldCount.setText(textTotal);
            break;
    }

Currently you are just changing the variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM