OnClick에 대해 좋은 블로그 글 (https://nanstrong.tistory.com/274) 이 있어서 공부하고 요약한 내용을 정리해본다.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn1;
private Button btn2;
private View view1;
private View view2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.btn1);
btn2 = (Button)findViewById(R.id.btn2);
view1 = findViewById(R.id.view1);
view2 = findViewById(R.id.view2);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn1 :
view1.setVisibility(View.VISIBLE);
view2.setVisibility(View.GONE);
break;
case R.id.btn2 :
view1.setVisibility(View.GONE);
view2.setVisibility(View.VISIBLE);
break;
}
}
}
//출처: https://nanstrong.tistory.com/274
package test.com.exam;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity{
private Button btn1;
private Button btn2;
private View view1;
private View view2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.btn1);
btn2 = (Button)findViewById(R.id.btn2);
view1 = findViewById(R.id.view1);
view2 = findViewById(R.id.view2);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
view1.setVisibility(View.VISIBLE);
view2.setVisibility(View.GONE);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
view1.setVisibility(View.GONE);
view2.setVisibility(View.VISIBLE);
}
});
}
}
// 출처: https://nanstrong.tistory.com/274 [D의 개발공간(일하면서 공부하면서 끄적끄적)]
package test.com.exam;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity{
private Button btn1;
private Button btn2;
private View view1;
private View view2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.btn1);
btn2 = (Button)findViewById(R.id.btn2);
view1 = findViewById(R.id.view1);
view2 = findViewById(R.id.view2);
btn1.setOnClickListener(btn1Listener);
btn2.setOnClickListener(btn2Listener);
}
View.OnClickListener btn1Listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
view1.setVisibility(View.VISIBLE);
view2.setVisibility(View.GONE);
}
};
View.OnClickListener btn2Listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
view1.setVisibility(View.GONE);
view2.setVisibility(View.VISIBLE);
}
};
// 출처: https://nanstrong.tistory.com/274 [D의 개발공간(일하면서 공부하면서 끄적끄적)]
public void onClick1(View v) {
view1.setVisibility(View.VISIBLE);
view2.setVisibility(View.GONE);
}
public void onClick2(View v) {
view1.setVisibility(View.GONE);
view2.setVisibility(View.VISIBLE);
}
}
// 출처: https://nanstrong.tistory.com/274 [D의 개발공간(일하면서 공부하면서 끄적끄적)]