2009年5月27日水曜日

Android で数独 - onClick -

新しい Activity を定義して起動する。

レイアウトを設定
Sudoku/res/layout/about.xml

  1. <!--xml version="1.0" encoding="utf-8"?-->  
  2. <scrollview  
  3.  xmlns:android="http://schemas.android.com/apk/res/android"  
  4.  android:layout_width="fill_parent"  
  5.  android:layout_height="fill_parent"  
  6.  android:padding="10dip"  
  7. >  
  8. <textview  
  9.  android:id="@+id/about_content"  
  10.  android:layout_width="wrap_content"  
  11.  android:layout_height="wrap_content"  
  12.  android:text="@string/about_text"  
  13. />  
  14.   
  15. </textview  
  16. </scrollview  


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. </resources>  


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

  1. package org.example.sudoku;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. public class About extends Activity{  
  7.  @Override  
  8.  protected void onCreate(Bundle saveInstanceState) {  
  9.   super.onCreate(saveInstanceState);  
  10.   setContentView(R.layout.about);  
  11.  }  
  12. }  


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.   
  9. public class Sudoku extends Activity implements OnClickListener{  
  10.     /** Called when the activity is first created. */  
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.main);  
  15.   
  16.         View continueButton = this.findViewById(R.id.continue_button);  
  17.         continueButton.setOnClickListener(this);  
  18.         View newButton = this.findViewById(R.id.new_button);  
  19.         newButton.setOnClickListener(this);  
  20.         View aboutButton = this.findViewById(R.id.about_button);  
  21.         aboutButton.setOnClickListener(this);  
  22.         View exitButton = this.findViewById(R.id.exit_button);  
  23.         exitButton.setOnClickListener(this);  
  24.     }  
  25.       
  26.     public void onClick(View v) {  
  27.      switch(v.getId()){  
  28.      case R.id.about_button:  
  29.       Intent i = new Intent(this, About.class);  
  30.       startActivity(i);  
  31.       break;  
  32.      }  
  33.     }  
  34.       
  35. }  


Sudoku/AndroidManifest.xml

  1. <!--xml version="1.0" encoding="utf-8"?-->  
  2. <manifest  
  3.    xmlns:android="http://schemas.android.com/apk/res/android"  
  4.    package="org.example.sudoku"  
  5.    android:versionCode="1"  
  6.    android:versionName="1.0">  
  7.    <application <brbr="">      android:icon="@drawable/icon"   
  8.       android:label="@string/app_name">  
  9.       <activity  
  10.         android:name=".Sudoku"  
  11.         android:label="@string/app_name">  
  12.       <intent-filter>  
  13.         <action android:name="android.intent.action.MAIN">  
  14.         <category android:name="android.intent.category.LAUNCHER">  
  15.       </category></action></intent-filter>  
  16.         
  17.       <activity  
  18.         android:name=".About"  
  19.         android:label="@string/about_title">  
  20.         
  21.    </activity  
  22. </activity  
  23. </application>  
  24.    <uses-sdk android:minsdkversion="3">  
  25.    
  26. </uses-sdk></manifest  


About ボタンをクリックするとこんな感じ



0 件のコメント:

コメントを投稿