2009年5月27日水曜日

Android - リストからの選択 -

android.util.Log クラスは、Android のシステムログに
メッセージを出力するメソッドを持っている

・Log.e():エラー
・Log.w():警告
・Log.i():情報
・Log.d():デバッグ
・Log.v():冗長

Eclipse でこのログをみるには
[Window] -> [Show View] -> [Other...] -> [Android] -> [LogCat]




More...

Sudoku/res/values/strings.xml
  1. <!--xml version="1.0" encoding="utf-8"?-->  
  2. <resources>  
  3.  <string name="app_name">Sudoku</string>  
  4.  <string name="main_title">Android Sudoku</string>  
  5.  <string name="continue_label">Continue</string>  
  6.  <string name="new_game_label">New Game</string>  
  7.  <string name="about_label">About</string>  
  8.  <string name="about_title">About Android Sudoku</string>  
  9.  <string name="about_text">\  
  10.  Sudoku is a logic-based number placement puzzle.  
  11.  Starting with a partially completed 9x9 grid, the  
  12.  objective is to fill the grid so that each row,  
  13.  each column, and each of the 3x3 boxes   
  14.  (also called <i>blocks</i>) contains the digits  
  15.  1 to 9 exactly once.  
  16.  </string>  
  17.  <string name="exit_label">Exit</string>  
  18.  <string name="settings_label">Settings...</string>  
  19.  <string name="settings_title">Sudoku settings</string>  
  20.  <string name="settings_shortcut">s</string>  
  21.  <string name="music_title">Music</string>  
  22.  <string name="music_summary">Play background music</string>  
  23.  <string name="hints_title">Hints</string>  
  24.  <string name="hints_summary">Show hints during play</string>  
  25.  <string name="new_game_title">Difficulty</string>  
  26.  <string name="easy_label">Easy</string>  
  27.  <string name="medium_label">Medium</string>  
  28.  <string name="hard_label">Hard</string>  
  29. </resources>  


リストの形式を記述

Sudoku/res/values/arrays.xml
  1. <!--xml version="1.0" encoding="utf-8"?-->  
  2. <resources>  
  3.  <array name="difficulty">  
  4.   <item>@string/easy_label</item>  
  5.   <item>@string/medium_label</item>  
  6.   <item>@string/hard_label</item>  
  7.  </array>  
  8. </resources>  


Sudoku/src/org/example/sudoku/Sudoku.java
  1. package org.example.sudoku;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.content.Intent;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.view.Menu;  
  9. import android.view.MenuInflater;  
  10. import android.view.MenuItem;  
  11. import android.app.AlertDialog;  
  12. import android.content.DialogInterface;  
  13. import android.util.Log;  
  14.   
  15. public class Sudoku extends Activity implements OnClickListener{  
  16.     /** Called when the activity is first created. */  
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.main);  
  21.   
  22.         View continueButton = this.findViewById(R.id.continue_button);  
  23.         continueButton.setOnClickListener(this);  
  24.         View newButton = this.findViewById(R.id.new_button);  
  25.         newButton.setOnClickListener(this);  
  26.         View aboutButton = this.findViewById(R.id.about_button);  
  27.         aboutButton.setOnClickListener(this);  
  28.         View exitButton = this.findViewById(R.id.exit_button);  
  29.         exitButton.setOnClickListener(this);  
  30.     }  
  31.       
  32.     public void onClick(View v) {  
  33.      switch(v.getId()){  
  34.      case R.id.about_button:  
  35.       Intent i = new Intent(this, About.class);  
  36.       startActivity(i);  
  37.       break;  
  38.      case R.id.new_button:  
  39.       openNewGameDialog();  
  40.       break;  
  41.      case R.id.exit_button:  
  42.       finish();  
  43.       break;  
  44.      }  
  45.     }  
  46.   
  47.     @Override  
  48.     public boolean onCreateOptionsMenu(Menu menu){  
  49.      super.onCreateOptionsMenu(menu);  
  50.      MenuInflater inflater = getMenuInflater();  
  51.      inflater.inflate(R.menu.menu, menu);  
  52.      return true;  
  53.     }  
  54.       
  55.     @Override  
  56.     public boolean onOptionsItemSelected(MenuItem item){  
  57.      switch (item.getItemId()){  
  58.      case R.id.settings:  
  59.       startActivity(new Intent(this, Settings.class));  
  60.       return true;  
  61.      }  
  62.      return false;  
  63.     }  
  64.       
  65.     private void openNewGameDialog() {  
  66.      new AlertDialog.Builder(this)  
  67.       .setTitle(R.string.new_game_title)  
  68.       .setItems(R.array.difficulty,   
  69.       new DialogInterface.OnClickListener() {  
  70.        public void onClick(DialogInterface dialoginterface, int i) {  
  71.         startGame(i);  
  72.        }  
  73.       })  
  74.       .show();  
  75.     }  
  76.       
  77.     private static final String TAG = "Sudoku";  
  78.       
  79.     private void startGame(int i) {  
  80.      Log.d(TAG, "clicked on " + i);  
  81.      // start game here  
  82.     }  
  83. }  


Exit ボタンを押したとき、Activity を破棄するために
finish()
を呼び出す



New Game をクリックすると難易度の選択ポップアップが表示される



0 件のコメント:

コメントを投稿