==================================================
activity_main.xml
==================================================
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:id="@+id/fruit"
android:layout_height="wrap_content"
android:text="fruit"
android:textColor="#ffff00"
android:textSize="48sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
>
<Button
android:id="@+id/apple"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="apple"
/>
<Button
android:id="@+id/orange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="orange"
/>
</LinearLayout>
</LinearLayout>
-----------------------------------------------------------
===========================================
MainActivity.java
===========================================
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnApple=(Button)findViewById(R.id.apple);
btnApple.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
TextView textfruit = (TextView)findViewById(R.id.fruit);
textfruit.setText("Apple");
}
});
Button btnOrange=(Button)findViewById(R.id.orange);
btnOrange.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
TextView textfruit = (TextView)findViewById(R.id.fruit);
textfruit.setText("Orange");
}
});
}
}
==============================================================
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnApple=(Button)findViewById(R.id.apple);
btnApple.setOnClickListener(this);
Button btnOrange=(Button)findViewById(R.id.orange);
btnOrange.setOnClickListener(this);
}
public void onClick(View v){
TextView textFruit = (TextView)findViewById(R.id.fruit);
switch (v.getId()){
case R.id.apple:
textFruit.setText("Apple");
break;
case R.id.orange:
textFruit.setText("Orange");
break;
}
}
}
=======================================
public class MainActivity extends AppCompatActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.apple).setOnClickListener(mClickListener);
findViewById(R.id.orange).setOnClickListener(mClickListener);
}
Button.OnClickListener mClickListener = new View.OnClickListener(){
public void onClick(View v){
TextView textFruit = (TextView)findViewById(R.id.fruit);
switch (v.getId()){
case R.id.apple:
textFruit.setText("Apple");
break;
case R.id.orange:
textFruit.setText("Orange");
break;
}
}
};
}
==========================================================================
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:id="@+id/fruit"
android:layout_height="wrap_content"
android:text="fruit"
android:textColor="#ffff00"
android:textSize="48sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
>
<Button
android:id="@+id/apple"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="apple"
android:onClick="mOnClick"
/>
<Button
android:id="@+id/orange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="orange"
android:onClick="mOnClick"
/>
</LinearLayout>
</LinearLayout>
=====================================================================
public class MainActivity extends AppCompatActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void mOnClick(View v){
TextView textFruit = (TextView)findViewById(R.id.fruit);
switch (v.getId()){
case R.id.apple:
textFruit.setText("Apple");
break;
case R.id.orange:
textFruit.setText("Orange");
break;
}
}
}
'IT 통신 수학 과학 > 자바 안드로이드' 카테고리의 다른 글
Android popmenu (0) | 2020.09.08 |
---|---|
컨텍스트 메뉴 (0) | 2020.09.06 |
menu기능//item.setIcon(R.drawable.ic_launcher)오류 (0) | 2020.09.02 |
타이머 메시지 핸들러 (0) | 2020.08.29 |
터치 입력시험 (0) | 2020.08.19 |
콜백메서드 재정의 / 리스너 인터페이스 구현 (0) | 2020.08.17 |
onRequestPermissionsResult error // READ_CONTACT_CODE error (0) | 2020.08.15 |
안드로이드 앱 권한 요청 (0) | 2020.08.02 |