- 버튼 이벤트는 어떤 버튼이 클릭되었는지 Toast Message를 띄우는 것만 되어 있습니다.
DevilsCampAndroidActivity.java ¶
package com.zp.cau; import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class DevilsCampAndroidActivity extends Activity implements OnClickListener { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ImageView imageView1 = new ImageView(this); imageView1.setImageResource(R.drawable.images); Button button1 = (Button)findViewById(R.id.button1); Button button2 = (Button)findViewById(R.id.button2); Button button3 = (Button)findViewById(R.id.button3); button1.setOnClickListener(this); button2.setOnClickListener(this); button3.setOnClickListener(this); } @Override public void onClick(View v){ String btn = ""; if(v.getId() == R.id.button1) btn = "button1"; if(v.getId() == R.id.button2) btn = "button2"; if(v.getId() == R.id.button3) btn = "button3"; Toast.makeText(this, btn+" clicked", Toast.LENGTH_SHORT).show(); } public boolean onTouchEvent(MotionEvent event){ super.onTouchEvent(event); if(event.getAction() == MotionEvent.ACTION_DOWN){ return true; } return false; } }
Main.xml ¶
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello"></TextView> <ImageView android:src="@drawable/images" android:layout_width="wrap_content" android:id="@+id/imageView1" android:layout_height="wrap_content"></ImageView> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/inputText"></EditText> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:id="@+id/linearLayout1" android:layout_height="wrap_content"> <Button android:id="@+id/button1" android:text="button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:id="@+id/button2" android:text="button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:id="@+id/button3" android:text="button3" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout> </LinearLayout>