Do it! - 도전 05 : 로그인 화면과 메뉴 화면 전환하기 안드로이드

MainActivity, MenuActivity 만들어서 버튼 누를때 화면 전환.

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
package org.androidtown.mission05;

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.Toast;

public class MainActivity extends AppCompatActivity {
public static final int REQUEST_CODE_MENU = 101;

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

// button listner 설정
Button btnLogin = (Button)findViewById(R.id.button);
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// 메뉴 activity를 띄워준다.
Intent intent = new Intent(getApplicationContext(), MenuActivity.class);
startActivityForResult(intent, REQUEST_CODE_MENU);
}
});

Intent passedIntent = getIntent();
processIntent(passedIntent);
}

@Override
protected void onNewIntent(Intent intent) {
processIntent(intent);
super.onNewIntent(intent);
}

public void processIntent(Intent intent){
if(intent != null)
{
int iFromIdx = intent.getIntExtra("from", 0);

switch(iFromIdx)
{
case 1 :
Toast.makeText(this, "고객 관리에서 돌아옴", Toast.LENGTH_LONG).show();
break;
case 2 :
Toast.makeText(this, "매출 관리에서 돌아옴", Toast.LENGTH_LONG).show();
break;
case 3 :
Toast.makeText(this, "상품 관리에서 돌아옴", Toast.LENGTH_LONG).show();
break;
}
}
}
}



MenuActivity.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
package org.androidtown.mission05;

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

public class MenuActivity extends AppCompatActivity {
public static final int FROM_CUSTOMER = 1;
public static final int FROM_SALES = 2;
public static final int FROM_PRODUCT = 3;

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

public void onClickCustomer(View view) {
gotoMain(FROM_CUSTOMER);
finish();
}

public void onClickSales(View view) {
gotoMain(FROM_SALES);
finish();
}

public void onClickProduct(View view) {
gotoMain(FROM_PRODUCT);
finish();
}

private void gotoMain(int iFromIdx) {
// main으로 돌아간다.
Intent mainIntent = new Intent(getApplicationContext(), MainActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TOP );

mainIntent.putExtra("from", iFromIdx);
startActivity(mainIntent);
}
}


activity_main.xml

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
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="org.androidtown.mission05.MainActivity">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>


activity_menu.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
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
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="org.androidtown.mission05.MenuActivity">

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="109dp"
android:onClick="onClickCustomer"
android:text="@string/customer"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:onClick="onClickSales"
android:text="@string/sales"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button1" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:onClick="onClickProduct"
android:text="@string/product"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button2"
app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>


1 2 3 4 5 6 7



메모장