简体   繁体   中英

TextView attribute in android for displaying intergers?

public void onClick(View v) {
        if (v == button1){
            counter++;
            textView2.setText(Integer.toString(counter));
            textView2.setText(counter);
        }

    }

I have tried to create a program that counts the number of clicks on a button, but for some reason it is not displaying it. Here is my textview component.

<TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="180dp"
        android:layout_alignParentTop="true"
        android:layout_above="@+id/button1"
        android:layout_toRightOf="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:textColor="#000000"/>

I can't seem to find an attribute for display the number of clicks. Is this because it interprets it as an interger instead of a string?

textView2.setText(String.valueOf(counter));

这将获取您的Integer并将其转换为它的String值,并将其设置为TextView。

try casting your counter variable to string variable and then try assigning it to button text as:

public void onClick(View v) {
        if (v == button1){
            counter++;
            String s=counter.toString();
            textView2.setText(S);
        }

    }

what your code might be doing as of what you have posted you are trying to pass an integer which is creating problem here..

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