Activity만드는 방법 (일반적인) 

1. Activity를 상속받는 자바클래스 생성 
2. Manifest에 등록
3. ui를 작성할 xml파일 생성 
4. 3의 xml파일을 1에서 만든 클래스의 onCreate메소드에서 setContentView에 지정 

Fragment를 만드는 방법 (일반적인) 
1. fragment를 상속받는 자바클래스를 생성 
2. ui를 작성할 xml파일을 생성 
3. 2의 xml파일을 1에서 만든 클래스의 onCrea


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
public class MainActivity extends Activity {
   private Button btn1;
   private Button btn2;
   private Button btn3;
   private LinearLayout form;
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      btn1 = (Button) findViewById(R.id.main_btn1);
      btn2 = (Button) findViewById(R.id.main_btn2);
      btn3 = (Button) findViewById(R.id.main_btn3);
      form = (LinearLayout) findViewById(R.id.main_form);
      btn1.setOnClickListener(new View.OnClickListener() {
        
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Fragment f = new FirstFragment();
            FragmentManager fm = getFragmentManager();
            fm.beginTransaction().add(R.id.main_form, f).commit();
        }
    });
   }
   
}
cs


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
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="kr.mulcam.hui.p0728"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />
 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>
cs

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
<LinearLayout 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:orientation="vertical">
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <Button 
            android:id="@+id/main_btn1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="버튼1"
            />
        <Button 
            android:id="@+id/main_btn2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="버튼2"
            />
        <Button 
            android:id="@+id/main_btn3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="버튼3"
            />
    </LinearLayout>
        <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/main_form"
        >
        </LinearLayout>
</LinearLayout>
cs

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
@SuppressLint("NewApi")
public class FirstFragment extends Fragment{
    private Button btn;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
//        return super.onCreateView(inflater, container, savedInstanceState);
        
        View view = (View) inflater.inflate(R.layout.fragment_first, 
                container,false); // true면 바로 받는거 false면 지정만 해두는거
        //프레그먼트에서 우리가 직접박는게 아니고박을 준비만 해두면 프레그먼트가 적당한 상황에 addView해줄거임
        btn = (Button) view.findViewById(R.id.first_btn);
        btn.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(), "버튼눌림"0).show();
            }
        });
        return view;
    }
    
 
}
cs


' IOT 기반 응용 SW과정 > Android, Arduino' 카테고리의 다른 글

Day94 안드로이드 listview  (0) 2016.08.02
Day93 AlertDialog  (0) 2016.07.29
Day91  (0) 2016.07.27
Day90  (0) 2016.07.26
Day89 안드로이드 엑티비티 전환  (0) 2016.07.25

+ Recent posts