ステップ 9
onCreateOptionsMenu メソッドのコーディングに入ります。res/values/strings.xmlを以下のように変更します。
1 2 3 4 5 6 | <? xml version = "1.0" encoding = "utf-8" ?> < resources > < string name = "app_name" >Notepad v1</ string > < string name = "no_notes" >No Notes Yet</ string > < string name = "menu_insert" >Add Item</ string > </ resources > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | /* * Copyright (C) 2008 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.demo.notepad1; import android.app.ListActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class Notepadv1 extends ListActivity { public static final int INSERT_ID = Menu.FIRST; private int mNoteNumber = 1 ; private NotesDbAdapter mDbHelper; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.notepad_list); mDbHelper = new NotesDbAdapter( this ); mDbHelper.open(); fillData(); } @Override public boolean onCreateOptionsMenu(Menu menu) { boolean result = super .onCreateOptionsMenu(menu); menu.add( 0 , INSERT_ID, 0 , R.string.menu_insert); return result; } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub return super .onOptionsItemSelected(item); } } |
ステップ 10
onOptionsItemSelected メソッドのコーディングに入ります。Notepadv1.javaを開き以下のように変更します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | /* * Copyright (C) 2008 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.demo.notepad1; import android.app.ListActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class Notepadv1 extends ListActivity { public static final int INSERT_ID = Menu.FIRST; private int mNoteNumber = 1 ; private NotesDbAdapter mDbHelper; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.notepad_list); mDbHelper = new NotesDbAdapter( this ); mDbHelper.open(); fillData(); } @Override public boolean onCreateOptionsMenu(Menu menu) { boolean result = super .onCreateOptionsMenu(menu); menu.add( 0 , INSERT_ID, 0 , R.string.menu_insert); return result; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case INSERT_ID: createNote(); return true ; } return super .onOptionsItemSelected(item); } } |
そして、それ以外のメニューであれば継承元のonOptionsItemSelectedを実行しています。
関連記事
0 件のコメント:
コメントを投稿