How to use java classes within one activity?

后端 未结 4 655
一生所求
一生所求 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!

    0 讨论(0)
  • 2021-01-21 17:34

    As I know this will help someone else who encountered the same problem as me will benefit, the problem was that I needed to pass the activity into my Something(Activity a){} constructor and initialize everything into that. Like Something(Activity a){
    aT1 = (EditText) a.findViewById(R.id.numberOne); aT2 = (EditText) a.findViewById(R.id.numberTwo); aT3 = (EditText) a.findViewById(R.id.result); add = (Button) a.findViewById(R.id.add); subtract = (Button) a.findViewById(R.id.subtract); multiply = (Button) a.findViewById(R.id.multiply); devide = (Button) a.findViewById(R.id.devide); }

    And all you need to do in main activity is pass the Activity when you make an instance/execute the code(ex: Something s = new Something(this);/new Something(this);) After you done that you can just call s.click(); and it should work.

    0 讨论(0)
  • 2021-01-21 17:48

    You can use your "something" class in the mainactivity.

    first initialize "Something" class in the main activity by

    Something s = new Something(MainActivity.this);
    

    Than you can use every method of that class in your main Activity.Just as you wanted,like this

    s.add.setonclicklistener(...
    

    or

    s.click();
    //Just use like this.
    
    0 讨论(0)
  • 2021-01-21 17:51

    like a before post XD with 1 java class, call it again after pressing buttons, for example, you can have a invisible buttons, radiobuttons whatever you want invisible and just with a click it turns visible and useful for you, here i go:

    First the variable which controls the activity are going to do

    String num ="";
    

    then have your buttons, i use 2 of them and the others are invisible

    Button bn1;
    Button bn2;
    Button bn3;
    Button bn4;
    bn3.setVisibility(View.INVISIBLE);
    bn4.setVisibility(View.INVISIBLE);
    

    then the button code, depends on how many buttons you want

    Button.setOnClickListener(new Button onclickListener(){
            public void onClick(){
                //get a default variable in this case String num
        Intent intent = new Intent(MainActivity.this, MainActivity.class); num="cero"; intent.putExtra("po", num);
                            CodigoPeticion=2; startActivityForResult (intent,CodigoPeticion); finish(); break;
                    }
                }
            });
    

    this one to get the String num:

    Bundle extras = getIntent().getExtras();
            if (extras!= null) {
                num =extras.getString("po");
            }
    

    and at last but not least this one to do someting depending the String:

        if (num.matches("cero")){
                    //do something, enable more buttons, disable radiobuttons,
    bn3.setVisibility(View.VISIBLE);
                }else if(num.matches("one")){//this string is from another button
                    //do something else in the same activity, as you spected enable radiobuttons, show a image, etc
    bn4.setVisibility(View.VISIBLE);
                }else{
                    //some textview with a specific title
                    TextView.setText("Something's Wrong");
                }
    

    don't forget the bn3 and bn4 listeners!

    hope that helps you, see ya!

    0 讨论(0)
提交回复
热议问题