openSearch() in Android beginners app not defined

前端 未结 3 1670
说谎
说谎 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.

    0 讨论(0)
  • 2020-12-03 11:05

    This is the code that you have to use in those methods:

    private void openSearch(){
        startActivity(new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH));
    }
    
    private void openSettings(){
        startActivity(new Intent(Settings.ACTION_SETTINGS));
    }
    

    The openSearch() method execute the google global search of the cellphone. The openSettings() method open the global configuration of the cellphone.

    I'm also a beginner in android,hope this helps with the question. Good Luck

    0 讨论(0)
  • 2020-12-03 11:09

    drees (thanks for Toast) answered the question for the satisfaction of the beginning android programmer (which I am), but to actually answer the question asked, you need to follow the instructions in the Setting Up The Search Interface article, however you need to preemptively understand how to place the commented material here into the switch statement's openSearch() method.

    I guess the real question is, though: Why does the android tutorial use this methodology instead of having a nonlinear explanation to add real content as the situation requires OR at least link to it to follow it out OR at least let you know that you're going to be obtain an end product that is at best broken code.

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