openSearch() in Android beginners app not defined

前端 未结 3 1682
说谎
说谎 2020-12-03 10:42

I\'m just started the Android beginners tutotial and I now face a problem. On this page under \"Respond to Action Buttons\" it tells me to define a switch statement with som

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 10:45

    These methods are just examples that Google put in to show how you would use a switch statement. You can put anything you want in there, but I think the point is to make function calls from a switch statement, instead of putting the code of a function in the statement, to keep code clean. The functions would probably be declared in the same .java file in some fashion like

    private void openSearch() {
        // start or show the search activity/fragment
    }
    

    They can technically contain anything you want them to, depending on what you want the action bar button to do. If you simply want to see that the buttons work, you can splash a Toast notification to see something appear

    private void openSearch() {
        Toast.makeText(this, "Search button pressed", Toast.LENGTH_SHORT).show();
    }
    

    You'll have to import the Toast package which can be done by Ctrl+Shift+O. (Or Cmd+Shift+O for Mac)

    Hope this helps clear up confusion.

提交回复
热议问题