반응형
MainActivity에서 Join IN 버튼을 누르면
정보를 입력하는 JoinActivity를 보여주고
JoinActivity에서 정보입력 완료후 Join 버튼을 누르면
MainActivity의 텍스트 뷰에 정보들을 보여줍니다.
체크되어 있지 않으면, product layout을
체크되어 있으면 advertise layout을 inflate 하는 예 입니다.
추가로 activity_join.xml과 JoinActivity.java가 필요합니다.
또한 Manifist.xml에서 JoinActivyt에 대한 내용을 추가해야 합니다.
---------------------------------------------------------------
MainActivity.java
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | package com.ex2_150519; import android.content.Intent; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends ActionBarActivity { static final int REQUEST_CODE = 1000; Button btnJoinin; TextView resultView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnJoinin = (Button) findViewById(R.id.btnJoinIn); btnJoinin.setOnClickListener(new joinBtnClickListener()); resultView = (TextView) findViewById(R.id.resultView); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } protected class joinBtnClickListener implements View.OnClickListener{ @Override public void onClick(View v) { Intent intent = new Intent(getApplicationContext(), JoinActivity.class); startActivityForResult(intent, REQUEST_CODE); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(RESULT_OK == resultCode){ String resultStr=""; for(String key:data.getExtras().keySet()){ resultStr += key + " : " + data.getExtras().getString(key) + '\n'; } resultView.setText(resultStr); } } } | cs |
---------------------------------------------------------------
JoinActivity.java
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | package com.ex2_150519; import android.content.Intent; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; public class JoinActivity extends ActionBarActivity { Button btnJoin; EditText etName, etPhone; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_join); btnJoin = (Button) findViewById(R.id.btnJoin); btnJoin.setOnClickListener(new joinBtnClickListener()); etName = (EditText) findViewById(R.id.etName); etPhone = (EditText) findViewById(R.id.etPhone); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_join, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } protected class joinBtnClickListener implements View.OnClickListener{ @Override public void onClick(View v) { Intent intent = new Intent(); intent.putExtra("phone", etPhone.getText().toString()); intent.putExtra("name", etName.getText().toString()); setResult(RESULT_OK, intent); finish(); } } } | cs |
---------------------------------------------------------------
activity_main.xml
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 | <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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/resultView" android:textSize="30dp" android:gravity="center" android:hint="Click 'Join in'" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Join in" android:id="@+id/btnJoinIn" android:layout_below="@+id/resultView" android:layout_centerHorizontal="true" android:layout_marginTop="155dp" android:textSize="30dp" /> </RelativeLayout> | cs |
---------------------------------------------------------------
activity_join.xml
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | <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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.ex2_150519.JoinActivity"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="52dp" android:id="@+id/linearLayout" android:paddingLeft="50dp" android:paddingRight="30dp"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Name : " android:id="@+id/textView" android:layout_weight="1.5" android:textSize="30dp" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/etName" android:layout_weight="1" android:textSize="30dp" android:hint="name" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/linearLayout" android:layout_centerHorizontal="true" android:id="@+id/linearLayout2" android:paddingLeft="50dp" android:paddingRight="30dp"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Phone : " android:id="@+id/textView3" android:layout_weight="1.5" android:textSize="30dp" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/etPhone" android:layout_weight="1" android:textSize="30dp" android:hint="phone" /> </LinearLayout> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Join" android:id="@+id/btnJoin" android:layout_below="@+id/linearLayout2" android:layout_centerHorizontal="true" android:layout_marginTop="56dp" android:textSize="30dp" /> </RelativeLayout> | cs |
마지막으로
아래의 내용을 Manifist의 application안에 추가해줍니다.
1 2 3 4 | <activity android:name=".JoinActivity" android:label="@string/title_activity_join" > </activity> | cs |
반응형
'Android' 카테고리의 다른 글
[Android GCM] 2. GCM 안드로이드 앱! (0) | 2015.07.10 |
---|---|
[Android GCM] 1. 준비 (0) | 2015.07.10 |
[Android] layout Inflater 예제 (0) | 2015.05.19 |
안드로이드 푸시 기능 구현 (4) - 결과 (0) | 2015.01.16 |
안드로이드 푸시 기능 구현 (3) - Nodejs 푸시 서버 (0) | 2015.01.14 |
댓글