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 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
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 |
< 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 > <!--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 |
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.
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.