单独创建方法 类

Creating Methods in Separate Classes

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

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);
            }
        });

    }
}

您可以先将匿名 class 提取到内部 class:

...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));
        }
    }

    ...
}

然后你可以让那个私有内部classstatic。这意味着它不包含对外部 class、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
    }
}

因此,您可以做的是传递此 class 它需要的所有依赖项,即 - 两个按钮、显示和计数器(如果需要):

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));
        }
    }

}

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