简体   繁体   中英

Increase value of TextView e.g. 0 as clicked value +1

I am developing my program but it seems It really giving me hard time regarding TextView.XD Just like in Scoreboard. Increasing the value of score as you click the TextView assigned to it.

This is my code:

private OnClickListener mHscoreListener = new OnClickListener() {
    public void onClick(View v) {
        //DO INCREASE
        h1++;
        TextView HScore = (TextView) findViewById(R.id.hscore);
        HScore.setText(h1);
    };
};

The above code not working and I don't know why.

I think you should set onClickListener to TextView HScore.

Try this way Define HScore and h1 as class variable.

HScore = (TextView) findViewById(R.id.hscore);
OnClickListener mHscoreListener = new OnClickListener()
{
    public void onClick(View v)
    {
     // DO INCREASE
     h1++;
     HScore.setText(h1 + "");
    };
};
HScore.setOnClickListener(mHscoreListener);

What type is h1 ?

You may need to use h1.toString()

private OnClickListener mHscoreListener = new OnClickListener() {
    public void onClick(View v) {
        //DO INCREASE
        h1++;
        TextView HScore = (TextView) findViewById(R.id.hscore);
        HScore.setText(h1.toString());
    };
};

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