안드로이드

안드로이드 프로그래밍 - 다용도 앱 만들기 10

IT의 큰손 2023. 1. 18. 18:44
728x90
  • 앱의 기능 설명
    • 1. 로그인 기능
    • 2. 메모장
    • 3. 달력
    • 4. 연락처
    • 5. 그림판
    • 6. 미니게임
    • 7. 계산기
    • 8. 스톱워치
    • 9. 가계부

 

  • 가계부

가계부

 

  • 가계부 xml 코드
더보기
<ScrollView 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=".MainActivity"
    android:background="#25E9FD35">


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


        <TextView
            android:id="@+id/timeView"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@drawable/bg_index"
            android:gravity="center"
            android:text="00:00:00:00"
            android:textColor="#FFD000"
            android:textSize="35dp"
            android:textStyle="bold" />


        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="20dp">

            <Button
                android:id="@+id/btn_start"
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:background="#00897B"
                android:text="시작"
                android:textColor="#FDD835"
                android:textStyle="bold" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/btn_record"
                    android:layout_width="0dp"
                    android:layout_height="70dp"
                    android:layout_marginRight="3dp"
                    android:layout_weight="1"
                    android:background="@drawable/bg_index"
                    android:text="기록"
                    android:textColor="#FDD835"
                    android:textStyle="bold"
                    android:visibility="invisible" />

                <Button
                    android:id="@+id/btn_pause"
                    android:layout_width="0dp"
                    android:layout_height="70dp"
                    android:layout_marginRight="3dp"
                    android:layout_weight="1"
                    android:background="@drawable/bg_index"
                    android:text="일시정지"
                    android:textColor="#FDD835"
                    android:textStyle="bold"
                    android:visibility="invisible" />

                <Button
                    android:id="@+id/btn_stop"
                    android:layout_width="0dp"
                    android:layout_height="70dp"
                    android:layout_weight="1"
                    android:background="@drawable/bg_index"
                    android:text="중지"
                    android:textColor="#FDD835"
                    android:textStyle="bold"
                    android:visibility="invisible" />
            </LinearLayout>

        </RelativeLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="20dp"
            android:text="기록"
            android:textColor="#000000"
            android:textSize="22dp" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="5dp"
            android:layout_marginRight="15dp"
            android:background="#1E88E5">

        </View>

        <TextView
            android:id="@+id/recordView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="15dp"
            android:text=""
            android:textColor="#000000"
            android:textSize="22dp" />


    </LinearLayout>


</ScrollView>
  • ScrollView, LinearLayout, RelativeLayout을 사용
  • EditText를 이용하여 지출명, 지출액을 입력 받음
  • 추가, 탐색, 합산 금액, 전체 조회 버튼

 

  • 가계부 java 코드
더보기
class DBHelper2 extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "gageboo.db";

    private static final int DATABASE_VERSION = 3;

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

    public void onCreate(SQLiteDatabase db2) {
        db2.execSQL("CREATE TABLE contacts ( _id INTEGER PRIMARY KEY" +
                " AUTOINCREMENT, name TEXT, tel TEXT, hapasn TEXT);");
    }

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

public class Activity10 extends AppCompatActivity {
    DBHelper2 helper;
    SQLiteDatabase db2;
    EditText edit_name, edit_tel;
    TextView edit_result, hapsan_result;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout10);
        helper = new DBHelper2(this);
        try {
            db2 = helper.getWritableDatabase();
        } catch (SQLiteException ex) {
            db2 = helper.getReadableDatabase();
        }
        hapsan_result = (TextView) findViewById(R.id.textView7);
        edit_name = (EditText) findViewById(R.id.name);
        edit_tel = (EditText) findViewById(R.id.money);
        edit_result = (TextView) findViewById(R.id.textView);
        Button b = (Button)findViewById(R.id.button3);
        b.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                hapsan_result.setText("100000");
            }
        });
    }

    public void insert(View target) {
        String name = edit_name.getText().toString();
        String tel = edit_tel.getText().toString();
        db2.execSQL("INSERT INTO contacts VALUES (null, '" + name + "', '" + tel
                + "');");
        Toast.makeText(getApplicationContext(), "성공적으로 추가되었음",
                Toast.LENGTH_SHORT).show();
        edit_name.setText("");
        edit_tel.setText("");
    }

    public void search(View target) {
        String name = edit_name.getText().toString();
        Cursor cursor;
        cursor = db2.rawQuery("SELECT name, tel FROM contacts WHERE name='"
                + name + "';", null);

        Toast.makeText(getApplicationContext(), ""+cursor.getCount() ,
                Toast.LENGTH_SHORT).show();

        while (cursor.moveToNext()) {
            String tel = cursor.getString(1);
            edit_tel.setText(tel);
        }
    }
    public void select_all(View target) {
        Cursor cursor;
        cursor = db2.rawQuery("SELECT * FROM contacts", null);

        String s="Id   Name   Money \r\n";
        while (cursor.moveToNext()) {
            s += cursor.getString(0) + "    ";
            s += cursor.getString(1) + "    ";
            s += cursor.getString(2) + "    \r\n";
        }
        edit_result.setText(s);


    }
    public void hapsan(View target) {
        Cursor cursor;
        cursor = db2.rawQuery("SELECT * FROM contacts", null);
        String s="Id   Name   Money \r\n";
        while (cursor.moveToNext()) {
            s += cursor.getString(0) + "    ";
            s += cursor.getString(1) + "    ";
            s += cursor.getString(2) + "    \r\n";
        }
        edit_result.setText(s);


    }
}
  • DBHelper를 통하여, gageboo.db라는 db를 생성한다.
  • TABLE에 접근하면, 기본키는 자동생성되고, 지출명과 지출액을 입력받는다.
  • 접근을 종료하면 테이블은 제거 된다.
  • insert가 눌리면, SQL의 INSERT INTO문이 실행되어 지출명과 지출액을 입력 받아 테이블에 저장한다.
  • search가 눌리면, Query에서 SELECT문을 이용하여 name을 입력 받고 해당 지출명과 지출액을 출력한다.
  • hapsan이 눌리면, Query에서 모든 지출명과 지출액을 출력하여 합산한다.
  • select_all이 눌리면, Query에서 SELECT *, 즉 모든 사항을 출력한다.

 

728x90