2012年7月21日土曜日

複数ボタンの処理:view button



import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class ButtonActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

 // ボタンイベント
 public void SetButtonOnClick(View v) {

  switch (v.getId()) {
  case R.id.button1:
   Log.i("buttonTest", "ボタン1が押された");
   break;
  case R.id.button2:
   Log.i("buttonTest", "ボタン2が押された");
   break;
  case R.id.button3:
   Log.i("buttonTest", "ボタン3が押された");
   break;

  }

 }
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="SetButtonOnClick"
        android:text="Button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="SetButtonOnClick"
        android:text="Button2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="SetButtonOnClick"
        android:text="Button3" />

</LinearLayout>

関連記事

0 件のコメント:

コメントを投稿