main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
	
	<!-- 예약어@android:id/tabhost -->

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <AnalogClock
                        android:layout_width="100dp"
                        android:layout_height="100dp" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <DigitalClock
                        android:layout_width="100dp"
                        android:layout_height="100dp" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <Chronometer
                        android:layout_width="100dp"
                        android:layout_height="100dp" />
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>

    </TabHost>

</LinearLayout>
sub.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />

    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPassword" />


    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Auto Login" />


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>
manifast.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gusfree.tapview"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".TapViewActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="SubActivity"></activity>
    </application>

</manifest>
TabActivity.java
package com.gusfree.tapview;

import android.app.TabActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.AnalogClock;
import android.widget.TabHost;
import android.widget.TextView;

public class TapViewActivity extends TabActivity {

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //@android:id/tabhost
        //탭위젯에다가 제목등을 셋팅하고,
        //각각의 탭위젯과 LinearLayout을 연결해주기
        
        //1. TabHost를 불러오자 @android:id/tabhost
        //TabActivity 상속 받아서 가능한 메서드 
        TabHost tabHost = getTabHost();
        
        TabHost.TabSpec spec1 =tabHost.newTabSpec("태그1");
        TabHost.TabSpec spec2 =tabHost.newTabSpec("태그2");
        TabHost.TabSpec spec3 =tabHost.newTabSpec("태그3");
        TabHost.TabSpec spec4 =tabHost.newTabSpec("태그3");
        
        spec1.setIndicator("첫번째 스펙");
        spec2.setIndicator("",getResources().getDrawable(R.drawable.red));
        spec3.setIndicator(new AnalogClock(this));
        spec4.setIndicator("네번째");
        
        spec1.setContent(R.id.tab1);
        spec2.setContent(R.id.tab2);
        spec3.setContent(R.id.tab3);
        Intent intent =new Intent();
        intent.setClass(this,SubActivity.class);
        spec4.setContent(intent);
        
        tabHost.addTab(spec1);
        tabHost.addTab(spec2);
        tabHost.addTab(spec3);
        tabHost.addTab(spec4); 
        
        SharedPreferences pref=getSharedPreferences("login", MODE_PRIVATE);
        
        String id = pref.getString("id","noname");
        ((TextView)findViewById(R.id.textView1)).setText("안녕하세요! "+id+ "님");
    }
} 
SubActivity.java
package com.gusfree.tapview;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;

public class SubActivity extends Activity{
	
	EditText editText1,editText2;
	CheckBox checkbox;
	SharedPreferences preference;//공유 환경 변수
	
	@Override
	protected void onStart() {

		super.onStart();
		preference = getSharedPreferences("login", this.MODE_PRIVATE);
		
		setContentView(R.layout.sub);
		editText1=(EditText) findViewById(R.id.editText1);
		editText2=(EditText) findViewById(R.id.editText2);
		checkbox=(CheckBox) findViewById(R.id.checkBox1);
		
		findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				String id = editText1.getText().toString();
				String pw = editText2.getText().toString();
				boolean auto =checkbox.isChecked();
				
				Editor editor =preference.edit();//환경변수 에디트하기
				editor.putString("id", id);
				editor.putString("pw", pw);
				editor.putBoolean("auto",auto);
				
				editor.commit(); //save
				/*어플 지울 때까지 날아가지 않고 저장 됩니다.*/				
			}
		});
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		Log.i("onPause","onPause");
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		Log.i("onStop","onStop");
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.i("onDestroy","onDestroy");
	}
}

editText에 아이디랑 비번을 적으면 DDMS에 값이 저장이 된다.



저장된 값이 TextView에 자동으로 호출이된다.




'Android > 2012.04월 강좌' 카테고리의 다른 글

11일차 Samples for SDK 사용하기  (0) 2012.05.10
10일차 TouchEvent  (0) 2012.05.09
10일차 TabView  (0) 2012.05.09
10일차 PullParser  (1) 2012.05.09
10일차 XMLParser  (0) 2012.05.09

+ Recent posts