简体   繁体   English

在android / JAVA中更改变量的值

[英]Changing value of variable in android /JAVA

I was trying to make a simple app in which clicking tapb Button increments the variable value of notaps and the reset Button sets it to 0. When I click on tapb it increments the value & clicking reset resets it but when I again click tabp it increments from the previous value. 我试图创建一个简单的应用程序,其中单击tapb Button增加tapb Button的变量值,重置Button将其设置为0.当我点击tapb它增加值并点击reset重置它但当我再次点击tabp它增量从以前的价值。

Eg : 例如:

init value of notaps = 0;

I click tabp 3 times and notaps value = 3 我点击tabp 3次, notaps值= 3

I click reset and notaps value = 0 我点击resetnotaps值= 0

I click tabp 3 times and notaps value = 4 我点击tabp 3次, notaps值= 4

    Button tapb = (Button)findViewById(R.id.tapb);
    Button reset = (Button)findViewById(R.id.reset);


    tapb.setOnClickListener(
            new Button.OnClickListener(){
                int notaps = 0;
                @Override
                public void onClick(View v) {
                    TextView taps = (TextView)findViewById(R.id.taps);
                    notaps++;
                    taps.setText(String.valueOf(notaps));

                }
            }
    );

    reset.setOnClickListener(
            new Button.OnClickListener() {

                @Override
                public void onClick(View v) {
                    TextView taps = (TextView) findViewById(R.id.taps);
                    int notaps=0;
                    taps.setText(String.valueOf(notaps));

                }
            }
    );

First, you have 2 instances of int named notaps that have nothing to do with each other. 首先,你有2个实例int命名notaps什么都没有做对方。 Your reset button does not set the right notaps variable. 您的重置按钮未设置正确的notaps变量。

Here's a snippet that should work. 这是一个应该有效的片段。

    private int notaps;

    Button tapb = (Button)findViewById(R.id.tapb);
    Button reset = (Button)findViewById(R.id.reset);
    TextView taps = (TextView)findViewById(R.id.taps);

    tapb.setOnClickListener(
        new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                notaps++;
                taps.setText(String.valueOf(notaps));
            }
        }
    );

    reset.setOnClickListener(
        new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                notaps = 0;
                taps.setText(String.valueOf(notaps));
            }
        }
    );

Try declaring your notaps variable globally meaning where every you declare you buttons declare your notaps variable there, here is what I mean. 尝试在全局范围内声明你的notaps变量意味着每个你声明按钮的地方都声明你的notaps变量,这就是我的意思。

Button tapb = (Button)findViewById(R.id.tapb);
Button reset = (Button)findViewById(R.id.reset);
int notaps = 0; // declare variable globally

tapb.setOnClickListener(
        new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                TextView taps = (TextView)findViewById(R.id.taps);
                taps.setText(String.valueOf(++notaps));

            }
        }
);

reset.setOnClickListener(
        new Button.OnClickListener() {

            @Override
            public void onClick(View v) {
                TextView taps = (TextView) findViewById(R.id.taps);                    
                taps.setText(String.valueOf(notaps=0));

            }
        }
);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM