버튼 클릭 이벤트
1
2
3
4
5
6
7
8
9
10
11 |
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">BtnStudy</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="btn">이게바로 버튼</string>
<string name="tv">여기가 텍스트뷰</string>
</resources>
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.hoitstudy.btn.BtnClick" >
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tv" />
</LinearLayout> |
cs |
첫 번째 이벤트 처리방법
[src-com.hoitstudy.btn-BtnClick.java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 |
package com.hoitstudy.btn;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.example.btnstudy.R;
public class BtnClick extends Activity
{
Button btn;
TextView tv;
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.btn);
tv = (TextView)findViewById(R.id.tv);
btn.setOnClickListener(btnClick);
}
private OnClickListener btnClick = new OnClickListener()
{
@Override
public void onClick(View v)
{
i++;
tv.setText("버튼"+i+"번 눌렀다");
}
};
}
|
cs |
두 번째 이벤트 처리방법
[src-com.hoitstudy.btn-BtnClick.java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 |
package com.hoitstudy.btn;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.example.btnstudy.R;
public class BtnClick extends Activity
{
Button btn;
TextView tv;
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.btn);
tv = (TextView)findViewById(R.id.tv);
btn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
i++;
tv.setText("버튼"+i+"번 눌렀다");
}
});
}
}
|
cs |
세 번째 이벤트 처리방법
[res-values-string.xml]1 2 3 4 5 6 7 8 9 10 11 12 13 | <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">BtnStudy</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="btn1">첫번째 버튼입니다</string> <string name="btn2">두번째 버튼입니다</string> <string name="tv1">여기가 첫번째 텍스트뷰</string> <string name="tv2">여기가 두번째 텍스트뷰</string> </resources> | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.hoitstudy.btn.BtnClick" > <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn1" /> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn2" /> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tv1" /> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tv2" /> </LinearLayout> | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | package com.hoitstudy.btn; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import com.example.btnstudy.R; public class BtnClick extends Activity { Button btn1, btn2; TextView tv1, tv2; int i,j = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btn1).setOnClickListener(mClickListener); findViewById(R.id.btn2).setOnClickListener(mClickListener); tv1 = (TextView)findViewById(R.id.tv1); tv2 = (TextView)findViewById(R.id.tv2); } Button.OnClickListener mClickListener = new OnClickListener() { @Override public void onClick(View v) { switch(v.getId()) { case R.id.btn1: i++; tv1.setText("첫번째 버튼이 "+i+"번 눌렸습니다"); break; case R.id.btn2: j++; tv2.setText("두번째 버튼이 "+j+"번 눌렸습니다"); break; } } }; } | cs |
'Study > Android' 카테고리의 다른 글
Toast(토스트) 출력 (0) | 2015.07.30 |
---|---|
setVisibility를 사용해보자 (0) | 2015.03.27 |