Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
126 views
in Technique[技术] by (71.8m points)

java - Getting a button to do an action in Android Studio

Im currently trying to convert my Java code to function within an android app. There are currently 3 classes, DecisionMap, DecisionNode and Utils. What the code does is that it builds a decision map from an unordered CSV file and then the user can navigate it by typing in either 1 or 2. The code works fine but for the life of me I cant get the buttons to navigateMap(). My original intent was to add another paramater into navigateMap as the which would be gotten from onClick but apparently you cant get a return from it?

The code for MainActivity.java :

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button yesButton = findViewById(R.id.yesButton);
        yesButton.setOnClickListener(this);

        final Button noButton = findViewById(R.id.noButton);
        noButton.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.yesButton:
                System.out.println("YES IS CLICKED");
                break;

            case R.id.noButton:
                System.out.println("NO IS CLICKED");
                break;
        }
    }

And the code im trying to implement:

public class DecisionMapTest {

    public static void main(String[] args) throws FileNotFoundException {

        Utils u = new Utils();
        Scanner choice = new Scanner(System.in);
        DecisionMap perec;
        try {
            perec = new DecisionMap();
        } catch(FileNotFoundException fe){
            u.console("File not found");return;
        }

        u.lineBreak();
        u.console("Start...");

        navigateMap(u, perec);


    }


    public static void navigateMap(Utils u, DecisionMap perec){
        DecisionNode node = perec.entryPoint();

        while(node != null) {

            u.console(node.getDescription());
            u.console(node.getQuestion());

            if ( node.getQuestion().equals("-")) {
                u.pressEnterToContinue();
                node = node.getYesNode();
            } else {
                int decision = u.fromConsoleGetInt("Yes or No? (press 1 for Yes or 2 No)");
                switch (decision) {
                    case 1:
                        node = node.getYesNode();
                        break;
                    case 2:
                        node = node.getNoNode();
                        break;
                }
            }

        }
    }
}





与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'm not an android expert by any means, but let me give you some of my initial guesses.

As far as I see, your DecisionMapTest class has a main method, which uses main thread. However, any android application will your its own corresponding thread.

So I would strongly suggested that, you first assign simple stupid functionality to your button clicks. So you can do the following:

  1. when button 1 is clicked: make a function call,run and print some text to screen
  2. when button 2 is clicked: make a function call, run and print some text to screen
  3. create a normal class without any main method. if you want to initialize some values, do it in the constructor of that class.
  4. initialize your class in the place where button click happens, and then call the corresponding method from this class.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...