명시적 인텐트 (explicit intent)


명시적 인텐트 (explicit intent)



1. 메인 액티비티 레이아웃


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.progdv.explicit_intent.MainActivity">

<EditText
android:id="@+id/et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Write your name down!" />

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_toRightOf="@+id/et"
/>

</RelativeLayout>







2. 인텐트를 이용하여 실행시킬 액티비티 생성






3. 서브 액티비티의 레이아웃 xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.progdv.explicit_intent.SubActivity"
android:background="#000"
>

<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="#ffffff"
android:textSize="10pt"
/>


</RelativeLayout>







4. 메인 액티비티 클래스


package com.progdv.explicit_intent;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

EditText et;
Button btn;
String input;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

et = (EditText)findViewById(R.id.et);
btn = (Button)findViewById(R.id.btn);

btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
input = et.getText().toString();

Intent intent =
new Intent(MainActivity.this, SubActivity.class);
intent.putExtra("key", input);
startActivity(intent);
}
});



}
}


5. 서브 액티비티 클래스


package com.progdv.explicit_intent;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class SubActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub);

Intent intent = getIntent();
String str = intent.getExtras().getString("key");

TextView tv = (TextView)findViewById(R.id.tv);
tv.setText(str);


}
}








Posted by netyhobby
,