setOnCheckedChangeListener变量问题

时间:2014-02-06 22:18:23

标签: variables button toggle global

我正在尝试编写一个采用变量并计算分数的应用程序。在单击切换按钮时,变量设置为一个简单的数字,例如1.我试图将此数字导出为全局变量。之后,我想简单地将它们加在一起。但是我的代码没有这样做。到目前为止,还没有任何机构提供解决方案。

import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.ToggleButton;

public class NewActivity1 extends Activity{

public static int exportNumber1;
public static int exportNumber2;

public void calc(){

    int sum;
    sum = exportNumber1 + exportNumber2;
    TextView textView = (TextView) findViewById(R.id.textView11);
    textView.setText("The total is " + sum);
}



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.new_activity1);





    final ToggleButton atb1 = (ToggleButton) findViewById(R.id.toggleButton1);
    atb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean         isChecked) {

            int x = 0;

                if (isChecked)
                {
                    x = 1;
                }
                exportNumber1 = x;
                    }
                });

    final ToggleButton atb2 = (ToggleButton) findViewById(R.id.toggleButton2);  
    atb2.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            int x = 0;

                if (isChecked)
                {
                    x = 1;
                }
                exportNumber2 = x;
                    }
                });

} }

0 个答案:

没有答案