*

【android】ハードウェアキー入力を検出する

公開日: : 最終更新日:2012/05/07 android

Activityを継承したクラスで下記を記述します。

    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	if(keyCode == KeyEvent.KEYCODE_BACK){
    		finish();
    		return false;
    	}else{
    		return super.onKeyDown(keyCode, event);
    	}
    }

ダイアログを表示している場合は、Activityのイベントとしてハードウェアキーの入力が検出できません。
ダイアログにキーイベントリスナを登録してやります。

		final AlertDialog.Builder builder = new AlertDialog.Builder(act)
		.setTitle( act.getResources().getString(R.string.bookmark_editdialog_title) )
		.setIcon( R.drawable.icon_01 )
		.setOnKeyListener( new DialogInterface.OnKeyListener() {

		    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                        //ハードウェアキーの入力イベントを処理
		    	if(keyCode == KeyEvent.KEYCODE_BACK){
		    		finish();
		    		return true;
		    	}else{
		    		return false;
		    	}
		    }
		})

関連記事

no image

【android】 年月だけのDatePicker

final DatePicker datePicker = new DatePicker(sel

記事を読む

no image

【android】アプリ内課金を実装する

アプリ内課金のサンプルコードを解析します。 参考URL 公式API Android

記事を読む

no image

【android】任意のスレッドで処理を行う

非UIスレッドでUIを操作したい場合に、任意の処理をUIスレッド上で実行する。 目次

記事を読む

no image

【android】ネットワークの接続状況を確認する

コードから接続状況を確認。 ConnectivityManager co

記事を読む

no image

[android] JSONのパースにかかる時間

リストデータなんかを保存したいというのはよくある要件だと思います。 DBは面倒だしカラム毎に集計す

記事を読む

no image

【android】バイブレーションを使う

必要なファイル MyApp.manifest MyApp.java MyApp.m

記事を読む

no image

【android】サービスの実装

ダウンロードなどActivityに依存したくない大きなバックグラウンド処理や常駐プロセスを作りたい場

記事を読む

no image

【android】webviewでアプリ内にwebページを読み込む

webviewを使ってandroidアプリ内にwebページを読み込む定型文です。 //vie

記事を読む

no image

アプリ間連携 Intentfiler

ブラウザの共有からURLを受け取る。 Manifest.xmlのURL受け取り先のactivity

記事を読む

no image

【android】DrawableとBitmap、リソースの相互変換 

Resource → Bitmap Bitmap bm = BitmapFactory.dec

記事を読む

Message

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

no image
知らないと損をする6つのライセンスまとめ

オープンソースやフリーウェア、フリー素材などが巷に溢れ、それらを利用す

no image
ガリレオ:ニュースブラウザをリリースしました。

概要 ガリレオはニュースを読んだり、検索する機能に特化したブラウザア

no image
【android】Activityとプロセスのライフサイクル

またもやライフサイクル周りでハマったのですが、Androidのライフサ

→もっと見る

PAGE TOP ↑