일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 한국산업인력공단
- 개인정보안전성
- 가명정보처리
- 한국정보보호산업협회기자단
- package.json
- package-lock.json
- 호이스팅
- 백엔드입문
- 확장 유클리드 알고리즘
- Writeup
- node.js
- 개인정보보호교육
- 덧셈 암호
- 개인정보보호
- function scope
- 웹 프레임워크
- 모듈러 연산
- 백엔드
- pwnable.tw
- 곱셈 암호
- 디오판투스 알고리즘
- 동적타이핑
- 국가인적자원개발컨소시엄
- 포너블
- 개인정보보호위원회
- 마감임박
- 한국정보보호산업협회
- 무료교육
- arrow function
- 유클리드_알고리즘
- Today
- Total
짱짱해커가 되고 싶은 나
10-2. 액티비티와 인텐트 본문
* 양방향 인텐트
- startActivityForResult()
- setResult()
<MainActivity.java>
package com.example.project10_2;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("메인 액티비티");
Button btn = (Button)findViewById(R.id.mainBtn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText num1 = (EditText)findViewById(R.id.num1);
EditText num2 = (EditText)findViewById(R.id.num2);
Intent intent = new Intent(getApplicationContext(),SecondActivity.class);
intent.putExtra("Num1", Integer.parseInt(num1.getText().toString()));
intent.putExtra("Num2", Integer.parseInt(num2.getText().toString()));
startActivityForResult(intent, 0);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
int hap = data.getIntExtra("Hap", 0);
Toast.makeText(getApplicationContext(), "합계 : " + hap, Toast.LENGTH_SHORT).show();
}
}
}
- startActivityForResult(Intent, param2) : 값을 돌려받기 위해 사용하며 돌려받을 값이 있을 경우 param2>=0으로 설정
- onActivityResult() : setResult()로 값을 돌려주면 오버라이딩한 onActivityResult()가 수행
<SecondActivity.java>
package com.example.project10_2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.annotation.Nullable;
public class SecondActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
setTitle("세컨드 액티비티");
Button btn = (Button)findViewById(R.id.secondBtn);
Intent intent = getIntent();
final int hapValue = intent.getIntExtra("Num1", 0) +
intent.getIntExtra("Num2", 0);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent outIntent = new Intent(getApplicationContext(), MainActivity.class);
outIntent.putExtra("Hap", hapValue);
setResult(RESULT_OK, outIntent);
finish();
}
});
}
}
데이터를 받을 Intent와 데이터를 보내줄 Intent 생성
- setResult(상태코드, Intent) : 메인 엑티비티로 데이터를 돌려준다.
* 액티비티 생명주기
: 액티비티의 생성부터 소멸까지의 주기
onCreate() -> onStart() -> onResume() -> MainActivity 실행
if) main activity 종료 : onPause() -> onStop() -> onDestroy()
else if) 다른 액티비티 요청 : onPause() -> onStop() -> 다른 액티비티 실행/종료 -> onRestart() -> onStart() -> onResume()
안드로이드 응용 프로그램은 화면이 작기 때문에 동시에 여러 개의 액티비티가 나올 수 없다. 따라서 여러 개의 액티비티로 작성 된 응용 프로그램이 있다면 앞에 나오는 화면 하나만 활성화 상태고 나머지는 모두 비활성화 상태가 된다.
+ 로그캣(LogCat)
: 작성 중인 프로그램에 예기치 못한 오류가 발생했을 때 원인을 파악하는 방법 중 하나로 로그캣이라는 화면을 제공해 로그를 확인 할 수 있다.
프로젝트1: 로그캣을 이용한 액티비티 생명주기 확인
Button 2개 - 전화걸기(다른 엑티비티), 끝내기(메인 엑티비티 종료)
package com.example.project10_2;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onDestroy() {
super.onDestroy();
android.util.Log.i("액티비티 테스트", "onDestroy()");
}
@Override
protected void onPause() {
super.onPause();
android.util.Log.i("액티비티 테스트", "onPause()");
}
@Override
protected void onRestart() {
super.onRestart();
android.util.Log.i("액티비티 테스트", "onRestart()");
}
@Override
protected void onStart() {
super.onStart();
android.util.Log.i("액티비티 테스트", "onStart()");
}
@Override
protected void onStop() {
super.onStop();
android.util.Log.i("액티비티 테스트", "onStop()");
}
protected void onResume() {
super.onResume();
android.util.Log.i("액티비티 테스트", "onResume()");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("메인 액티비티");
android.util.Log.i("액티비티 테스트", "onCreate()");
Button btn1 = (Button)findViewById(R.id.btn1);
Button btn2 = (Button)findViewById(R.id.btn2);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("tel:01012345678");
Intent intent = new Intent(Intent.ACTION_DIAL, uri);
startActivity(intent);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
프로젝트2: 액티비티 3개
MainActivity - Button 1개(새 화면 열기)
SecondActivity - Button 2개(새 화면 열기/ 돌아가기)
ThirdActivity - Button 1개(돌아가기)
<MainActivity.java>
package com.example.project10_2;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.mainBtn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(intent);
}
});
}
}
<SecondActivity.java>
package com.example.project10_2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.annotation.Nullable;
public class SecondActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
setTitle("Second 액티비티");
Button btn1 = (Button)findViewById(R.id.secondBtn1);
Button btn2 = (Button)findViewById(R.id.secondBtn2);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), ThirdActivity.class);
startActivity(intent);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
<ThirdActivity.java>
package com.example.project10_2;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.annotation.Nullable;
public class ThirdActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third);
setTitle("Third 액티비티");
Button btn = (Button)findViewById(R.id.thirdBtn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
프로젝트3: 명화 선호도 투표 수정2
뷰플리퍼로 인기 순으로 1초마다 자동으로 이미지 나오기
<second.xml>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_margin="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="자동보기 시작"
android:id="@+id/secondBtn1"/>
<Button
android:layout_margin="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="자동보기 정지"
android:id="@+id/secondBtn2"/>
</LinearLayout>
<ViewFlipper
android:id="@+id/viewFlipper"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView1"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"/>
<ImageView
android:id="@+id/imageView2"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"/>
<ImageView
android:id="@+id/imageView3"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"/>
<ImageView
android:id="@+id/imageView4"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"/>
<ImageView
android:id="@+id/imageView5"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"/>
<ImageView
android:id="@+id/imageView6"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"/>
<ImageView
android:id="@+id/imageView7"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"/>
<ImageView
android:id="@+id/imageView8"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"/>
<ImageView
android:id="@+id/imageView9"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"/>
</ViewFlipper>
</LinearLayout>
<SecondActivity.java>
package com.example.project10;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.ViewFlipper;
import androidx.annotation.Nullable;
public class SecondActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
setTitle("투표 결과");
final ViewFlipper viewFlipper;
Integer imageResourceId[] = {R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4,
R.drawable.image5, R.drawable.image6, R.drawable.image7, R.drawable.image8, R.drawable.image9};
Integer imageId[] = new Integer[]{R.id.imageView1, R.id.imageView2, R.id.imageView3, R.id.imageView4,
R.id.imageView5, R.id.imageView6, R.id.imageView7, R.id.imageView8, R.id.imageView9};
ImageView imageView[] = new ImageView[9];
int[] indexs = {0,1,2,3,4,5,6,7,8};
Button btn1 = (Button)findViewById(R.id.secondBtn1);
Button btn2 = (Button)findViewById(R.id.secondBtn2);
viewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper);
for(int i=0; i<imageId.length; i++){
imageView[i] = (ImageView)findViewById(imageId[i]);
}
viewFlipper.setFlipInterval(1000);
Intent intent = getIntent();
int[] voteCount = intent.getIntArrayExtra("VoteCount");
String[] imageName = intent.getStringArrayExtra("ImageName");
for(int i=0; i<voteCount.length-1; i++){
for(int j= i+1; j<voteCount.length; j++){
if(voteCount[i] < voteCount[j]){
int tmp = voteCount[i];
voteCount[i] = voteCount[j];
voteCount[j] = tmp;
tmp = indexs[i];
indexs[i] = indexs[j];
indexs[j] = tmp;
}
}
}
for(int i=0; i<indexs.length; i++){
imageView[i].setImageResource(imageResourceId[indexs[i]]);
}
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewFlipper.startFlipping();
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewFlipper.stopFlipping();
}
});
}
}
'모바일' 카테고리의 다른 글
11-2. 갤러리 & 스피너 (0) | 2021.02.25 |
---|---|
11-1. 리스트뷰 & 그리드뷰 (0) | 2021.02.25 |
10-1. 액티비티와 인텐트 (0) | 2021.02.24 |
09-2. 이미지 (0) | 2021.02.24 |
09-1. 그래픽 (0) | 2021.02.23 |