Android menu check box and androidmenu check box

Source: Internet
Author: User

Android menu check box and androidmenu check box

A beginner in android, which is currently in an android demand, needs to integrate a media player, add a checkbox in the menu, stop the player when the checkbox is selected, and start the player when the player is deselected, currently, I have enabled checkbox to stop the player.


When I unselect the checkbox, an error is reported. I haven't found the cause for a long time. The mood is bad .....

My code is as follows:
MainActivity

1234567891011121314151617181920212223242526272829303132 @Override    publicboolean onOptionsItemSelected(MenuItem item)    {        //Switch-sats i syfte om att det kommer tillkomma fler alternativ        //Switchen bygger på att hämta rätt id ifrån användarens val        switch(item.getItemId())        {        caseR.id.action_help:                //Bytar Activity till help                Intent intent = newIntent(MainActivity1.this, help.class);                startActivity(intent);            returntrue;         caseR.id.music:            finalCheckBox music = (CheckBox)findViewById(R.id.music);            music.setChecked(true);            music.setOnClickListener(newOnClickListener(){                publicvoid onClick(View v){                 if(((CheckBox) v).isChecked()) {                    backsound = MediaPlayer.create(MainActivity1.this, R.raw.backsound);                    backsound.start();                    backsound.setLooping(true);                }else{                    music.setChecked(false);                    backsound.stop();                }            }});         default:            returnsuper.onOptionsItemSelected(item);        }}}

Main_activity.xml

12345678910111213141516171819 <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item    android:id="@+id/action_settings"    android:orderInCategory="100"    android:showAsAction="never"    android:title="@string/action_settings"/> <item    android:id="@+id/action_help"    android:orderInCategory="100"    android:showAsAction="never"    android:title="@string/help"/> <item     android:id="@+id/music"     android:title="@string/musik"     android:checkable="true"     android:checked="true" />
Solution

1 <preferencescreenxmlns:android="http://schemas.android.com/apk/res/android">      <preferencecategoryandroid:title="@string/title_category"android:summary="@string/summary_category">            <checkboxpreferenceandroid:title="@string/title_main"android:summary="@string/summary_main"android:defaultvalue="true"android:key="main">                   </checkboxpreference></preferencecategory><!--Any other categories include here--></preferencescreen>


You only need to set setting android: dependancy to implement this function. Now you can create an xml file in the res directory, put the above xml code, and then perform the following operations:


 

1234567891011 publicclass SettingsActivity extendsPreferenceActivity {@Override    protectedvoid onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        addPreferencesFromResource(R.xml.preferences);    }}


Of course, you can also do it in other ways, such as fragments, but this method is simpler.


Address: http://www.itmmd.com/201411/156.html
This article is organized and published by Meng IT personnel. The reprinted article must indicate the source.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.