在单独的类中创建方法

时间:2015-01-22 21:40:08

标签: java android methods onclick

我想知道是否有人知道如何在单独的类中创建方法,并且能够在主活动中更改int值。如果你看一下下面这个简单的计数器,我希望能够在一个单独的类中的方法中将计数器+ = 1代码等放在onClicks中。任何帮助将非常感激。

public class MainActivity extends ActionBarActivity {

    int counter = 0;
    TextView counterDisplay;
    Button plusOne, minusOne;



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

        // Plus One Button
        plusOne = (Button) findViewById(R.id.btnPlus);
        counterDisplay = (TextView) findViewById(R.id.tv);


        plusOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                counter += 1;
                counterDisplay.setText("" + counter);
            }
        });

        // Subtract One Button
        minusOne = (Button) findViewById(R.id.btnMinus);
        counterDisplay = (TextView) findViewById(R.id.tv);

        minusOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                counter -= 1;
                counterDisplay.setText("" + counter);
            }
        });

    }
}

2 个答案:

答案 0 :(得分:0)

您可以从将匿名类提取到内部类开始:

...extends Activity {

   ...
   minusOne.setOnClickListener(new MyClickListener());
   ...

    private class MyClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            counter -= 1;
            counterDisplay.setText(String.valueOf(counter));
        }
    }

    ...
}

然后你可以创建私有内部类static。这意味着它不会持有对外部类Activity的引用,这意味着它不能更改Activity中的字段:

private static class MyClickListener implements View.OnClickListener {
    @Override
    public void onClick(View v) {
        counter -= 1; // ERROR - cannot resolve 
        counterDisplay.setText(String.valueOf(counter)); // ERROR cannot resolve
    }
}

所以,你可以做的是将这个类传递给它所需的所有依赖项,即 - 两个按钮,显示和计数器(如果需要):

public class MyActivity extends Activity {

    @Override
    public void onCreate() {
        ...
        View.OnClickListener clickListener = new MyClickListener(minusButton, plusButton, counterDisplay);
        minusButton.setOnClickListener(clickListener);
        plusButton.setOnClickListener(clickListener);
        ...
    }

    ...

    private static class MyClickListener implements View.OnClickListener {

        private final Button minusButton;
        private final Button plusButton;
        private final TextView counterDisplay;

        private counter;

        MyClickListener(Button minusButton, Button plusButton, TextView counterDisplay) {
            this.minusButton = minusButton;
            this.plusButton = plusButton;
            this.counterDisplay = counterDisplay;
        }

        ...
    }

}

现在您可以更新文字:

public class MyActivity extends Activity {

    @Override
    public void onCreate() {
        ...
        View.OnClickListener clickListener = new MyClickListener(minusButton, plusButton, counterDisplay);
        minusButton.setOnClickListener(clickListener);
        plusButton.setOnClickListener(clickListener);
        ...
    }

    ...

    private static class MyClickListener implements View.OnClickListener {

        private final Button minusButton;
        private final Button plusButton;
        private final TextView counterDisplay;

        private counter;

        MyClickListener(Button minusButton, Button plusButton, TextView counterDisplay) {
            this.minusButton = minusButton;
            this.plusButton = plusButton;
            this.counterDisplay = counterDisplay;
        }

        @Override
        public void onClick(View v) {
            if (v.getId() == minusButton.getId()) {
                counter--;
            } else if (v.getId() == plusButton.getId()) {
                counter++;
            }

            counterDisplay.setText(String.valueOf(counter));
        }
    }

}

答案 1 :(得分:-1)

将计数器传递给AsyncTask可能是个更好的主意。