본문 바로가기
JAVA/Android

[Andorid] 이벤트 처리와 액티비티간 이동

by 개폰지밥 2019. 5. 3.
반응형

| 안드로이드 화면전환

윤동주 별헤는 밤 클릭 -> 별헤는 밤 전체시
로버트 프로스트의 가지 않은 길 클릭 -> 가지 않은 길 전체시

즉 클릭하면 화면이 전환되게 만들기

 

| MainActivity.java

package com.example.a301.pora;

 

import android.content.Intent;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.LinearLayout;

 

public class MainActivity extends AppCompatActivity {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }

 

    public void displayPoem(View v){

        int id = v.getId();

        LinearLayout layout = (LinearLayout)v.findViewById(id);

        /*어떤 녀석이 클리되었는지 구분하기 위해서 tag를 해준다.*/

        String tag = (String)layout.getTag();

 

        /*메시지를 보내기 위한 쪽지이다, poem.activity*/

        Intent it = new Intent(this, Poem.class);

        it.putExtra("it_tag", tag);

        startActivity(it);

    }

}

 

| Poem.java

package com.example.a301.pora;
 
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
 
public class Poem extends AppCompatActivity {
 
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_poem);
 
        /*레이아웃에 선언한 애들*/
        LinearLayout layout_poem = (LinearLayout)findViewById(R.id.poem);
 
        TextView tv_title = (TextView)findViewById(R.id.title);
        TextView tv_author = (TextView)findViewById(R.id.author);
        TextView tv_body = (TextView)findViewById(R.id.body);
 
        /*화면 전환하고 구분자 값인 태그값을 받아라*/
        Intent it = getIntent();
        /*첫번째 꺼를 눌렀을 때 두번째꺼를 눌렀을 때가 실행된다.*/
        String tag = it.getStringExtra("it_tag");
 
        /*뒤에 작업을하기 위해서 리소스 받기*/
        Resources res = getResources();
 
        int id_title = res.getIdentifier("title"+tag, "string", getPackageName());
        String title = res.getString(id_title);
        tv_title.setText(title);
 
        int id_author = res.getIdentifier("author"+tag, "string", getPackageName());
        String author = res.getString(id_author);
        tv_author.setText(author);
 
        int id_body = res.getIdentifier("body"+tag, "string", getPackageName());
        String body = res.getString(id_body);
        tv_body.setText(body);
 
        int id_background = res.getIdentifier("background"+tag, "string", getPackageName());
        String background = res.getString(id_background);
 
 
        int id_img = res.getIdentifier(background,"drawable", getPackageName());
 
 
        Drawable drawable = res.getDrawable(id_img);
        layout_poem.setBackground(drawable);
    }
}

 

| Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@drawable/shape_list"
        android:id="@+id/poem01"
        android:tag="01"
        android:clickable="true"
        android:onClick="displayPoem" >
        <TextView
            android:id="@+id/title01"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/title01"
            android:textSize="18sp"
            android:gravity="center"
            android:textStyle="bold"
            android:textColor="#61380B"
            android:background="@drawable/shape_title" />
        <TextView
            android:id="@+id/author01"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/author01"
            android:textSize="15sp"
            android:textColor="#22741C" />
        <TextView
            android:id="@+id/body01"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/body01"
            android:singleLine="true"
            android:ellipsize="end"
            android:textSize="15sp"  />
    </LinearLayout>
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@drawable/shape_list"
        android:id="@+id/poem02"
        android:tag="02"
        android:clickable="true"
        android:onClick="displayPoem" >
 
        <TextView
            android:id="@+id/title02"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/title02"
            android:textSize="18sp"
            android:gravity="center"
            android:textStyle="bold"
            android:textColor="#61380B"
            android:background="@drawable/shape_title" />
        <TextView
            android:id="@+id/author02"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/author02"
            android:textSize="15sp"
            android:textColor="#22741C" />
        <TextView
            android:id="@+id/body02"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/body02"
            android:singleLine="true"
            android:ellipsize="end"
            android:textSize="15sp"  />
    </LinearLayout>
 
</LinearLayout>

 

 

| Activity_poem.xml 

<?xml version="1.0" encoding="utf-8"?>


    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=".Poem">
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/poem"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">
 
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/title"
            android:textSize="15sp"
            android:gravity="right"
            android:textColor="#22741c"
            android:layout_margin="20dp"
            android:layout_marginBottom="10dp" />
 
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/body"
            android:textSize="15sp"
            android:textColor="#61380B" />
 
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/author"
            android:textSize="15sp"
            android:gravity="right"
            android:textColor="#22741c"
            android:layout_margin="20dp"
            android:layout_marginBottom="10dp" />
    </LinearLayout>
</ScrollView>

 

| 결과 

 

[관련 게시글]

[Android] 안드로이드 스튜디오 설치 : https://seul96.tistory.com/58
[Android] 바람개비 회전 애니메이션 : https://seul96.tistory.com/62
[Android] 화면터치 시 이미지 따라오기 : https://seul96.tistory.com/310
[Android] 그림 글 배치 : https://seul96.tistory.com/63
[Android] 글의 목록 만들기 : https://seul96.tistory.com/311
[Android] manifests, java, res / 레이아웃 유형 : https://seul96.tistory.com/64
[Android] toast 배경색 변경 방법 + 색상표 : https://seul96.tistory.com/65
[Android] 계산기 구현 : https://seul96.tistory.com/66
[Android] 위치 배열 gravity linear layout relative layout 사용 : https://seul96.tistory.com/67
[Android] 액티비티 전환 intent 예시 + 4대 컴포넌트 : https://seul96.tistory.com/68
[Android] 이벤트 처리와 액티비티간 이동 : https://seul96.tistory.com/70
[Android] 리스트뷰 : https://seul96.tistory.com/79
[Android] 커스텀 리스트뷰 : https://seul96.tistory.com/80
[Android] 안드로이드 공공데이터(API) 사용하는 방법 : https://seul96.tistory.com/85
[Android] Padding/layout_margin, visibility 속성 : https://seul96.tistory.com/312
반응형

댓글