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");
}
'Android(Java)' 카테고리의 다른 글
[Java] Thread 상속 클래스 타이머 예제 (0) | 2024.01.12 |
---|---|
[Java] Thread 기본 (0) | 2024.01.12 |
[Java] 암시적 Intent 공유하기 (1) | 2024.01.11 |
[Java] 암시적 Intent 기본 사용방법 (0) | 2024.01.11 |
[Java] 사용자 Activity 활성화 명시적 Intent 사용 방법 (1) | 2024.01.11 |