반응형

Button

 

사용자가 버튼을 누를 때 발생하는 동작을 전달하는 텍스트 또는 아이콘 (또는 텍스트와 아이콘 모두)으로 구성

 

 

 

Button XML

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_text"
    ... />

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/button_icon"
    android:contentDescription="@string/button_icon_desc"
    ... />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_text"
    android:drawableLeft="@drawable/button_icon"
    ... />

 

 

 

 

 

Button Event

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/button_send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="sendMessage" />

 

 

1. android:onClick ( recommended )

 

/** Called when the user touches the button */
public void sendMessage(View view) {
    // Do something in response to button click
}

 

2. OnClickListener

Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // Do something in response to button click
    }
});

 

 

developer.android.com/guide/topics/ui/controls/button#java

 

Buttons  |  Android 개발자  |  Android Developers

A button consists of text or an icon (or both text and an icon) that communicates what action occurs when the user touches it. Depending on whether you want a button with text, an icon, or both, you can create the button in your layout in three ways: With

developer.android.com

 

 

 

 

(번외)

LongClick Event

 

버튼을 길게 눌렀을 때, Event 발생

 

        Button button = (Button) findViewById(R.id.button_send);
        button.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
            	// Do something in response to button click
                
                return true;
                // 이벤트 처리를 못했을 경우 false return
            }
        });
728x90
반응형

+ Recent posts