Android Studio 11일차
2021. 11. 15. 16:59ㆍAndroid Development
액티비티를 사용한 선호도 투표.
더보기
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.EmbossMaskFilter;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
int voteCount[] = new int[9];// 각 명화별 투표횟수 저장.
String imgName[] = {"독서하는 소녀", "꽃장식 모자 소녀", "부채를 든 소녀",
"이레느깡 단 베르양", "잠자는 소녀", "테라스의 두 자매", "피아노 레슨"
, "피아노 앞의 소녀들", "해변에서"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("명화 선호도 투표");
for (int i = 0; i < 9; i++) {
voteCount[i] = 0;// 9 개 배열방에 0 으로 초기화.
Log.d("for_loop : ", "" + i);
}
// 9 개의 이미지 버튼 객체배열
ImageView image[] = new ImageView[9];
// 9 개의 이미지버튼 ID 배열
Integer imageId[] = {R.id.iv1, R.id.iv2, R.id.iv3, R.id.iv4,
R.id.iv5,
R.id.iv6, R.id.iv7, R.id.iv8, R.id.iv9};
for (int i = 0; i < imageId.length; i++) {
final int index; // 주의! 꼭 필요함..
index = i;
image[index] = (ImageView) findViewById(imageId[index]);
image[index].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// 투표수 증가.
voteCount[index]++;
// voteCount[0]++; 후치, voteCount[0]=voteCount[0]+1;
// 우선 할당하고, 다음에 증가.
Toast.makeText(getApplicationContext(),
imgName[index] + ": 총 " + voteCount[index] + " 표",
Toast.LENGTH_SHORT).show();
}
});
}
Button bt = findViewById(R.id.button2);
// 투표 종료 버튼을 누른다면,
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
SecondActivity.class);
intent.putExtra("VoteCount", voteCount);
// 인덴트라는 일종의 우편물에 VoteCount 라는 이름으로 voteCount 배열을
전달.
intent.putExtra("ImageName", imgName);
startActivity(intent);
}
});
}
}
MainActivity
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
setTitle("투표 결과");
// 앞 화면에서 보낸 투표 결과 값을 받는다.
Intent intent = getIntent();
int[] voteResult = intent.getIntArrayExtra("VoteCount");// 숫자 배열
String[] imageName = intent.getStringArrayExtra("ImageName");//
문자열 배열
// 9 개의 TextView, RatingBar 객체배열
TextView tv[] = new TextView[imageName.length];
RatingBar rbar[] = new RatingBar[imageName.length];
// 9 개의 TextView, RatingBar ID 배열
Integer tvID[] = {R.id.tv1, R.id.tv2, R.id.tv3, R.id.tv4, R.id.tv5,
R.id.tv6, R.id.tv7, R.id.tv8, R.id.tv9};
Integer rbarID[] = {R.id.rbar1, R.id.rbar2, R.id.rbar3, R.id.rbar4,
R.id.rbar5, R.id.rbar6, R.id.rbar7, R.id.rbar8, R.id.rbar9};
// TextView, RatingBar 개체 찾기.
for (int i = 0; i < voteResult.length; i++) {
tv[i] = (TextView) findViewById(tvID[i]);
rbar[i] = (RatingBar) findViewById(rbarID[i]);
}
// 각 TextVeiw 및 RatingBar 에 넘겨 받은 값을 반영.
for (int i = 0; i < voteResult.length; i++) {
tv[i].setText(imageName[i]);
rbar[i].setRating((float) voteResult[i]);
}
Button btBack = findViewById(R.id.btBack);
btBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
SecondActivity
양방향 activity 예제
더보기
public class MainActivity extends AppCompatActivity {
ArrayList<String> midList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list = findViewById(R.id.listView1);
EditText edtItem = (EditText) findViewById(R.id.et1);
Button btnAdd = (Button) findViewById(R.id.button);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, midList);
list.setAdapter(adapter);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
midList.add(edtItem.getText().toString());
//에디트 텍스트의 내용을 가져다가 어레이 리스트에 추가.
adapter.notifyDataSetChanged();
// 리스트 새로고침.
}
});
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position
, long id) {
midList.remove(position);
adapter.notifyDataSetChanged();
return false;
}
});
}
}
그리드뷰 영화 포스터 만들기.
더보기
<?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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/ivPoster"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@android:drawable/btn_star_big_on" />
</LinearLayout>
dialog.xml 레이아웃리소스 만들기
package com.example.test25;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("그리드뷰 영화 포스터");
GridView gv = findViewById(R.id.gridView1);
MyGridAdapter gAdapter = new MyGridAdapter(this);
gv.setAdapter(gAdapter);
}
//이전의 MyGraphicView 처리와 비슷
public class MyGridAdapter extends BaseAdapter{
Context context;
public MyGridAdapter(Context c){
context = c;
}
Integer[] posterID = { R.drawable.mov01, R.drawable.mov02,
R.drawable.mov03, R.drawable.mov04, R.drawable.mov05,
R.drawable.mov06, R.drawable.mov07, R.drawable.mov08,
R.drawable.mov09, R.drawable.mov10, R.drawable.mov11,
R.drawable.mov12, R.drawable.mov13, R.drawable.mov14,
R.drawable.mov15, R.drawable.mov16, R.drawable.mov17,
R.drawable.mov18, R.drawable.mov19, R.drawable.mov20,
R.drawable.mov21, R.drawable.mov22, R.drawable.mov23,
R.drawable.mov24, R.drawable.mov25, R.drawable.mov26,
R.drawable.mov27, R.drawable.mov28, R.drawable.mov29,
R.drawable.mov30 };
@Override
public int getCount() {
return posterID.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
imageView.setLayoutParams(new ViewGroup.LayoutParams(200,300));
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setPadding(5,5,5,5);
imageView.setImageResource(posterID[position]);
final int pos = position;
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View dialogView = View.inflate(MainActivity.this, R.layout.dialog, null);
AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
ImageView ivPoster= dialogView.findViewById(R.id.ivPoster);
ivPoster.setImageResource(posterID[pos]);
dlg.setTitle("큰 포스터");
dlg.setIcon(R.drawable.movie_icon);
dlg.setView(dialogView);
dlg.setNegativeButton("닫기",null);
dlg.show();
}
});
return imageView;
}
}
}
'Android Development' 카테고리의 다른 글
Android Studio 13일차 (0) | 2021.11.17 |
---|---|
Android Studio 12일차 (0) | 2021.11.16 |
Android Studio 10일차 (0) | 2021.11.12 |
Android Studio 9일차 (0) | 2021.11.11 |
Android Studio 7일차 (0) | 2021.11.09 |