이번 예제는 회원가입 화면을 만드는 것입니다.

  1. 첫번째 화면에서 맨아래 Member Registration버튼을 누르면


  1. 다음과 같이 회원 가입 화면으로 이동하게 됩니다.



  1. 이 화면에서 아래와 같이 드롭다운 박스를 구현하고 라디오버튼도 구현합니다.



activity_register.xml

회원가입 화면을 구현하는 부분입니다.

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.example.kch.registration_v2.RegisterActivity">

    <LinearLayout
        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="vertical"
            android:background="@color/colorPrimary"
            >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Member Registration"
                android:textColor="#ffffff"
                android:textSize="30dp"
                android:textStyle="bold"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                />
        </LinearLayout>

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

            <EditText
                android:layout_width="280dp"
                android:layout_height="wrap_content"
                android:hint="ID"
                android:layout_gravity="center"
                android:padding="10dp"
                android:textColor="#ffffff"
                android:textColorHint="#ffffff"
                android:id="@+id/idText"
                android:drawableStart="@drawable/ic_person_black_24dp"
                android:drawableLeft="@drawable/ic_person_black_24dp"
                android:layout_marginTop="50dp"
                android:background="@color/colorPrimary"
                />

            <EditText
                android:inputType="textPassword"
                android:layout_width="280dp"
                android:layout_height="wrap_content"
                android:hint="PASSWORD"
                android:layout_gravity="center"
                android:padding="10dp"
                android:textColor="#ffffff"
                android:textColorHint="#ffffff"
                android:id="@+id/passwordText"
                android:drawableStart="@drawable/ic_lock_outline_black_24dp"
                android:drawableLeft="@drawable/ic_lock_outline_black_24dp"
                android:layout_marginTop="10dp"
                android:background="@color/colorPrimary"
                />

            <EditText
                android:inputType="text"
                android:layout_width="280dp"
                android:layout_height="wrap_content"
                android:hint="E-mail"
                android:layout_gravity="center"
                android:padding="10dp"
                android:textColor="#ffffff"
                android:textColorHint="#ffffff"
                android:id="@+id/emailText"
                android:drawableStart="@drawable/ic_mail_outline_black_24dp"
                android:drawableLeft="@drawable/ic_mail_outline_black_24dp"
                android:layout_marginTop="10dp"
                android:background="@color/colorPrimary"
                />
           
            <RadioGroup
                android:layout_width="280dp"
                android:layout_height="wrap_content"
                android:id="@+id/genderGroup"
                android:layout_gravity="center_horizontal"
                android:orientation="horizontal"
                android:layout_marginTop="10dp"
                >
                <RadioButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="female"
                    android:id="@+id/genderWoman"
                    android:layout_weight="1"
                    android:checked="true"
                    />

                <RadioButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="male"
                    android:id="@+id/genderMan"
                    android:layout_weight="1"
                    />
            </RadioGroup>

            <Spinner
                android:layout_width="280dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:id="@+id/majorSpinner"
                ></Spinner>

            <Button
                android:layout_width="280dp"
                android:layout_height="wrap_content"
                android:textSize="20dp"
                android:textStyle="bold"
                android:textColor="#ffffff"
                android:background="@color/colorPrimary"
                android:text="Register"
                android:layout_marginTop="10dp"
                android:layout_gravity="center"
                android:id="@+id/registerButton"
                />


        </LinearLayout>

    </LinearLayout>



</LinearLayout>


values/arrays.xml

다음은 스피너에 추가되는 값들입니다.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="major">
        <item>정보통신공학</item>
        <item>기계공학</item>
        <item>화학공학과</item>
    </string-array>


</resources>


LoginActivity.java

여기서 첫  로그인 화면에서 Member Registration버튼을 누르면  RegisterActivity로 가게 합니다.

package com.example.kch.registration_v2;

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

public class LoginActivity extends AppCompatActivity {

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

        TextView registerButton = (TextView)findViewById(R.id.registerButton);

        //버튼이 눌리면 RegisterActivity로 가게함
        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);
                LoginActivity.this.startActivity(registerIntent);
            }
        });

    }
}

 
RegisterActivity.java

다음과 같이 스피너에 데이터를 연결시켜주는 작업을 합니다.

package com.example.kch.registration_v2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class RegisterActivity extends AppCompatActivity {

    private ArrayAdapter adapter;
    private Spinner spinner;

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

        //스피너 객체 선언 및 리소스를 가져오는 부분
        spinner = (Spinner)findViewById(R.id.majorSpinner);
        adapter = ArrayAdapter.createFromResource(this, R.array.major, android.R.layout.simple_dropdown_item_1line);
        spinner.setAdapter(adapter);

    }
}




블로그 이미지

가카리

소프트웨어와 하드웨어 프로그래밍, 취업 및 직장생활 전문 블로그