簡體   English   中英

單擊時,帶有onClickListener的ImageView不會執行任何操作

[英]My ImageView with an onClickListener does nothing when clicked

我在圖像上設置了一個onClickListener,以便在單擊圖像時,該值增加10。該值在屏幕上實時顯示。 但是,當我在模擬器中運行該應用程序並單擊該圖像時,無論單擊多少次或持續多長時間,都不會發生任何反應。

我相關的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;
    }


}

}

以及布局的XML:

<?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>

有人知道如何解決此問題嗎?

您必須在onClick方法中更新Textfield:

@Override
public void onClick(View v) {

    switch (v.getId()) {

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

當前,您只是在更改變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM