Android-event-handling

提供:Dev Guides
移動先:案内検索

Android-イベント処理

'_*イベントは、アプリケーションのインタラクティブなコンポーネントとのユーザーの相互作用に関するデータを収集するための便利な方法です。*ボタンの押下や画面のタッチなど。 Androidフレームワークは、イベントキューを先入れ先出し(FIFO)ベースとして維持します。 プログラムでこれらのイベントをキャプチャし、要件に従って適切なアクションを実行できます。_

Androidイベント管理に関連する次の3つの概念があります-

  • イベントリスナー-イベントリスナーは、単一のコールバックメソッドを含むViewクラスのインターフェイスです。 これらのメソッドは、リスナーが登録されているビューがUI内のアイテムとのユーザーの対話によってトリガーされると、Androidフレームワークによって呼び出されます。
  • イベントリスナーの登録-イベント登録は、イベントハンドラーがイベントリスナーに登録され、イベントリスナーがイベントを発生させたときにハンドラーが呼び出されるプロセスです。
  • イベントハンドラ-イベントが発生し、イベントのイベントリスナーを登録すると、イベントリスナーはイベントハンドラを呼び出します。これは、実際にイベントを処理するメソッドです。

イベントリスナーとイベントハンドラー

Event Handler Event Listener & Description
onClick()

OnClickListener()

これは、ユーザーがボタン、テキスト、画像などのウィジェットをクリック、タッチ、またはフォーカスしたときに呼び出されます。 onClick()イベントハンドラを使用して、このようなイベントを処理します。

onLongClick()

OnLongClickListener()

これは、ユーザーがボタン、テキスト、画像などのウィジェットをクリック、タッチ、またはフォーカスしたときに呼び出されます。 1秒以上。 onLongClick()イベントハンドラを使用して、このようなイベントを処理します。

onFocusChange()

OnFocusChangeListener()

これは、ウィジェットがフォーカスを失うときに呼び出されます。 ユーザーはビュー項目から離れます。 onFocusChange()イベントハンドラを使用して、このようなイベントを処理します。

onKey()

OnFocusChangeListener()

これは、ユーザーがアイテムに焦点を合わせ、デバイスのハードウェアキーを押すか放したときに呼び出されます。 onKey()イベントハンドラを使用して、このようなイベントを処理します。

onTouch()

OnTouchListener()

これは、ユーザーがキーを押したとき、キーを離したとき、または画面上の任意の移動ジェスチャーで呼び出されます。 onTouch()イベントハンドラを使用して、このようなイベントを処理します。

onMenuItemClick()

OnMenuItemClickListener()

ユーザーがメニュー項目を選択すると呼び出されます。 onMenuItemClick()イベントハンドラを使用して、このようなイベントを処理します。

onCreateContextMenu()

onCreateContextMenuItemListener()

これは、コンテキストメニューが構築されているときに呼び出されます(持続的な「ロングクリックの結果」)

OnHoverListener、OnDragListenerなど、アプリケーションに必要な View クラスの一部として、さらに多くのイベントリスナーを使用できます。 そのため、洗練されたアプリを開発する場合に備えて、Androidアプリケーション開発の公式ドキュメントを参照することをお勧めします。

イベントリスナーの登録

イベント登録は、イベントハンドラーがイベントリスナーに登録され、イベントリスナーがイベントを発生させたときにハンドラーが呼び出されるプロセスです。 イベントリスナーをイベントに登録するにはいくつかのトリッキーな方法がありますが、ここでは上位3つの方法のみをリストします。そのうち3つは状況に応じて使用できます。

  • 匿名の内部クラスを使用する
  • アクティビティクラスは、Listenerインターフェイスを実装します。
  • レイアウトファイルactivity_main.xmlを使用して、イベントハンドラーを直接指定します。

以下のセクションでは、3つのシナリオすべての詳細な例を示します-

タッチモード

ユーザーは、ハードウェアキーまたはボタンを使用するか、画面に触れることでデバイスと対話できます。画面に触れると、デバイスがタッチモードになります。 ユーザーは、画面上の仮想ボタンや画像などに触れることで操作できます。ViewクラスのisInTouchMode()メソッドを呼び出すと、デバイスがタッチモードになっているかどうかを確認できます。

フォーカス

ビューまたはウィジェットは通常、強調表示されているか、フォーカスが合っているときに点滅するカーソルを表示します。 これは、ユーザーからの入力を受け入れる準備ができていることを示しています。

  • * isFocusable()*-trueまたはfalseを返します
  • * isFocusableInTouchMode()*-ビューがタッチモードでフォーカス可能かどうかを確認します。 (ハードウェアキーを使用するとビューがフォーカス可能になる場合がありますが、デバイスがタッチモードの場合はフォーカスできません)
android:foucsUp="@=id/button_l"

onTouchEvent()

public boolean onTouchEvent(motionEvent event){
   switch(event.getAction()){
      case TOUCH_DOWN:
      Toast.makeText(this,"you have clicked down Touch button",Toast.LENTH_LONG).show();
      break();

      case TOUCH_UP:
      Toast.makeText(this,"you have clicked up touch button",Toast.LENTH_LONG).show();
      break;

      case TOUCH_MOVE:
      Toast.makeText(this,"you have clicked move touch button"Toast.LENTH_LONG).show();
      break;
   }
   return super.onTouchEvent(event) ;
}

イベント処理の例

匿名内部クラスを使用したイベントリスナーの登録

ここでは、リスナーの匿名実装を作成します。各クラスが単一のコントロールにのみ適用され、イベントハンドラーに引数を渡す利点がある場合に役立ちます。 このアプローチでは、イベントハンドラーメソッドはActivityのプライベートデータにアクセスできます。 Activityを呼び出すための参照は必要ありません。

ただし、ハンドラーを複数のコントロールに適用した場合、ハンドラーのコードをカットアンドペーストする必要があり、ハンドラーのコードが長い場合、コードの保守が難しくなります。

次に、個別のListenerクラスを使用してクリックイベントを登録およびキャプチャする方法を示す簡単な手順を示します。 同様に、他の必要なイベントタイプのリスナーを実装できます。

Step Description
1 You will use Android studio IDE to create an Android application and name it as myapplication under a package com.example.myapplication as explained in the Hello World Example chapter.
2 Modify src/MainActivity.java file to add click event listeners and handlers for the two buttons defined.
3 Modify the detault content of res/layout/activity_main.xml file to include Android UI controls.
4 No need to declare default string constants.Android studio takes care default constants.
5 Run the application to launch Android emulator and verify the result of the changes done in the aplication.

以下は、変更されたメインアクティビティファイル src/com.example.myapplication/MainActivity.java の内容です。 このファイルには、基本的な各ライフサイクルメソッドを含めることができます。

package com.example.myapplication;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {
   private ProgressDialog progress;
   Button b1,b2;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      progress = new ProgressDialog(this);

      b1=(Button)findViewById(R.id.button);
      b2=(Button)findViewById(R.id.button2);
      b1.setOnClickListener(new View.OnClickListener() {

         @Override
         public void onClick(View v) {
            TextView txtView = (TextView) findViewById(R.id.textView);
            txtView.setTextSize(25);
         }
      });

      b2.setOnClickListener(new View.OnClickListener() {

         @Override
         public void onClick(View v) {
            TextView txtView = (TextView) findViewById(R.id.textView);
            txtView.setTextSize(55);
         }
      });
   }
}

以下は res/layout/activity_main.xml ファイルの内容です-

'_ここにabcはfinddevguidesロゴについて示しています_

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity">

   <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Event Handling "
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:textSize="30dp"/>

   <TextView
      android:id="@+id/textView2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials point "
      android:textColor="#ff87ff09"
      android:textSize="30dp"
      android:layout_above="@+id/imageButton"
      android:layout_centerHorizontal="true"
      android:layout_marginBottom="40dp"/>

   <ImageButton
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageButton"
      android:src="@drawable/abc"
      android:layout_centerVertical="true"
      android:layout_centerHorizontal="true"/>

   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Small font"
      android:id="@+id/button"
      android:layout_below="@+id/imageButton"
      android:layout_centerHorizontal="true"/>

   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Large Font"
      android:id="@+id/button2"
      android:layout_below="@+id/button"
      android:layout_alignRight="@+id/button"
      android:layout_alignEnd="@+id/button"/>

   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Hello World!"
      android:id="@+id/textView"
      android:layout_below="@+id/button2"
      android:layout_centerHorizontal="true"
      android:textSize="25dp"/>

</RelativeLayout>

以下は、2つの新しい定数を定義する res/values/strings.xml の内容です-

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">myapplication</string>
</resources>

以下は、 AndroidManifest.xml のデフォルトのコンテンツです-

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.myapplication" >

   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >

      <activity
         android:name="com.example.myapplication.MainActivity"
         android:label="@string/app_name" >

         <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
         </intent-filter>

      </activity>

   </application>
</manifest>
*myapplication* アプリケーションを実行してみましょう。 環境設定中に *AVD* を作成したと思います。 Android Studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[image:/android/eclipse_run.png[Eclipse Run Icon]アイコンの実行]アイコンをクリックします。 Android StudioはAVDにアプリをインストールして起動し、セットアップとアプリケーションで問題がなければ、次のエミュレータウィンドウが表示されます-

Androidイベント処理

ここで、2つのボタンを1つずつクリックしてみると、登録されたクリックイベントハンドラーメソッドが各クリックイベントに対して呼び出されているため、 Hello World テキストのフォントが変更されることがわかります。

運動

さまざまなイベントタイプに対してさまざまなイベントハンドラを作成し、さまざまなイベントタイプとその処理の正確な違いを理解することをお勧めします。 メニュー、スピナー、ピッカーウィジェットに関連するイベントはほとんど異なりませんが、上記で説明したものと同じ概念に基づいています。