How to use java classes within one activity?

后端 未结 4 660
一生所求
一生所求 2021-01-21 17:16

How can you use a java class within one activity, by that I mean is having different components of that activity spread out in a bunch of java classes. I\'m a little new to andr

4条回答
  •  情话喂你
    2021-01-21 17:31

    I think that you should rearrange the code to have the onClickListener inside Something constructor:

    Something.java

    import android.view.View;
    import android.widget.EditText;
    import android.widget.Button;
    import android.app.Activity;
    
    public class Something {
    
        private Activity activity;
        private Button add, subtract, multiply, devide;
        private EditText editA, editB, editC;
        private double doubleA, doubleB, doubleC;
    
        public Something(Activity a) {
    
            activity = a;
    
            editA = (EditText) activity.findViewById(R.id.editText);
            editB = (EditText) activity.findViewById(R.id.editText2);
            editC = (EditText) activity.findViewById(R.id.editText3);
    
            add = (Button) activity.findViewById(R.id.add);
            subtract = (Button) activity.findViewById(R.id.subtract);
            multiply = (Button) activity.findViewById(R.id.multiply);
            devide = (Button) activity.findViewById(R.id.devide);
    
            add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    click(1);
    
                }
            });
    
            subtract.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    click(2);
    
                }
            });
    
            multiply.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    click(3);
                }
            });
    
            devide.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    click(4);
                }
            });
        }
    
    
        public void click(int calculate) {
    
            // assume number entered - no error
            doubleA = Double.parseDouble(editA.getText().toString());
            doubleB = Double.parseDouble(editB.getText().toString());
    
            switch (calculate) {
    
                case 1:
                    doubleC = doubleA + doubleB;
                    String s = "" + doubleC;
                    editC.setText(s);
                    break;
    
                case 2:
                    doubleC = doubleA - doubleB;
                    String s = "" + doubleC;
                    editC.setText(s);
                    break;
    
                case 3:
                    doubleC = doubleA * doubleB;
                    String s = "" + doubleC;
                    editC.setText(s);
                    break;
    
                case 4:
                    doubleC = doubleA / doubleB;
                    String s = "" + doubleC;
                    editC.setText(s);
                    break;
    
                default:
                    break;
    
            }
    
        }
    
    }
    

    I think the problem was putting 'click()' method inside the main constructor runs through the code once only. I hope this helps!

提交回复
热议问题