简体   繁体   English

增加TextView的值,例如0作为点击值+1

[英]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. 我正在开发程序,但似乎真的像给我记分板一样,给我关于TextView.XD的麻烦。 Increasing the value of score as you click the TextView assigned to it. 单击分配给它的TextView时,增加score的值。

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. 我认为您应该将onClickListener设置为TextView HScore。

Try this way Define HScore and h1 as class variable. 尝试这种方式将HScore和h1定义为类变量。

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 ? h1是什么类型?

You may need to use h1.toString() 您可能需要使用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());
    };
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么必须用值初始化某些变量(例如0,null)? - Why must some variables be initialized with a value (e.g. 0, null)? Groovy OpenCSV 读取带反斜杠的值(例如 "domain\\user\u0026quot; ) - Groovy OpenCSV read value with backslash (e.g. "domain\user" ) 在FreeMarker中,如何自动将自定义对象(例如java.awt.Color)转换为特定的String值(例如HTML十六进制颜色)? - In FreeMarker, how do I automatically convert a custom object (e.g. java.awt.Color) to a particular String value (e.g. HTML hex color)? JAXB:如果有特殊值(例如`null`或`Double.NaN`),则忽略元素序列化 - JAXB: ignore element serialization in case of special value (e.g. `null` or `Double.NaN`) 基于值顺序的Java排序对象列表(例如d,s,a,q,c) - Java Sort object list based on value order (e.g. d, s, a, q, c) 我将如何进行高效的键值存储(例如memcache)/简单的数据库? - How would I go about making an efficient key value store (e.g. memcache) / simple database? 有一个包装器 object 返回值(例如整数)会导致 Java 中的自动装箱吗? - Does having a wrapper object return value (e.g. Integer) cause auto boxing in Java? 如何检查 Cloud Firestore 中任何文档的集合中是否存在值(例如名称)? - How can I check if a value e.g. name exists in a collection within any of documents in Cloud Firestore? 用Java转换通用类型(例如T值=(T)inputParam) - Casting Generic Types in Java (e.g. T value = (T) inputParam) 如何检查数组的所有元素是否不等于某个值? (例如,不是空的) - How can I check if all elements of array doesn't equal to some value? (e.g. not empty)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM