How to use java classes within one activity?

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

提交回复
热议问题