2016.05.31


구성

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
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
    <EditText
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
 
        <requestFocus>
        </requestFocus>
    </EditText>
 
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center" >
 
        <Button
            android:id="@+id/button_ok"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="입력완료" >
        </Button>
 
        <Button
            android:id="@+id/button_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="취소" >
        </Button>
    </LinearLayout>
 
</LinearLayout>
cs


activity_sub.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <TextView
        android:id="@+id/textView1"
        android:textSize="25dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="받은 문자열" >
    </TextView>
 
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="확인" >
    </Button>
 
</LinearLayout>
cs


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
package com.example.user150226.a20160531ex2;
 
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 
 
    private static final int GET_STRING = 1;
    EditText editText;
    Button button_ok, button_cancel;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        editText = (EditText) findViewById(R.id.edit);
 
 
        Button button_ok = (Button) findViewById(R.id.button_ok);
        Button button_cancel = (Button) findViewById(R.id.button_cancel);
 
        button_ok.setOnClickListener(this);
        button_cancel.setOnClickListener(this);
    }
 
    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.button_ok) {
            Intent intent = new Intent(MainActivity.this, SubActivity.class);
            intent.putExtra("edit", editText.getText().toString());
 
            startActivityForResult(intent, GET_STRING);
        } else {
            finish();
        }
    }
 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode==GET_STRING) {
            if (resultCode==RESULT_OK) {
                editText.setText("");
 
                String edit = data.getExtras().getString("edit").toString();
                Toast.makeText(this"이전에 받았던 문자는" + edit + "입니다.", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
cs


SubActivity.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
package com.example.user150226.a20160531ex2;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
public class SubActivity extends Activity {
    String edit;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub);
 
        Intent intent = getIntent();
        edit = intent.getExtras().getString("edit").toString();
 
        TextView textView1 = (TextView) findViewById(R.id.textView1);
        textView1.setText("받아온 문자는 " + edit);
 
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
 
            @Override
            public void onClick(View v) {
                process();
                finish();
            }
        });
    }
 
    private void process() {
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("edit", edit);
        setResult(Activity.RESULT_OK, intent);
    }
}
 
cs


AndroidManifest.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.user150226.a20160531ex2">
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SubActivity"></activity>
    </application>
 
</manifest>
cs


*실행결과




,


명시적 인텐트 값의 전달 예제)


구성



 뷰페이지 구현 

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?xml version="1.0" encoding="utf-8"?>
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="" />
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:text="아이디 : "
            android:textSize="22dp"
            android:layout_width="100dp"
            android:layout_height="wrap_content"/>
        <EditText
            android:hint="아이디 입력"
            android:id="@+id/edtId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:text="패스워드 : "
            android:textSize="22dp"
            android:layout_width="100dp"
            android:layout_height="wrap_content"/>
        <EditText
            android:hint="패스워드 입력"
            android:id="@+id/edtPw"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <Button
            android:text="전송"
            android:id="@+id/btnSend"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />
        <Button
            android:text="종료"
            android:id="@+id/btnExit"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>
 
cs



receive.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation = "vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
 
    <TextView
        android:text="결과"
        android:textSize="50px"
        android:layout_margin="10dp"
        android:id="@+id/txtResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:text="확인"
        android:id="@+id/btnOk"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>
cs



 뷰페이지 구현 

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
package com.example.user150226.a20160531ex1;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
public class MainActivity extends Activity implements OnClickListener {
 
    EditText edtId, edtPw;
    Button btnSend, btnExit;
    // 결과를 되받기 위한 변수 설정
    private static final int CALL_REQUEST = 1;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        edtId = (EditText)findViewById(R.id.edtId);
        edtPw = (EditText)findViewById(R.id.edtPw);
 
        Button btnSend = (Button)findViewById(R.id.btnSend);
        Button btnExit = (Button)findViewById(R.id.btnExit);
 
        btnSend.setOnClickListener(this);
        btnExit.setOnClickListener(this);
 
    }//onCreate()
 
    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.btnSend) {
            Intent intent = new Intent(this, IntentReceive.class);
            // 값을 받을 액티비티로 보낼 값을 지정
            //(key, value 한 쌍으로 이루어진 임의 타입의 정보를 원하는 개수만큼 전달할수있음)
            intent.putExtra("id", edtId.getText().toString());
            intent.putExtra("pw", edtPw.getText().toString());
            //리턴값을 돌려주는 액티비티 호출
            startActivityForResult(intent, CALL_REQUEST);
        }
        else {
            finish();
        }
 
    }
 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //호출된 액티비티가 종료시 onActivityResult 메소드 호출
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == CALL_REQUEST) {
            if(resultCode==RESULT_OK) {
 
                edtId.setText("");
                edtPw.setText("");
 
                String id = data.getExtras().getString("id").toString();
                String pwd = data.getExtras().getString("pw").toString();
                Toast.makeText(this, id + " 님, 로그아웃 되셨습니다.", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
cs


IntentReceive.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
package com.example.user150226.a20160531ex1;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
public class IntentReceive extends Activity {
    String id, pwd;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.receive);
 
        //이전 액티비티에서 전달한 값 불러오기
        Intent intent = getIntent();
        id = intent.getExtras().getString("id").toString();
        pwd = intent.getExtras().getString("pw").toString();
 
        TextView txtResult = (TextView) findViewById(R.id.txtResult);
        txtResult.setText("아이디는 " + id + ", 비밀번호는 " + pwd);
 
        Button btnOk = (Button) findViewById(R.id.btnOk);
        btnOk.setOnClickListener(new View.OnClickListener() {
 
            @Override
            public void onClick(View v) {
                process();
                finish();
            }
        });
    }
 
    private void process() {
        //호출한 액티비티에게 결과값 전달
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("id", id);
        intent.putExtra("pw", pwd);
        setResult(Activity.RESULT_OK, intent);
    }
}
cs


AndroidManifest.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
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.user150226.a20160531ex1"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".IntentReceive">
        </activity>
    </application>
 
</manifest>
cs


*실행결과






,


뷰 컨테이너 예제)


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
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
 
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/edtUrl"
            android:layout_weight="1"
            android:singleLine="true" />
 
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnGo"
            android:text="이동" />
 
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnBack"
            android:text="이전" />
 
        </LinearLayout>
 
    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView1" />
 
</LinearLayout>
cs


 뷰페이지 구현 


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
package com.example.user150226.test6;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
 
public class MainActivity extends AppCompatActivity {
 
    EditText edtUrl;
    Button btnGo, btnBack;
    WebView web;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        edtUrl = (EditText) findViewById(R.id.edtUrl);
        btnGo = (Button) findViewById(R.id.btnGo);
        btnBack = (Button) findViewById(R.id.btnBack);
        web = (WebView) findViewById(R.id.webView1);
 
        web.setWebViewClient(new test6Client());
 
        WebSettings webSet = web.getSettings();
        webSet.setBuiltInZoomControls(true);
 
        btnGo.setOnClickListener(new View.OnClickListener() {
 
            @Override
            public void onClick(View v) {
                web.loadUrl(edtUrl.getText().toString());
            }
        });
 
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                web.goBack();
            }
        });
    }
 
    class test6Client extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
 
            return super.shouldOverrideUrlLoading(view, url);
        }
    }
 
}
 
cs


 액션 처리 


AndroidMainfest.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.user150226.test6">
 
    <uses-permission android:name="android.permission.INTERNET" />
  // 퍼미션
    <application
        android:allowBackup="true"
        android:icon="@drawable/ronaldo"
        android:label="간단 웹브라우저"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
                    android:label="간단 웹브라우저">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>
cs

 환경설정  


*실행결과


퍼미션 종류(승인, 권한이라는 의미로 퍼미션이 제대로 설정되어 있어야 해당 기능을 사용 가능)

<uses-permission android:name="android.permission.INTERNET"/> 
   - 인터넷 사용 

<uses-permission android:name="android.permission.READ_CALENDAR"/> 

   - 캘린더 읽어오기

<uses-permission android:name="android.permission.WRITE_CALENDAR"/>       

   - 캘린더 쓰기
                   
<uses-permission android:name="android.permission.READ_CONTACTS"/>         
   - 주소록 읽어오기

<uses-permission android:name="android.permission.WRITE_CONTACTS"/> 

   - 주소록 쓰기

<uses-permission android:name="android.permission.REBOOT"/>  
   - reboot
                        
<uses-permission android:name="android.permission.RECEIVE_MMS"/>  

   - MMS 수신    
                       
<uses-permission android:name="android.permission.RECEIVE_SMS"/>  
   - SMS 수신

<uses-permission android:name="android.permission.WRITE_SMS"/>               

   - SMS 쓰기 

<uses-permission android:name="android.permission.STATUS_BAR"/>    

   - 상태표시줄 지정  

<uses-permission android:name="android.permission.VIBRATE"/>                      

   - 진동        
                      
<uses-permission android:name="android.permission.WAKE_LOCK"/>    
   - 알람                 

<uses-permission android:name="android.permission.CALL_PHONE"/> 

   - 통화   
                           
<uses-permission android:name="android.permission.CALL_PRIVILEGED"/> 
   - 통화(긴급전화 포함)  
             
<uses-permission android:name="android.permission.CAMERA"/>  
   - 카메라  

<uses-permission android:name="android.permission.FLASHLIGHT"/>

   - 플래시라이트  

<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>    

   - 통신상태 변경  

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>      

   - 네트워크 상태 접근
                   
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>         
   - WiFi 상태 변경 

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>       

   - WiFi 상태 접근   
                  
<uses-permission android:name="android.permission.BATTERY_STATS"/> 
   - 배터리 상태      
                 
<uses-permission android:name="android.permission.BLUETOOTH"/>     
   - 블루투스

,


날짜/시간 관련 예제)


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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
 
    <LinearLayout
        android:orientation = "vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <Chronometer
            android:id="@+id/chronometer1"
            android:format="예약에 걸린 시간 %s"
            android:gravity="center"
            android:textSize="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
 
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btnStart"
            android:text="예약시작" />
    </LinearLayout>
 
    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
 
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/rdoCal"
            android:text="날짜 설정 (캘린더 뷰)" />
 
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/rdoTime"
            android:text="시간 설정" />
    </RadioGroup>
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <CalendarView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/calendarView1"
                    android:showWeekNumber="false" />
                <TimePicker
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/timePicker1" />
            </FrameLayout>
        </LinearLayout>
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ffffff">
 
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btnEnd"
                android:text="예약완료" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/tvYear"
                android:text="0000"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="년"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/tvMonth"
                android:text="00"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="월"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/tvDay"
                android:text="00"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="일"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/tvHour"
                android:text="00"/>
 
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="시"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/tvMin"
                android:text="00"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="분"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="예약됨"/>
 
        </LinearLayout>
</LinearLayout>
cs


 아래와 같은 뷰페이지 구현 


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
92
93
94
95
package com.example.user150226.myttime;
 
import android.graphics.Color;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.Chronometer;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.TimePicker;
 
import java.util.Calendar;
 
public class MainActivity extends AppCompatActivity {
 
    Chronometer chrono;
    Button btnStart, btnEnd;
    RadioButton rdoCal, rdoTime;
    CalendarView calendarView;
    TimePicker tPicker;
    TextView tvYear, tvMonth, tvDay, tvHour, tvMin;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
       setTitle("시간예약");
 
        btnStart = (Button) findViewById(R.id.btnStart);
        btnEnd = (Button) findViewById(R.id.btnEnd);
        chrono = (Chronometer) findViewById(R.id.chronometer1);
        rdoCal = (RadioButton) findViewById(R.id.rdoCal);
        rdoTime = (RadioButton) findViewById(R.id.rdoTime);
        calendarView = (CalendarView) findViewById(R.id.calendarView1);
        tPicker = (TimePicker) findViewById(R.id.timePicker1);
        tvYear = (TextView) findViewById(R.id.tvYear);
        tvMonth = (TextView) findViewById(R.id.tvMonth);
        tvDay = (TextView) findViewById(R.id.tvDay);
        tvHour = (TextView) findViewById(R.id.tvHour);
        tvMin = (TextView) findViewById(R.id.tvMin);
        tPicker.setVisibility(View.INVISIBLE);
        calendarView.setVisibility(View.INVISIBLE);
 
        rdoCal.setOnClickListener(new View.OnClickListener() {
 
            @Override
            public void onClick(View v) {
                tPicker.setVisibility(View.INVISIBLE);
                calendarView.setVisibility(View.VISIBLE);
            }
        });
 
        rdoTime.setOnClickListener(new View.OnClickListener() {
 
            @Override
            public void onClick(View v) {
                tPicker.setVisibility(View.VISIBLE);
                calendarView.setVisibility(View.INVISIBLE);
            }
        });
 
        btnStart.setOnClickListener(new View.OnClickListener() {
 
            @Override
            public void onClick(View v) {
                chrono.setBase(SystemClock.elapsedRealtime());
                chrono.start();
                chrono.setTextColor(Color.RED);
            }
        });
 
        btnEnd.setOnClickListener(new View.OnClickListener() {
 
            @Override
            public void onClick(View v) {
                chrono.stop();
                chrono.setTextColor(Color.BLUE);
 
                java.util.Calendar curDate = java.util.Calendar.getInstance();
                curDate.setTimeInMillis(calendarView.getDate());
 
                tvYear.setText(Integer.toString(curDate.get(Calendar.YEAR)));
                tvMonth.setText(Integer.toString(1 + curDate.get(Calendar.MONTH)));
                tvDay.setText(Integer.toString(curDate.get(Calendar.DATE)));
 
                tvHour.setText(Integer.toString(tPicker.getCurrentHour()));
                tvMin.setText(Integer.toString(tPicker.getCurrentMinute()));
            }
        });
    }
}
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
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
 
        <LinearLayout
        android:background="@drawable/layout_border"
        android:layout_width="match_parent"
        android:layout_height="200dp">
 
        <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="여기에 입력" />
        </LinearLayout>
 
        <LinearLayout
            android:orientation="horizontal"
            android:background="#0000ff"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <Button
            android:text="save"
            android:layout_width="190dp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />
        <Button
            android:text="cancle"
            android:layout_width="190dp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />
        </LinearLayout>
 
        <LinearLayout
        android:background="#0000ff"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button
        android:text="Exit"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
        </LinearLayout>
    </LinearLayout>
cs

 레이아웃 세개를 만들었다. TextView와 Save + Cancle 버튼 그리고 

 EXIT 버튼을 각각 레이아웃으로 지정하여 정렬 하였다. 


*실행결과






'Android' 카테고리의 다른 글

Android Studio 뷰 컨테이너  (0) 2016.05.30
Android Studio 날짜/시간 관련 위젯  (0) 2016.05.30
Android Studio 레이아웃 연습  (0) 2016.05.27
Android Studio 기본 위젯 활용  (0) 2016.05.27
Android Studio 버튼액션 구현  (0) 2016.05.26
,


Android Studio 레이아웃 연습 예제)


 흰 건반(7개)을 만들어 주었다, 

 FrameLayout 은 검은 건반을 흰 건반 위에 올리기 위해 사용 



 검은 건반 6개를 만든 뒤  2개의 검은 건반 뒤의 건반을 투명하게 만들었다. 



*실행결과 






'Android' 카테고리의 다른 글

Android Studio 날짜/시간 관련 위젯  (0) 2016.05.30
Android Studio 레이아웃 연습2  (0) 2016.05.27
Android Studio 기본 위젯 활용  (0) 2016.05.27
Android Studio 버튼액션 구현  (0) 2016.05.26
Android Studio  (0) 2016.05.25
,

기본 위젯 활용 예제)


 activity_main.xml  


뷰페이지 구성 



 MainActivity.java 

액션처리

 사용할 기능별 변수 선언 


 아이디값을 통해 각각 기능들 불러오기 




 체크박스에 체크를 하면 보이도록 구현 



 두가지의 선택지 중 각각의 액션 구현 




'Android' 카테고리의 다른 글

Android Studio 날짜/시간 관련 위젯  (0) 2016.05.30
Android Studio 레이아웃 연습2  (0) 2016.05.27
Android Studio 레이아웃 연습  (0) 2016.05.27
Android Studio 버튼액션 구현  (0) 2016.05.26
Android Studio  (0) 2016.05.25
,


Android Studio 버튼액션 구현

=> 버튼을 눌렀을 때 실행 될 액션 


예제)

 res-values-string.xml 

string 에서 버튼들의 정보를 미리 지정하였다.



 res-layout-activity_main.xml 

정렬을 위한 코드 android:orientation 속성을 vertical 로 주어 각 버튼들을 아래로 정렬 시켰다.


버튼 4개를 만들어 주었다(각 버튼은 string 에서 미리 만들어 놓았던 속성을 불러왔음)



 MainActivity.java 

사용할 버튼들을 선언하였다.


버튼1 (웹 연결), 버튼2 (전화걸기 연결), 버튼3 (갤러리로 이동), 버튼4 (종료) 에 

각각의 액션들을 지정해 주었다.


*실행결과




+ 버튼에 onClick 속성을 넣어 구현)


 res-layout-activity_main.xml 

1
2
3
4
5
6
7
    <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation = "vertical"
                    android:id="@+id/button1"
                    android:text="@string/strBtn1"
                    android:onClick="onClick01" />
cs

위의 기존 코드에서 추가해 준 부분 :  android:onClick="onClick01"



 MainActivity.java 

위의 기존 코드에서 변화 된 부분

1
2
3
4
5
6
7
8
button1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("http://www.daum.net"));
        startActivity(intent);
    }
});
cs

         ▼

1
2
3
4
    public void onClick01(View v) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.daum.net"));
        startActivity(intent);
    }
cs


=> Intent 는 어떤 액션이 수행됨에 있어서 특정 데이터의 특정 부분을 가지고 

수행하라는 명령을 전달한다. 위의 코드에서는 Intent 를 intent 로 정의하여 여기에 담긴 

정보 "http://www.daum.net" 로 이동하라는 명령을 전달하고 

startActivity(intent) 는 intent 라는 이름을 가지고 있는 Intent 를 실행하라는 뜻이다.




'Android' 카테고리의 다른 글

Android Studio 날짜/시간 관련 위젯  (0) 2016.05.30
Android Studio 레이아웃 연습2  (0) 2016.05.27
Android Studio 레이아웃 연습  (0) 2016.05.27
Android Studio 기본 위젯 활용  (0) 2016.05.27
Android Studio  (0) 2016.05.25
,

Android Studio

Android 2016. 5. 25. 21:24


Android Studio 구성



 mainfests 

: 어플리케이션의 기본정보(이름, 아이콘이미지, 화면, 버전 퍼미션관리 등)를 가지고 있는 XML 파일


 java 

: 실질적인 동작과 구현내용 정의 (java는 동작 xml은 화면구성(UI)


 res 
- drawable : 어플리케이션에서 사용할 모든 이미지 파일들을 가지고 있는 폴더 

- layout : 정적 UI 화면 구성 파일들을 가지고 있는 폴더

- mipmap : 어플리케이션의 메인 아이콘 이미지(해상도별)를 저장하는 폴더

- values

-- colors : 어플리케이션에서 사용할 색상 한번에 지정

-- dimens : 어플리케이션에서 사용할 여백, 글자크기 등을 한번에 지정

-- strings : 어플리케이션에서 사용할 텍스트를 미리 지정(언어 지원)

-- styles : 어플리케이션의 기본 디자인 지정 



'Android' 카테고리의 다른 글

Android Studio 날짜/시간 관련 위젯  (0) 2016.05.30
Android Studio 레이아웃 연습2  (0) 2016.05.27
Android Studio 레이아웃 연습  (0) 2016.05.27
Android Studio 기본 위젯 활용  (0) 2016.05.27
Android Studio 버튼액션 구현  (0) 2016.05.26
,