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