Android/Android Studio

이미지 버튼

Dev-Drake 2019. 4. 4. 17:42
반응형

// 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"
    >
<ImageButton
 android:id="@+id/ImgBtn"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src = "@drawable/icon"
    android:text="@string/hello"
    />
</LinearLayout>

 

// ImgButton.java

package pkg.ImgButton;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

public class ImgButton extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        ImageButton imgbtn = (ImageButton)findViewById(R.id.ImgBtn);
        imgbtn.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    Toast.makeText(ImgButton.this, "Image Button Clicked", Toast.LENGTH_SHORT).show();
   }
  });
    }
}

 

반응형