質問編集履歴
1
コード追加
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
Android検索画面の表示
|
1
|
+
Android検索画面の表示がなかなかうまくいかない
|
test
CHANGED
@@ -1,7 +1,271 @@
|
|
1
|
+
今、Androidの学習をしており,
|
2
|
+
|
1
|
-
|
3
|
+
「ほんきで学ぶAndroidアプリ開発入門」という本のChapter26のメモ帳アプリを作るもので、検索バーをActionBarに追加で表示させたいのですが、中々上手く行かず糸口がつかめなかったので質問しました。
|
2
|
-
|
4
|
+
|
3
|
-
検索バーを表示させるだけで良いので例として
|
5
|
+
検索バーを表示させるだけで良いので例として下記のコードにどのように追加すれば実装できるのかの一例をどなたか示していただけないでしょうか。
|
4
6
|
|
5
7
|
|
6
8
|
|
7
9
|
開発環境はAndroidStudioです
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
```Java
|
14
|
+
|
15
|
+
package com.yokmama.learn10.chapter06.lesson26;
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
import android.app.Activity;
|
20
|
+
|
21
|
+
import android.app.SearchManager;
|
22
|
+
|
23
|
+
import android.content.ActivityNotFoundException;
|
24
|
+
|
25
|
+
import android.content.Intent;
|
26
|
+
|
27
|
+
import android.net.Uri;
|
28
|
+
|
29
|
+
import android.os.Bundle;
|
30
|
+
|
31
|
+
import android.text.TextUtils;
|
32
|
+
|
33
|
+
import android.view.Menu;
|
34
|
+
|
35
|
+
import android.view.MenuItem;
|
36
|
+
|
37
|
+
import android.view.WindowManager;
|
38
|
+
|
39
|
+
import android.widget.EditText;
|
40
|
+
|
41
|
+
import android.widget.Toast;
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
public class MainActivity extends Activity {
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
private EditText mEditText;
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
@Override
|
54
|
+
|
55
|
+
protected void onCreate(Bundle savedInstanceState) {
|
56
|
+
|
57
|
+
super.onCreate(savedInstanceState);
|
58
|
+
|
59
|
+
setContentView(R.layout.activity_main);
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
mEditText = (EditText) findViewById(R.id.edit);
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
// キーボードを表示
|
68
|
+
|
69
|
+
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
@Override
|
76
|
+
|
77
|
+
public boolean onCreateOptionsMenu(Menu menu) {
|
78
|
+
|
79
|
+
//メニューを生成
|
80
|
+
|
81
|
+
getMenuInflater().inflate(R.menu.menu_main, menu);
|
82
|
+
|
83
|
+
return super.onCreateOptionsMenu(menu);
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
@Override
|
90
|
+
|
91
|
+
public boolean onOptionsItemSelected(MenuItem item) {
|
92
|
+
|
93
|
+
//入力されているテキストを取得
|
94
|
+
|
95
|
+
String keyword = mEditText.getText().toString();
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
//連携処理を実施
|
100
|
+
|
101
|
+
int itemId = item.getItemId();
|
102
|
+
|
103
|
+
try {
|
104
|
+
|
105
|
+
if (itemId == R.id.action_send) {
|
106
|
+
|
107
|
+
//テキスト連携
|
108
|
+
|
109
|
+
if (!checkEmpty(keyword)) {
|
110
|
+
|
111
|
+
Intent intent = new Intent(Intent.ACTION_SEND);
|
112
|
+
|
113
|
+
intent.setType("text/plain");
|
114
|
+
|
115
|
+
intent.putExtra(Intent.EXTRA_TEXT, keyword);
|
116
|
+
|
117
|
+
startActivity(intent);
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
} else if (itemId == R.id.action_google) {
|
122
|
+
|
123
|
+
//ウェブ検索
|
124
|
+
|
125
|
+
if (!checkEmpty(keyword)) {
|
126
|
+
|
127
|
+
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
|
128
|
+
|
129
|
+
intent.putExtra(SearchManager.QUERY, keyword);
|
130
|
+
|
131
|
+
startActivity(intent);
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
} else if (itemId == R.id.action_store) {
|
136
|
+
|
137
|
+
// Playストア検索
|
138
|
+
|
139
|
+
if (!checkEmpty(keyword)) {
|
140
|
+
|
141
|
+
Uri uri = Uri.parse("https://play.google.com/store/search")
|
142
|
+
|
143
|
+
.buildUpon().appendQueryParameter("q", keyword).build();
|
144
|
+
|
145
|
+
startActivity(new Intent(Intent.ACTION_VIEW, uri));
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
} catch (ActivityNotFoundException e) {
|
152
|
+
|
153
|
+
//開こうとしているアプリが見つからないエラー
|
154
|
+
|
155
|
+
Toast.makeText(this, getString(R.string.lb_activity_not_found), Toast.LENGTH_SHORT)
|
156
|
+
|
157
|
+
.show();
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
return super.onOptionsItemSelected(item);
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
/**
|
168
|
+
|
169
|
+
* キーワードが空(null、または空文字列)かどうかを確認.
|
170
|
+
|
171
|
+
*
|
172
|
+
|
173
|
+
* @param keyword キーワード
|
174
|
+
|
175
|
+
* @return キーワードが空なら true
|
176
|
+
|
177
|
+
*/
|
178
|
+
|
179
|
+
private boolean checkEmpty(String keyword) {
|
180
|
+
|
181
|
+
if (TextUtils.isEmpty(keyword)) {
|
182
|
+
|
183
|
+
Toast.makeText(this, getString(R.string.lb_please_input_keyword), Toast.LENGTH_SHORT)
|
184
|
+
|
185
|
+
.show();
|
186
|
+
|
187
|
+
return true;
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
return false;
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
```
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
```xml
|
204
|
+
|
205
|
+
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
206
|
+
|
207
|
+
xmlns:tools="http://schemas.android.com/tools"
|
208
|
+
|
209
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
210
|
+
|
211
|
+
tools:context=".MainActivity">
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
<!--共有-->
|
216
|
+
|
217
|
+
<item
|
218
|
+
|
219
|
+
android:id="@+id/action_send"
|
220
|
+
|
221
|
+
android:icon="@android:drawable/ic_menu_search"
|
222
|
+
|
223
|
+
android:orderInCategory="100"
|
224
|
+
|
225
|
+
android:title="@string/lb_menu_send"/>
|
226
|
+
|
227
|
+
<!--android:showAsAction="never" -->
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
<!--ウェブで検索-->
|
232
|
+
|
233
|
+
<item
|
234
|
+
|
235
|
+
android:id="@+id/action_google"
|
236
|
+
|
237
|
+
android:icon="@android:drawable/ic_menu_search"
|
238
|
+
|
239
|
+
android:orderInCategory="100"
|
240
|
+
|
241
|
+
android:title="@string/lb_menu_web"/>
|
242
|
+
|
243
|
+
<!--android:showAsAction="never"-->
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
<!--Playストアで検索-->
|
248
|
+
|
249
|
+
<item
|
250
|
+
|
251
|
+
android:id="@+id/action_store"
|
252
|
+
|
253
|
+
android:icon="@android:drawable/ic_menu_search"
|
254
|
+
|
255
|
+
android:orderInCategory="100"
|
256
|
+
|
257
|
+
android:title="@string/lb_menu_play_store"/>
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
</menu>
|
270
|
+
|
271
|
+
```
|