Android SQLite 데이터베이스 연결 로그 인 기능 구현
레이아웃 파일
border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!--         -->
<!--  <solid android:color="#FFFFFF" />-->
<!--           -->
  <stroke
      android:width="0.01dp"
      android:color="#000" />
  <padding
      android:bottom="5dp"
      android:left="5dp"
      android:right="5dp"
      android:top="5dp" />
<!--    -->
  <corners android:radius="5dp" />
</shape> 
<?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=".MainActivity">
<LinearLayout
        android:padding="5dp"
        android:background="@drawable/border"
        android:orientation="vertical"
        android:layout_gravity="center_horizontal"
        android:layout_width="360dp"
        android:layout_height="112dp">
      <LinearLayout
          android:orientation="horizontal"
          android:layout_gravity="center_horizontal"
          android:layout_width="match_parent"
          android:layout_height="50dp">
        <ImageView
            android:layout_marginRight="15dp"
            android:layout_gravity="center_vertical"
            android:layout_width="30dp"
            android:layout_height="30dp" app:srcCompat="@drawable/usn" android:id="@+id/usn"/>
        <!-- android:background="@null"              -->
        <EditText
            android:singleLine="true"
            android:background="@null"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:inputType="text"
            android:hint="   "
            android:ems="10"
            android:id="@+id/username"/>
      </LinearLayout>
      <!--    -->
      <View
          android:layout_height="0.5dip"
          android:background="#686868"
          android:layout_width="match_parent"/>
      <LinearLayout
          android:orientation="horizontal"
          android:layout_gravity="center_horizontal"
          android:layout_width="match_parent"
          android:layout_height="50dp">
        <ImageView
            android:layout_marginRight="15dp"
            android:layout_gravity="center_vertical"
            android:layout_width="30dp"
            android:layout_height="30dp" app:srcCompat="@drawable/pwd" android:id="@+id/  "/>
        <EditText
            android:singleLine="true"
            android:background="@null"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:inputType="textPassword"
            android:hint="  "
            android:ems="10"
            android:id="@+id/password"/>
      </LinearLayout>
    </LinearLayout>
    <Button
        android:layout_gravity="center_horizontal"
        android:background="#EF8D89"
        android:layout_marginTop="20dp"
        android:text="    "
        android:onClick="userLogin"
        android:layout_width="360dp"
        android:layout_height="wrap_content" android:id="@+id/login"/>
        
</android.support.constraint.ConstraintLayout>MainActivity 클래스
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
  //       
  SQLiteDatabase db;
  //    ,     key
  public final static String MESSAGE_KEY="com.android2";
 @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    /**
     * (  )1、context MainActivity
     * 2、name     
     * 3、
     * 4、   
     */
    final DatabaseHelper databaseHelper = new DatabaseHelper(this,"emis.db",null,2);
    //         
    db = databaseHelper.getReadableDatabase();
    setContentView(R.layout.activity_main);
  }
  /*  */
  private void userLogin() {
    EditText et1 = findViewById(R.id.username);
    String username = et1.getText().toString();
    EditText et2 = findViewById(R.id.password);
    String password = et2.getText().toString();
    //   Cursor             
    Cursor cursor = db.rawQuery("SELECT * FROM users WHERE username=? AND password=?",new String[]{username,password});
    //        ,    
    if(cursor.getCount()>0){
      Intent intent = new Intent(this,ReceiveActivity.class);
      intent.putExtra(MESSAGE_KEY,username);
      startActivity(intent);
    }
    else{
      Toast.makeText(MainActivity.this,"        !",Toast.LENGTH_SHORT).show();
    }
  }
}ReceiveActivity 클래스 및 레이아웃
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ReceiveActivity"
>
  <TextView
      android:textSize="24dp"
      android:layout_gravity="center_vertical"
      android:id="@+id/output"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      />
</LinearLayout> 
package com.android02;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class ReceiveActivity extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_receive);
    //  intent  
    Intent intent = getIntent();
    // MESSAGE_KEY         
    String message = intent.getStringExtra(MainActivity.MESSAGE_KEY);
    // id  TextView
    TextView textView = findViewById(R.id.output);
    //  message
    textView.setText("  !"+message);
  }
}테스트:
 
 이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.