How to display an integer variable in a textView and add +1 to it every time I click on a button in the same activity?

0

I would like the textView to display the value of the variable I created in MainActivity "int numero = 0".

package genesysgeneration.a10;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    int numero = 0;
    private Button btnMais1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnMais1=(Button)findViewById(R.id.btnMais1);

    }
}

But I could not do it, so I put 0 to appear something in the image of the emulator and it becomes easier to understand what I would like to do.

Iwouldlikethebuttontochangethevalueofthevariabletoincreaseby1andtohavethetextViewupdated.

Iwouldalsoliketoknowifitwasnecessarytoputprivateorpublicbeforehavingdeclaredtheintegervariableint,type:

privateintnumero=0;

or

publicintnumero=0;

activity_main.xml

<?xmlversion="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="genesysgeneration.a10.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/tvContador"
        android:text="0" />

    <Button
        android:text="+ 1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/btnMais1" />
</RelativeLayout>
    
asked by anonymous 02.01.2017 / 23:08

1 answer

0

Just use the method OnClick() of button . To use this method, you need to apply a OnClickListener to your Button .

package genesysgeneration.a10;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    int numero = 0;
    private Button btnMais1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnMais1 =(Button) findViewById(R.id.btnMais1);
        btnMais1.setOnClickListener(maisHandle);
    }

    View.OnClickListener maisHandle = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             numero += 1; // adiciona +1 na váriavel numero
             TextView.setText(String.valueOf(numero)); // coloca o valor na textview
         }
     }
}

Using the variable numero as public or private does not exactly interfere with what you want to do. These are levels of visibility or to be more summarized, says who can access or not.

Private : The class that created it is the only one that can access it.

Public : All classes can access this variable.

This means that if the variable numero was set to public in its MainActivity all other classes could access it. Otherwise, only MainActivity could access this variable.

    
02.01.2017 / 23:28