今、Androidの学習をしており,
「ほんきで学ぶAndroidアプリ開発入門」という本のChapter26のメモ帳アプリを作るもので、検索バーをActionBarに追加で表示させたいのですが、中々上手く行かず糸口がつかめなかったので質問しました。
検索バーを表示させるだけで良いので例として下記のコードにどのように追加すれば実装できるのかの一例をどなたか示していただけないでしょうか。
開発環境はAndroidStudioです
Java
1package com.yokmama.learn10.chapter06.lesson26; 2 3import android.app.Activity; 4import android.app.SearchManager; 5import android.content.ActivityNotFoundException; 6import android.content.Intent; 7import android.net.Uri; 8import android.os.Bundle; 9import android.text.TextUtils; 10import android.view.Menu; 11import android.view.MenuItem; 12import android.view.WindowManager; 13import android.widget.EditText; 14import android.widget.Toast; 15 16public class MainActivity extends Activity { 17 18 private EditText mEditText; 19 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_main); 24 25 mEditText = (EditText) findViewById(R.id.edit); 26 27 // キーボードを表示 28 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 29 } 30 31 @Override 32 public boolean onCreateOptionsMenu(Menu menu) { 33 //メニューを生成 34 getMenuInflater().inflate(R.menu.menu_main, menu); 35 return super.onCreateOptionsMenu(menu); 36 } 37 38 @Override 39 public boolean onOptionsItemSelected(MenuItem item) { 40 //入力されているテキストを取得 41 String keyword = mEditText.getText().toString(); 42 43 //連携処理を実施 44 int itemId = item.getItemId(); 45 try { 46 if (itemId == R.id.action_send) { 47 //テキスト連携 48 if (!checkEmpty(keyword)) { 49 Intent intent = new Intent(Intent.ACTION_SEND); 50 intent.setType("text/plain"); 51 intent.putExtra(Intent.EXTRA_TEXT, keyword); 52 startActivity(intent); 53 } 54 } else if (itemId == R.id.action_google) { 55 //ウェブ検索 56 if (!checkEmpty(keyword)) { 57 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH); 58 intent.putExtra(SearchManager.QUERY, keyword); 59 startActivity(intent); 60 } 61 } else if (itemId == R.id.action_store) { 62 // Playストア検索 63 if (!checkEmpty(keyword)) { 64 Uri uri = Uri.parse("https://play.google.com/store/search") 65 .buildUpon().appendQueryParameter("q", keyword).build(); 66 startActivity(new Intent(Intent.ACTION_VIEW, uri)); 67 } 68 } 69 } catch (ActivityNotFoundException e) { 70 //開こうとしているアプリが見つからないエラー 71 Toast.makeText(this, getString(R.string.lb_activity_not_found), Toast.LENGTH_SHORT) 72 .show(); 73 } 74 return super.onOptionsItemSelected(item); 75 } 76 77 /** 78 * キーワードが空(null、または空文字列)かどうかを確認. 79 * 80 * @param keyword キーワード 81 * @return キーワードが空なら true 82 */ 83 private boolean checkEmpty(String keyword) { 84 if (TextUtils.isEmpty(keyword)) { 85 Toast.makeText(this, getString(R.string.lb_please_input_keyword), Toast.LENGTH_SHORT) 86 .show(); 87 return true; 88 } 89 return false; 90 } 91} 92
xml
1<menu xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 tools:context=".MainActivity"> 5 6 <!--共有--> 7 <item 8 android:id="@+id/action_send" 9 android:icon="@android:drawable/ic_menu_search" 10 android:orderInCategory="100" 11 android:title="@string/lb_menu_send"/> 12 <!--android:showAsAction="never" --> 13 14 <!--ウェブで検索--> 15 <item 16 android:id="@+id/action_google" 17 android:icon="@android:drawable/ic_menu_search" 18 android:orderInCategory="100" 19 android:title="@string/lb_menu_web"/> 20 <!--android:showAsAction="never"--> 21 22 <!--Playストアで検索--> 23 <item 24 android:id="@+id/action_store" 25 android:icon="@android:drawable/ic_menu_search" 26 android:orderInCategory="100" 27 android:title="@string/lb_menu_play_store"/> 28 29 30 31 32 33</menu>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。