본문 바로가기
Android/그림으로 쉽게 설명하는 안드로이드 프로그래밍 개정6판

[Android/그림으로 쉽게 설명하는 안드로이드 프로그래밍 개정6판]그림으로 쉽게 설명하는 안드로이드 프로그래밍(개정6판) 연습문제 12장

by starfish22 2022. 12. 3.
728x90

정답이 아닐 수 있습니다. 오류가 있으면 알려주세요ㅎ

 

1. 1번 내부 저장소

 

2. 2번 외부 저장소

 

3. 4번 /sdcard

 

4. 1번 getFilesDir()

 

5. 2번 Device File Explorer

 

6. 1번 getExternalStorageState()

 

7.

activity_main.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=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:gravity="center"
        android:text="로그인 화면"
        android:textColor="#5555CC"
        android:textSize="40dp"
        android:textStyle="italic" />

    <EditText
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="이메일 입력"
        android:textColor="@color/black" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:hint="패스워드 입력"
        android:textColor="@color/black" />

    <Button
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="20dp"
        android:onClick="login"
        android:text="로그인" />

    <Button
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:backgroundTint="#FF9900"
        android:onClick="signup"
        android:text="회원등록" />

</LinearLayout>

 

MainActivity.java

package kr.co.company.ch12_2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

class DBHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "mycontacts.db";
    private static final int DATABASE_VERSION = 2;

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE contacts ( email TEXT, password TEXT);");
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS contacts");
        onCreate(db);
    }
}

public class MainActivity extends AppCompatActivity {
    DBHelper helper;
    SQLiteDatabase db;
    EditText email, password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        email = findViewById(R.id.email);
        password = findViewById(R.id.password);
        helper = new DBHelper(this);
        try {
            db = helper.getWritableDatabase();
        } catch (SQLiteException ex) {
            db = helper.getReadableDatabase();
        }
    }

    public void login(View v) {
        String email_str = email.getText().toString();
        String password_str = password.getText().toString();
        Cursor cursor;
        cursor = db.rawQuery("SELECT email, password FROM contacts WHERE email='" + email_str + "';", null);
        while (cursor.moveToNext()) {
            String tmp = cursor.getString(1);
            if (tmp.equals(password_str)) {
                Toast.makeText(getApplicationContext(), "로그인 성공", Toast.LENGTH_SHORT).show();
                return;
            }
        }
        Toast.makeText(getApplicationContext(), "다시 입력해주세요", Toast.LENGTH_SHORT).show();
    }

    public void signup(View v) {
        String email_db = email.getText().toString();
        String password_db = password.getText().toString();
        db.execSQL("INSERT INTO contacts VALUES ('" + email_db + "', '" + password_db + "');");
        Toast.makeText(getApplicationContext(), "회원가입 완료", Toast.LENGTH_SHORT).show();
    }
}

 

결과

728x90

댓글