日の終わりに今日のまとめ

開発や仕事で使った技術をまとめてます。

[android]テキストボックスに入力時、ソフトウェアキーボードのアイコン変更と検索実行

androidでテキストボックスを選択し入力を行う際、ソフトウェアキーボードが表示される。

その右下に表示されている「完了」ボタンを他のボタンに変更することができる。

今回は検索によく使われる虫眼鏡を例に書きます。

 

以下のように記述することで、虫眼鏡のボタンに切り替わります。

1
2
3
4
<EditText android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionSearch"/>
 

 

java側のactivityに以下のように書くことで虫眼鏡のボタンを押したイベントを取得し検索処理が実行できます。

1
2
3
4
5
6
7
8
 
editText1.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (event == null || event.getAction() == KeyEvent.ACTION_UP) {
// ここに検索処理を記述
}
return true;
}
});