2009年5月27日水曜日

Android Menu action

MENU ボタンを押したときの動作を追加する

Sudoku/res/value/strings.xml



Sudoku
Android Sudoku
Continue
New Game
About
About Android Sudoku
\
Sudoku is a logic-based number placement puzzle.
Starting with a partially completed 9x9 grid, the
objective is to fill the grid so that each row,
each column, and each of the 3x3 boxes
(also called blocks) contains the digits
1 to 9 exactly once.

Exit
Settings...
Sudoku settings
s
Music
Play background music
Hints
Show hints during play




Sudoku/res/menu/menu.xml


<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/settings"
android:title="@string/settings_label"
android:alphabeticShortcut="@string/settings_shortcut"
/>
</menu>


Sudoku/src/org/example/sudoku/Sudoku.java

package org.example.sudoku;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class Sudoku extends Activity implements OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

View continueButton = this.findViewById(R.id.continue_button);
continueButton.setOnClickListener(this);
View newButton = this.findViewById(R.id.new_button);
newButton.setOnClickListener(this);
View aboutButton = this.findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
View exitButton = this.findViewById(R.id.exit_button);
exitButton.setOnClickListener(this);
}

public void onClick(View v) {
switch(v.getId()){
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.settings:
startActivity(new Intent(this, Settings.class));
return true;
}
return false;
}
}


Sudoku/res/xml/settings.xml



android:key="music"
android:title="@string/music_title"
android:summary="@string/music_summary"
android:defaultValue="true" />
android:key="hints"
android:title="@string/hints_title"
android:summary="@string/hints_summary"
android:defaultValue="true" />



Sudoku/src/org/example/sudoku/Settings.java

package org.example.sudoku;

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class Settings extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}


Sudoku/AndroidManifest.xml


xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example.sudoku"
android:versionCode="1"
android:versionName="1.0">
android:icon="@drawable/icon"
android:label="@string/app_name">
android:name=".Sudoku"
android:label="@string/app_name">





android:name=".About"
android:label="@string/about_title"
android:theme="@android:style/Theme.Dialog">

android:name=".Settings"
android:label="@string/settings_title">






MENU ボタンを押すと、Settings... が表示されるようになった



Settings... をクリックすると、設定項目が表示される




 

0 件のコメント:

コメントを投稿