xml 

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/intent_action_button"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="1"
            android:layout_marginLeft="10dp"
            android:text="Intent"
            android:onClick="onIntentActivityButton"
            />

        <Button
            android:id="@+id/activity_layer_button"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="1"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:text="activiry"
            android:onClick="onActivityButton"
            />

        <Button
            android:id="@+id/shard_button"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="1"
            android:text="공유하기"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:onClick="onShardButton"
            />
    </LinearLayout>

 

 

방법 1.

Button intentBtn = (Button) findViewById(R.id.intent_action_button);
Button activityBtn = (Button) findViewById(R.id.activity_layer_button);
Button shardBtn = (Button) findViewById(R.id.shard_button);
intentBtn.setOnClickListener(
        new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }
  
);

 

방법 2.

Button intentBtn = (Button) findViewById(R.id.intent_action_button);
Button activityBtn = (Button) findViewById(R.id.activity_layer_button);
Button shardBtn = (Button) findViewById(R.id.shard_button);

View.OnClickListener listener = new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.intent_action_button:
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
                break;
            case R.id.activity_layer_button:
                break;
            case R.id.shard_button:
                break;
        }
    }
};

intentBtn.setOnClickListener(listener);
activityBtn.setOnClickListener(listener);
shardBtn.setOnClickListener(listener);

 

방법 3. xml android:onClick 구문의 함수 명을 일치시켜서 구현

// Intent Button
public void onIntentActivityButton(View v) {
    Log.d("Button", "onIntentActivityButton");
    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
    startActivity(intent);

// Activity Button
public void onActivityButton(View v) {
    Log.d("Button", "onIntentActivityButton");

// Shard Button
public void onShardButton(View v) {
    Log.d("Button", "onIntentActivityButton");
}

+ Recent posts