Check box of android menu

Source: Internet
Author: User

Check box of android menu

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 26 27 28 29 30 31 32 @Override public boolean 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()) { case R.id.action_help: //Bytar Activity till help Intent intent = new Intent(MainActivity1.this, help.class); startActivity(intent); return true; case R.id.music: final CheckBox music = (CheckBox)findViewById(R.id.music); music.setChecked(true); music.setOnClickListener(new OnClickListener(){ public void 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: return super.onOptionsItemSelected(item); }}}

Main_activity.xml

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <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 <preferencescreen xmlns:android=http://schemas.android.com/apk/res/android> <preferencecategory android:title=@string/title_category android:summary=@string/summary_category> <checkboxpreference android:title=@string/title_main android:summary=@string/summary_main android:defaultvalue=true android:key=main> checkboxpreference> preferencecategory> 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:

 

1 2 3 4 5 6 7 8 9 10 11 public class SettingsActivity extends PreferenceActivity { @Override protected void 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.

 

 

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.