单击时,带有onClickListener的我的ImageView不执行任何操作

时间:2014-04-20 09:28:28

标签: java android eclipse android-layout imageview

我已将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>

任何人都知道如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您必须使用onClick方法更新文本字段:

@Override
public void onClick(View v) {

    switch (v.getId()) {

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

目前您只是在更改变量。