1. [과제] 계산기 만들기
<?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:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="계 산 기"
android:gravity="center"
android:textAppearance="@style/TextAppearance.AppCompat.Display1" />
<EditText
android:id="@+id/etResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:textColorHint="#000000"
android:hint="0"
android:gravity="right" />
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:background="#F3EAA1"
android:text="7" />
<Button
android:id="@+id/btn8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:background="#F3EAA1"
android:text="8" />
<Button
android:id="@+id/btn9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:background="#F3EAA1"
android:text="9" />
<Button
android:id="@+id/btn_div"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:background="#5FDAEA"
android:text="÷" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="54dp"
android:layout_margin="3dp"
android:background="#F3EAA1"
android:text="4" />
<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F3EAA1"
android:layout_margin="3dp"
android:text="5" />
<Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:background="#F3EAA1"
android:text="6" />
<Button
android:id="@+id/btn_mul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:background="#5FDAEA"
android:text="X" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:background="#F3EAA1"
android:text="1" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F3EAA1"
android:layout_margin="3dp"
android:text="2" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F3EAA1"
android:layout_margin="3dp"
android:text="3" />
<Button
android:id="@+id/btn_sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#5FDAEA"
android:layout_margin="3dp"
android:text="─" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/btn_del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:background="#B2E17B"
android:text="C" />
<Button
android:id="@+id/btn0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:background="#F3EAA1"
android:text="0" />
<Button
android:id="@+id/btn_et_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:background="#B2E17B"
android:text="=" />
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:background="#5FDAEA"
android:text="+" />
</TableRow>
</TableLayout>
</LinearLayout>
package com.lec.android.a003_widget;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Main2Activity extends AppCompatActivity {
EditText etResult;
Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;
Button btn_div, btn_mul, btn_sub, btn_add;
Button btn_et_result, btn_del;
// 피연산자
String cal1 = "";
String cal2 = "";
// 연산자
String operator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
etResult = findViewById(R.id.etResult);
// 숫자
btn0 = findViewById(R.id.btn0);
btn1 = findViewById(R.id.btn1);
btn2 = findViewById(R.id.btn2);
btn3 = findViewById(R.id.btn3);
btn4 = findViewById(R.id.btn4);
btn5 = findViewById(R.id.btn5);
btn6 = findViewById(R.id.btn6);
btn7 = findViewById(R.id.btn7);
btn8 = findViewById(R.id.btn8);
btn9 = findViewById(R.id.btn9);
// 연산자
btn_div = findViewById(R.id.btn_div);
btn_mul = findViewById(R.id.btn_mul);
btn_sub = findViewById(R.id.btn_sub);
btn_add = findViewById(R.id.btn_add);
// 결과, 지우기
btn_et_result = findViewById(R.id.btn_et_result);
btn_del = findViewById(R.id.btn_del);
btn_et_result.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cal2 = etResult.getText().toString();
if(operator.equals("") || cal1.equals("") || cal2.equals("")) {return;}
double result = 0;
try {
if(operator.equals("+")) {
result = Double.parseDouble(cal1) + Double.parseDouble(cal2);
etResult.setText(String.valueOf(result));
} else if(operator.equals("-")) {
result = Double.parseDouble(cal1) - Double.parseDouble(cal2);
etResult.setText(String.valueOf(result));
} else if(operator.equals("X")) {
result = Double.parseDouble(cal1) * Double.parseDouble(cal2);
etResult.setText(String.valueOf(result));
} else {
result = Double.parseDouble(cal1) / Double.parseDouble(cal2);
etResult.setText(String.valueOf(result));
}
} catch (Exception e){
etResult.setText("");
} finally {
cal1 = "";
cal2 = "";
operator = "";
}
}
});
btn_del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
etResult.setText("");
}
});
// 숫자 버튼 리스너
class numberListener implements View.OnClickListener {
@Override
public void onClick(View v) {
String text = (String)((Button)v).getText();
etResult.setText(etResult.getText().append(text));
}
}
// 피연산자 리스너
class calListener implements View.OnClickListener {
@Override
public void onClick(View v) {
cal1 = etResult.getText().toString();
operator = (String)((Button)v).getText();
etResult.setText("");
}
}
btn0.setOnClickListener(new numberListener());
btn1.setOnClickListener(new numberListener());
btn2.setOnClickListener(new numberListener());
btn3.setOnClickListener(new numberListener());
btn4.setOnClickListener(new numberListener());
btn5.setOnClickListener(new numberListener());
btn6.setOnClickListener(new numberListener());
btn7.setOnClickListener(new numberListener());
btn8.setOnClickListener(new numberListener());
btn9.setOnClickListener(new numberListener());
btn_add.setOnClickListener(new calListener());
btn_sub.setOnClickListener(new calListener());
btn_mul.setOnClickListener(new calListener());
btn_div.setOnClickListener(new calListener());
} // end onCreate()
} // end Main2Activity
** 과제를 하면서 아쉬웠던 점
1) 안드로이드에 익숙하지 못해서 과제를 완수하지 못했음.
(이번 과정을 통해 처음 안드로이드를 접함)
2) 블로그에 올린 코드는 쌤이 픽한 학생의 코드를 참고하여 작성하여 올림.
** 과제를 하면서 깨달은 점
1) 레이아웃과 액티비티는 연결된다는 점
(스크린 위에 액티비티가 깔려있고 액티비티 위에 뷰가 깔려 있다는 내용이 이해됨)
2) 레이아웃을 통해 사용자가 볼 수 있는 View가 만들어진다는 점
3) 뷰에 다양한 동작이 발생이나 상태 변화가 발생함,
이러한 이벤트가 발생하면 액티비티를 통해 동작을 심어줄 수 있음
(이 동작을 심어주기 위해 리스너를 사용한다는 점을 알게 됨)
2. a004_widger 모듈
1) Main2Activity 액티비티, activity_main2 레이아웃
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lec.android.a004_widger">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Main2Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
</application>
</manifest>
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Main2Activity">
<EditText
android:id="@+id/op1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="44dp"
android:ems="10"
android:inputType="number" />
<EditText
android:id="@+id/op2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="48dp"
android:ems="10"
android:inputType="number" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="395dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btnPlus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ADD"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<Button
android:id="@+id/btnMinus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="SUB"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<Button
android:id="@+id/btnMul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="MUL"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<Button
android:id="@+id/btnDiv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="DIV"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
</LinearLayout>
<TextView
android:id="@+id/tvResult"
android:layout_width="247dp"
android:layout_height="57dp"
android:layout_marginTop="48dp"
android:text="결과"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
</LinearLayout>
package com.lec.android.a004_widger;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.graphics.Color;
import android.hardware.TriggerEvent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Main2Activity extends AppCompatActivity {
EditText op1, op2;
TextView tvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
op1 = findViewById(R.id.op1);
op2 = findViewById(R.id.op2);
tvResult = findViewById(R.id.tvResult);
Button btnPlus = findViewById(R.id.btnPlus);
Button btnMinus = findViewById(R.id.btnMinus);
Button btnMul = findViewById(R.id.btnMul);
Button btnDiv = findViewById(R.id.btnDiv);
btnPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String oper1 = op1.getText().toString();
String oper2 = op2.getText().toString();
int a, b;
if(oper1 != null && !oper1.trim().equals("")){
a = Integer.parseInt(oper1);
} else {
a = 0;
}
// "".equals(oper2)
if(oper2 != null && !oper2.trim().equals("")){
b = Integer.parseInt(oper2);
} else {
b = 0;
}
tvResult.setText((a + b) + "");
//a + b
}
});
btnMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String oper1 = op1.getText().toString();
String oper2 = op2.getText().toString();
int a, b;
if(oper1 != null && !oper1.trim().equals("")){
a = Integer.parseInt(oper1);
} else {
a = 0;
}
// "".equals(oper2)
if(oper2 != null && !oper2.trim().equals("")){
b = Integer.parseInt(oper2);
} else {
b = 0;
}
tvResult.setText((a - b) + "");
//a - b
}
});
btnMul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String oper1 = op1.getText().toString();
String oper2 = op2.getText().toString();
int a, b;
if(oper1 != null && !oper1.trim().equals("")){
a = Integer.parseInt(oper1);
} else {
a = 0;
}
// "".equals(oper2)
if(oper2 != null && !oper2.trim().equals("")){
b = Integer.parseInt(oper2);
} else {
b = 0;
}
tvResult.setText((a * b) + "");
//a * b
}
});
btnDiv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String oper1 = op1.getText().toString();
String oper2 = op2.getText().toString();
int a, b;
if(oper1 != null && !oper1.trim().equals("")){
a = Integer.parseInt(oper1);
} else {
a = 0;
}
// "".equals(oper2)
if(oper2 != null && !oper2.trim().equals("")){
b = Integer.parseInt(oper2);
} else {
b = 0;
}
tvResult.setText((a / b) + "");
//a / b
}
});
op1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
// 포커스 변화가 발생했을때 호출되는 메소드
// boolean 타입 두번째 매개변수 : 포커스 받은 상태이면 true, 잃은상태면 false
@Override
public void onFocusChange(View view, boolean hasFocus) {
if(hasFocus){ // 포커스 받은 상태에선 yellow 로 배경색 변화
view.setBackgroundColor(Color.YELLOW);
} else {
view.setBackgroundColor(Color.parseColor("#00000000"));
}
}
});
op1.setOnEditorActionListener(new TextView.OnEditorActionListener() {
// 타이핑 완료 되었을때 호출되는 메소드
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
tvResult.setText("첫번째 숫자 입력완료");
return false;
}
});
} // end onCreate()
} // end Activity
2) Main3Activity 액티비티, activity_main3 레이아웃
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lec.android.a004_widger">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Main3Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main2Activity"></activity>
<activity android:name=".MainActivity"></activity>
</application>
</manifest>
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Main3Activity">
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CheckBox"
android:textAppearance="@style/TextAppearance.AppCompat.Display1" />
<CheckBox
android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="딸기"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<CheckBox
android:id="@+id/cb2"
style="@style/TextAppearance.AppCompat.Large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="포도" />
<CheckBox
android:id="@+id/cb3"
style="@style/TextAppearance.AppCompat.Large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="키위" />
<CheckBox
android:id="@+id/cb4"
style="@style/TextAppearance.AppCompat.Large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="바나나" />
<Button
android:id="@+id/btnComplete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="선택완료" />
<TextView
android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="선택결과"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
</LinearLayout>
package com.lec.android.a004_widger;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
public class Main3Activity extends AppCompatActivity {
CheckBox cb1, cb2, cb3, cb4;
TextView tvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
cb1 = findViewById(R.id.cb1);
cb2 = findViewById(R.id.cb2);
cb3 = findViewById(R.id.cb3);
cb4 = findViewById(R.id.cb4);
tvResult = findViewById(R.id.tvResult);
Button btnComplete = findViewById(R.id.btnComplete);
// 버튼을 클릭하면 체트된 내용들만 결과 출력하기
btnComplete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String result = "";
if(cb1.isChecked()) {result += cb1.getText();}
if(cb2.isChecked()) {result += cb2.getText();}
if(cb3.isChecked()) {result += cb3.getText();}
if(cb4.isChecked()) {result += cb4.getText();}
tvResult.setText("선택결과: " + result);
}
});
cb1.setOnCheckedChangeListener(new CbListener());
cb2.setOnCheckedChangeListener(new CbListener());
cb3.setOnCheckedChangeListener(new CbListener());
cb4.setOnCheckedChangeListener(new CbListener());
} // end onCreate()
class CbListener implements CompoundButton.OnCheckedChangeListener {
// CheckBox 의 '상태'가 변할때마다 호출되는 메소드
// isChecked : true <- 체크된 상태, false <- uncheck 상태
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int count = 0;
if(cb1.isChecked()) {count++;}
if(cb2.isChecked()) {count++;}
if(cb3.isChecked()) {count++;}
if(cb4.isChecked()) {count++;}
tvResult.setText(count + " 개 선택");
}
}
} // end Main3Activity
3) Main4Activity 액티비티, activity_main4 레이아웃
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lec.android.a004_widger">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Main4Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main3Activity" />
<activity android:name=".Main2Activity" />
<activity android:name=".MainActivity" />
</application>
</manifest>
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Main4Activity">
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RadioButton"
android:textAppearance="@style/TextAppearance.AppCompat.Display1" />
<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radio1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="국어" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="영어" />
<RadioButton
android:id="@+id/radio3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="수학" />
<RadioButton
android:id="@+id/radio4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="과학" />
</RadioGroup>
<Button
android:id="@+id/btnResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="선택완료" />
<TextView
android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="결과창"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
</LinearLayout>
package com.lec.android.a004_widger;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class Main4Activity extends AppCompatActivity {
RadioGroup rg;
Button btnResult;
TextView tvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
// 체크박스와는 달리
// RadioButton 은 각각 선언하는 것이 아니라
// RadioGroup 으로 선언하여 사용
// 여기서 shift + F6을 이용하여 이름 변경하면
// 모든 이름이 변경됨. 레이아웃, java 파일 전부..!!
rg = findViewById(R.id.rg);
btnResult = findViewById(R.id.btnResult);
tvResult = findViewById(R.id.tvResult);
btnResult.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 선택된 라디오 버튼의 id 값 가져오기
int id = rg.getCheckedRadioButtonId();
// 위 id 값을 통해 RadioButton 객체 가져오기
RadioButton rb = findViewById(id);
tvResult.setText("결과: " + rb.getText());
}
});
// 라디오 버튼을 선택했을 때도 위와 같은 동작하게 하기
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
// checkedId : 선택된 라디오버튼의 id
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb = findViewById(checkedId);
tvResult.setText("결과: " + rb.getText());
}
});
}
}
3. 사진 추가하기
: res > drawable 클릭 > 원하는 사진을 복사(Ctrl + C)한 상태에서 붙여넣기(Ctrl + V)
> choose Destination Directory 팝업 창 뜸 > v-24로 끝나는 경로 선택 > OK 클릭(둘중에 어떤것을 해도 무방함)
> 다시 한 번 경로 확인 후 OK 클릭 > 사용할 사진 다운 받기 끝..!!
4. a005_image 모듈
1) MainActivity 액티비티, activity_main 레이아웃
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lec.android.a005_image">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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>
</application>
</manifest>
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:text="ImageView"/>
<TextView
android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="이미지를 탭하면 다음이미지로 넘어갑니다"/>
<ImageView
android:id="@+id/iv1"
android:layout_width="match_parent"
android:layout_height="268dp"
android:layout_weight="0.56"
android:src="@drawable/a6"
/>
</LinearLayout>
package com.lec.android.a005_image;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
// 안드로이드 의 모든 리소스(Resource) 로 사용하는 파일 들은
// 파일명 규칙
// - 대문자 불가
// - 숫자 시작 불가
// - 공백, 특수문자 불가
// - 특수문자는 _(언더바)만 가능
public class MainActivity extends AppCompatActivity {
int[] imageId = {R.drawable.a1, R.drawable.a2, R.drawable.a3,
R.drawable.a4, R.drawable.a5, R.drawable.a6};
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = findViewById(R.id.iv1);
// res/drawable 폴더에 있는 이미지로 세팅하기
iv.setImageResource(R.drawable.a1);
iv.setOnClickListener(new MyListener());
} // end onCreate()
class MyListener implements View.OnClickListener {
int i = 0;
TextView tvResult = findViewById(R.id.tvResult);
@Override
public void onClick(View v) {
i++;
if(i == imageId.length) {i = 0;}
iv.setImageResource(imageId[i]);
tvResult.setText("이미지뷰: " + i);
}
}
} // end Activity
2) Main2Activity 액티비티, activity_main2 레이아웃
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lec.android.a005_image">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Main2Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
</application>
</manifest>
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity"
android:orientation="vertical">
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ImageView 표현하는 방법들"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
<ImageView
android:id="@+id/iv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@android:drawable/btn_star_big_on" />
<ImageView
android:id="@+id/iv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@android:drawable/ic_menu_camera" />
</LinearLayout>
</ScrollView>
</LinearLayout>
package com.lec.android.a005_image;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ImageView iv1 = findViewById(R.id.iv1);
ImageView iv2 = findViewById(R.id.iv2);
ImageView iv3 = findViewById(R.id.iv3);
// 방법1. 프로젝트 내의 res/drawable 폴더에 있는 파일을 보여주는 방법
iv1.setImageResource(R.drawable.a2);
// 방법2. Drawable 객체를 이용해서 보여주는 방법
//Drawable drawable = getResources().getDrawable(R.drawable.a3); // minSdkVersion 28로 변경하면 deprecated 됨.
Drawable drawable = ContextCompat.getDrawable(getApplicationContext(), R.drawable.a3);
// ContextCompat : API 23부터 추가
iv2.setImageDrawable(drawable);
// 방법3 bitmap 객체를 이용해서 보여주는 방법
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.a4);
iv3.setImageBitmap(bm);
} // end onCreate()
} // end Activity
3) Main3Activity 액티비티, activity_main3 레이아웃
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lec.android.a005_image">
<!-- 권한 명시 -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- 안드로이드 10.0 (Q, API29) 부터 발생하는 문제
https://developer.android.com/training/data-storage#scoped-storage
-->
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:requestLegacyExternalStorage="true">
<activity android:name=".Main4Activity" />
<activity android:name=".Main3Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main2Activity"></activity>
<activity android:name=".MainActivity" />
</application>
</manifest>
<?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/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="폰사진 ImageView 보이기"
android:textAppearance="@style/TextAppearance.AppCompat.Display1" />
<Button
android:id="@+id/btnAction"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="사진 가져오기" />
<TextView
android:id="@+id/tvPath"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="사진경로" />
<ImageView
android:id="@+id/ivPhoto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.63"
android:src="@mipmap/ic_launcher" />
</LinearLayout>
package com.lec.android.a005_image;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Arrays;
/**
* 폰의 저장장치에 있는 사진을 ImageView 에 띄우기
* 1. 권한 획득
* - 메니페스트
* - '위험권한'요청 (Android 6.0 / API23 이상)
* https://developer.android.com/guide/topics/security/permissions?hl=ko (위험권한및 위험권한 목록들)
* https://developer.android.com/training/permissions/requesting?hl=ko#java
*
* 설치 앱별 '권한' 확인 가능하다 :
* 픽셀2 폰의 경우 '앱' 아이콘 롱클릭후 App Info 확인
*
* 2. 이미지 경로
* 3. Bitmap 사용하여 ImageView 에 띄우기
*/
public class Main3Activity extends AppCompatActivity {
ImageView ivPhoto;
TextView tvPath;
Button btnAction;
// 이미지 경로 알아내기 (제조사, 모델마다 다를 수 있다)
// 픽셀폰 : 갤러리 이미지보기 -> 하단의 i 버튼
// 삼성폰 : 갤러리 이미지 롱클릭 후 상세정보 ...
private String imgPath = "/storage/emulated/0/DCIM/Camera/france.png";
private final int PERMISSION_REQUEST_CODE = 101; // 권한 요청 코드값(int)
private final String[] PERMISSIONS = { // 요청할 권한들을 String[] 로 준비
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
ivPhoto = findViewById(R.id.ivPhoto);
btnAction = findViewById(R.id.btnAction);
tvPath = findViewById(R.id.tvPath);
btnAction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawPhoto(); // 위험권한 획득 없이 실행하면? 아래 Exception 발생
// E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/DCIM/Camera/france.png: open failed: EACCES (Permission denied)
}
});
// '위험권한' 획득
// API 23(마시멜로) 이상에서만 권한 요청
// 권한을 획득하지 않은 상태에서만 요청
//if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
// != PackageManager.PERMISSION_GRANTED) {
// 위의 코드를 if 문 두개로 나눌 것이 아니라 &&(AND)를 통해 한번에 조건 기재하기..!!
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED){
// 권한 요청 : 비동기로 진행됨.
ActivityCompat.requestPermissions(this,
PERMISSIONS, // 요청할 위험권한(들)
PERMISSION_REQUEST_CODE // 권한 요청 코드
);
} else {
// 권한을 이미 획득한 상태
drawPhoto();
}
} // end onCreate()
public void drawPhoto(){
// ImageView <- Bitmap <-경로
Bitmap bm = BitmapFactory.decodeFile(imgPath);
ivPhoto.setImageBitmap(bm);
tvPath.setText(imgPath);
} // end drawPhoto()
// 권한 획득 결과 처리 콜백
// requestCode : 권한 요청 코드 값
// permissions : 권한 요청 (들)
// grantResults : 승인 내역 (들)
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case PERMISSION_REQUEST_CODE:
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
// 사용자가 권한 승인함!
drawPhoto();
Log.d("myapp", "권한획득결과\n\t" + Arrays.toString(permissions) + " : " + Arrays.toString(grantResults));
} else {
// 사용자가 권한 승인 안함!
}
break;
}
}
} // end Activity
[추가] 프로젝트 별로 Gradle 파일 생기고 각 모듈별로 Gradle 파일 생긴다.
현재 1개의 프로젝트 Gradle 파일과 5개의 모듈 Gradle 파일이 있다.
[추가] gradle 파일 수정한 뒤에는 무조건 Sync Now를 해야 함...!!
minSdkVersion을 21 -> 28로 변경(이전 단말기 중 지원하는 최소 버전을 21 -> 28로 변경)
[추가] AVD 에 저장된 사진 확인하기
: View > Tool Windows > Device File Explorer 클릭
> sdcard > DCIM > Camera 클릭
> 현재 AVM에 저장된 사진을 볼 수 있다(현재 저장된 5개의 사진은 AVD기기를 통해 찍은 사진들)
> Camera 선택 후 우클릭 > Upload 클릭 > 다운로드 원하는 파일 선택 후 OK 클릭
> 정상적으로 파일 다운로드 완..!!
> AVD 기기의 Google Photos에서 다운로드한 파일들을 확인 할 수 있다..!!
4) Main4Activity 액티비티, activity_main4 레이아웃
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lec.android.a005_image">
<!-- 권한 명시 -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Main4Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main3Activity" />
<activity android:name=".Main2Activity" />
<activity android:name=".MainActivity" />
</application>
</manifest>
<?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/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="인터넷 이미지 보기"
android:textAppearance="@style/TextAppearance.AppCompat.Display1" />
<TextView
android:id="@+id/tvUrl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="URL"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
<ImageView
android:id="@+id/iv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.64"
android:src="@drawable/abc_btn_check_to_on_mtrl_000" />
</LinearLayout>
package com.lec.android.a005_image;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
/**
* 인터넷 상의 이미지 보여주기
* 1. 권한을 획득한다 (인터넷에 접근할수 있는 권한을 획득한다) - 메니페스트 파일
* 2. Thread 에서 웹의 이미지를 받아온다 - honeycomb(3.0) 버젼 부터 바뀜
* 3. 외부쓰레드에서 메인 UI에 접근하려면 Handler 를 사용해야 한다.
*
*/
public class Main4Activity extends AppCompatActivity {
// 이미지 URL, 반드시 https:// 이어야 한다..!!
String imgUrl = "https://developer.android.com/studio/images/studio-icon-stable.png";
ImageView iv1;
TextView tvUrl;
Handler handler = new Handler(); // 외부쓰레드에서 메인 UI 화면에 그릴 때 사용
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
iv1 = findViewById(R.id.iv1);
tvUrl = findViewById(R.id.tvUrl);
// [이 부분은 안드로이드에서 추가됨, Bitmap] <- InputStream <- URL <- "url"
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
// Thread 없이 수행하면
// android.os.NetworkOnMainThreadException 발생
URL url = new URL(imgUrl);
InputStream in = url.openStream();
final Bitmap bm = BitmapFactory.decodeStream(in);
// Handler 없이 사용하면
// CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 발생
//iv1.setImageBitmap(bm);
handler.post(new Runnable() {
@Override
public void run() {
// 외부쓰레드에서 메인 UI에 접근할 때는
// 반드시 Handler 객체 사용.
iv1.setImageBitmap(bm);
tvUrl.setText(imgUrl);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
});
t.start();
} // end onCreate()
} // end Activity
5. A006_Widget2 모듈
1) MainActivity 액티비티, activity_main 레이아웃
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lec.android.a006_widget2">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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>
</application>
</manifest>
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ProgressBar"
android:textAppearance="@style/TextAppearance.AppCompat.Display1" />
<ProgressBar
android:id="@+id/pb1"
style="?android:attr/progressBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ProgressBar
android:id="@+id/pb2"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="보이기"
android:textOff="숨기기" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="막대프로그래스바" />
<ProgressBar
android:id="@+id/pb3"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
package com.lec.android.a006_widget2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ProgressBar;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
ProgressBar pb1, pb2, pb3;
int value = 0; // 막대프로그래스 바의 현재 진행값
int add = 10; // 증가량
int value2 = 0;
int add2 = 1;
Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb1 = findViewById(R.id.pb1);
pb2 = findViewById(R.id.pb2);
pb3 = findViewById(R.id.pb3);
ToggleButton btn1 = findViewById(R.id.toggleButton);
Button btn2 = findViewById(R.id.button1);
// 토글버튼 상태 변화시 호출되는 콜백
btn1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
pb1.setVisibility(View.INVISIBLE);
} else {
pb1.setVisibility(View.VISIBLE);
}
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
value = value + add;
if(value > 100 || value < 0) {
value = -100;
}
pb2.setProgress(value); // 프로그레스 바의 진행값 설정
}
});
// 앱 시작시 Thread를 사용하여 ProgressBar 증가시키기
Thread t = new Thread(new Runnable() {
@Override
public void run() {
while(true) {
value2 = value2 + add2;
if(value2 > 100 || value2 < 0) {
add2 = -add2;
}
// 별도의 작업 Thread 에서
// 메인 UI에 접근하려면 반드시 Handler 사용해야 함..!!
handler.post(new Runnable() {
@Override
public void run() {
pb3.setProgress(value2);
}
});
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t.start();
} // end onCreate()
} // end Activity
2) Main2Activity 액티비티, activity_main2 레이아웃
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lec.android.a006_widget2">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Main2Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
</application>
</manifest>
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Main2Activity">
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Spinner"
android:textAppearance="@style/TextAppearance.AppCompat.Display1" />
<TextView
android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="선택값" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/city"/>
</LinearLayout>
package com.lec.android.a006_widget2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class Main2Activity extends AppCompatActivity {
TextView tvResult;
Spinner spinner1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
tvResult = findViewById(R.id.tvResult);
spinner1 = findViewById(R.id.spinner1);
// 아이템을 선택했을때 호출되는 콜백
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
// 아이템을 선택했을때
// position : 몇번째 item 인지, 0 ~
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
tvResult.setText("position: " + position + parent.getItemAtPosition(position));
Toast.makeText(getApplicationContext(), (String)parent.getItemAtPosition(position), Toast.LENGTH_LONG).show();
}
// 선택한거 없이 화면에서 사라질때
@Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getApplicationContext(), "선택 안했어요", Toast.LENGTH_LONG).show();
}
});
} // end onCreate()
} // end Activity
** 안드로이드 문자열 배열 리소스 추가하기
: res 선택 value 폴더 클릭 > 우클릭 > New > Values Resource File 클릭
> File name 설정 후 OK
> 필요한 문자열 배열 만들기...!!
3) Main3Activity 액티비티, activit_main3 레이아웃
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lec.android.a006_widget2">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Main3Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main2Activity" />
<activity android:name=".MainActivity" />
</application>
</manifest>
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Main3Activity">
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SeekBar"
android:textAppearance="@style/TextAppearance.AppCompat.Display1" />
<TextView
android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="진행값"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="200"
android:progress="50"
android:thumb="@mipmap/ic_launcher"/>
</LinearLayout>
package com.lec.android.a006_widget2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
public class Main3Activity extends AppCompatActivity {
TextView tvResult;
SeekBar seekBar;
int value = 0;
int add = 2;
Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
tvResult = findViewById(R.id.tvResult);
seekBar = findViewById(R.id.seekBar);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
// 값의 변화가 생겼을때 콜백
// progress : 진행값 0 ~ max
// fromUer : 사용자에 의한 진행값 변화인 경우 true
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvResult.setText("onProgressChanged:" + progress + "(" + fromUser + ")");
}
// tracking 시작할때 콜백
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Toast.makeText(getApplicationContext(), "트래킹시작", Toast.LENGTH_SHORT).show();
}
// tracking 끝날때 콜백
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(getApplicationContext(), "트래킹종료", Toast.LENGTH_SHORT).show();
}
});
// 앱 시작시 Thread .. SeekBar 증가 시키기
new Thread(new Runnable() {
@Override
public void run() {
int max = seekBar.getMax();
while(true){
value = seekBar.getProgress() + add;
if(value > max || value < 0){
add = -add;
}
handler.post(new Runnable() {
@Override
public void run() {
seekBar.setProgress(value);
}
});
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
} // end onCreate()
} // end Activity
'웹_프론트_백엔드 > JAVA프레임윅기반_풀스택' 카테고리의 다른 글
2020.04.20 (0) | 2020.04.20 |
---|---|
2020.04.17 (0) | 2020.04.17 |
2020.04.14 (0) | 2020.04.14 |
2020.04.13 (0) | 2020.04.13 |
2020.04.10 (0) | 2020.04.10 |