728x90
728x170
1. MainActivity.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 |
package ...
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
LinearLayout linearLayout;
String[] names = {"철수", "영희", "민희", "수지", "지민"};
int count = 0;
Person[] persons = new Person[5];
TextView textView;
String[][] phonebook = new String[2][5];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] friends = {"철수", "영희", "민희", "수지", "지민"};
phonebook[0] = friends;
String[] family = {"할머니", "할아버지", "엄마", "아빠", "동생"};
phonebook[1] = family;
String outNames = "";
for (int i = 0; i < phonebook.length; i++) {
outNames = outNames + ("\n" + i + " 인덱스의 그룹 : ");
for (int j = 0; j < phonebook[i].length; j++) {
outNames = outNames + phonebook[i][j];
if (j < (phonebook[i].length-1)) {
outNames = outNames + ",";
}
}
}
System.out.println(outNames);
linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
textView = (TextView) findViewById(R.id.textView);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (count >= persons.length) {
Person[] tempPersons = new Person[persons.length + 5];
System.arraycopy(persons, 0, tempPersons, 0, persons.length);
persons = tempPersons;
}
int nameIndex = count % 5;
persons[count] = new Person(names[nameIndex]);
Toast.makeText(getApplicationContext(), "사람 " + names[nameIndex] + "이 만들어졌습니다.", Toast.LENGTH_LONG).show();
String curName = persons[count].getName();
TextView nameTextView = new TextView(getApplicationContext());
nameTextView.setText(curName);
nameTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 30);
nameTextView.setTextColor(Color.BLUE);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.addView(nameTextView, params);
count++;
textView.setText(count + " 명");
}
});
}
|
cs |
2. activity_main.xml
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 |
<RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="사람 만들기"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollView"
android:layout_below="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout"></LinearLayout>
</ScrollView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0 명"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/scrollView"
android:layout_alignEnd="@+id/scrollView"
android:textSize="30dp" />
</RelativeLayout>
|
cs |
728x90
그리드형(광고전용)
'Source Code > Android' 카테고리의 다른 글
리스트뷰 (ListView) 사용 예시 3 (0) | 2017.05.09 |
---|---|
리스트뷰 (ListView) 사용 예시 2 (0) | 2017.05.09 |
리스트뷰 (ListView) 사용 예시 1 (0) | 2017.05.09 |
프레임 레이아웃을 이용한 버튼 클릭 시 그림 전환 (0) | 2017.05.09 |
스택 (Stack) & 큐 (Queue) (0) | 2017.05.07 |
화면이 터치되었을 때, X 좌표 및 Y 좌표를 알아내는 방법 (0) | 2017.05.05 |