Notice
Recent Posts
Recent Comments
올해는 머신러닝이다.
[안드로이드]아주 간단한 로그인화면 구현하기 본문
참으로 싼티난다..그래도 처음 플젝이니깐..ㅋ
기능구현
1.확인 클릭시 DB에서 ID조회
2.저장 클릭시 DB에 저장함.
학습목표
1.안드로이드 기본 UI구현
2.안드로이드 기본 DB접속 이해
3.안드로이드 개발 과정 아주 조금이해..==;
LoginExamAct.java
package com.test;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class LoginExamAct extends Activity implements OnClickListener{
/** Called when the activity is first created. */
private String txtId = null;
private String txtPwd = null;
private LoginDBHelper mDBHelper = null;
private ContentValues cv = null;
private String MEMBERID = "memid";
private String PWD = "pwd";
private SQLiteDatabase db = null;
private MyAlertDialog alert= null;
private EditText et = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mDBHelper = new LoginDBHelper(this);
// System.out.println("콘솔화면나옴?");
Log.i("tag insert","hello");
alert = new MyAlertDialog(this);
Button idBtn =(Button)findViewById(R.id.findId);
Button pwdBtn =(Button)findViewById(R.id.register);
txtId = (idBtn).getText().toString();
//txtPwd = (pwdEdit).getText().toString();
idBtn.setOnClickListener(this);
pwdBtn.setOnClickListener(this);
Log.i("KTH","here ok");
}
public void onClick(View view){
txtId = (et=(EditText)findViewById(R.id.entryId)).getText().toString();
txtPwd = ((EditText)findViewById(R.id.pwd)).getText().toString();
Boolean ans = false;
switch(view.getId()){
case R.id.findId :
ans = isExistMemID(txtId);
et.setText(ans+"");
break;
case R.id.register :
if(!isExistMemID(txtId)){
alert.setMessage("중복된ID가 있음");
}
db = mDBHelper.getWritableDatabase();
cv = new ContentValues();
cv.put(MEMBERID,txtId);
cv.put(PWD,txtPwd);
db.insert("member",null, cv);
break;
}
//alert.setMessage(txtId+","+txtPwd);
//alert.show();
}
public Boolean isExistMemID(String varID){
Boolean ans = false;
if(varID==null)
return ans;
db = mDBHelper.getWritableDatabase();
String sql = "select memId,pwd from member where memID='"+varID.trim()+"'";
Cursor cursor = db.rawQuery(sql, null);
if(cursor.getCount()==0){
ans = !ans;
}
return ans;
}
class MyAlertDialog extends AlertDialog.Builder{
MyAlertDialog(Context ctx){
super(ctx);
this.setNeutralButton("닫기", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//기본적으로 창이 닫히고, 추가 작업은 없다.
}
});
}
@Override
public AlertDialog.Builder setMessage(CharSequence msg){
super.setMessage(msg);
this.show();
return this;
}
}
public void doLogin(View btn){
/*
try {
txtId = ((EditText)findViewById(R.id.entryId)).getText().toString();
txtPwd = ((EditText)findViewById(R.id.pwd)).getText().toString();
Toast.makeText(this, getResult(txtId,txtPwd), Toast.LENGTH_SHORT);
switch(btn.getId()){
case R.id.findId :
db = mDBHelper.getWritableDatabase();
String query = "select memId,pwd from member where memId='"+txtId+"'";
Cursor cursor = db.rawQuery(query, null);
/*
if(cursor.isNull(1)){
Toast.makeText(this,"자료없음",Toast.LENGTH_SHORT);
}else{
Toast.makeText(this,"있음 : "+txtId,Toast.LENGTH_SHORT);
}
Toast.makeText(this, "체크중",Toast.LENGTH_SHORT);
break;
case R.id.register :
db = mDBHelper.getWritableDatabase();
cv = new ContentValues();
cv.put(MEMBERID,txtId);
cv.put(PWD,txtPwd);
db.insert("member",null, cv);
break;
default :
}
//Toast.makeText(this,getResult(txtId,txtPwd), Toast.LENGTH_SHORT).show();
} catch (Exception e) {
// TODO: handle exception
Log.i("error",e.toString());
}
*/
}
public String getResult(String id,String pwd){
StringBuffer resultStr = new StringBuffer();
resultStr.append("id=");
resultStr.append(id);
resultStr.append(",pwd=");
resultStr.append(pwd);
return resultStr.toString();
}
}
class LoginDBHelper extends SQLiteOpenHelper{
public LoginDBHelper(Context context){
super(context,"login",null,1);
}
public void onCreate(SQLiteDatabase db){
db.execSQL("create table member(_id INTEGER PRIMARY KEY AUTOINCREMENT,memId TEXT,pwd TEXT);");
}
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
db.execSQL("drop table if exists member");
onCreate(db);
}
}
2.main.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 로그인화면 -->
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:paddingTop="10px">
<TableRow>
<TextView
android:text="@string/login"
android:padding="10px"
/>
<EditText android:id="@+id/entryId"
android:layout_span="3"
android:padding="10px"
android:autoText="false"
/>
</TableRow>
<TableRow>
<TextView
android:text="@string/pwd"
android:padding="10px"
/>
<EditText android:id="@+id/pwd"
android:layout_span="3"
android:padding="10px"
android:autoText="false"
/>
</TableRow>
<TableRow>
<TextView
android:layout_span="2"
/>
<Button android:id="@+id/findId"
android:text="@string/ok"
/>
<Button android:id="@+id/register"
android:text="@string/cancel"
/>
</TableRow>
</TableLayout>
3.string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, LoginExamAct!</string>
<string name="app_name">LoginExam</string>
<string name="login">아이디:</string>
<string name="ok">검색</string>
<string name="cancel">저장</string>
<string name="pwd">비밀번호:</string>
</resources>
'직접만든어플모음' 카테고리의 다른 글
UDP 에코 서비랑 클라이언트 (0) | 2011.01.02 |
---|---|
[왕초보팁]서비스연결후 현재 보여지고 있는 엑티비티에 값 넘겨줄때 (5) | 2010.12.08 |
[왕초보시리즈3]간단한 탐색기 (14) | 2010.12.01 |
(왕초보시리즈1)그림메모어플 (3) | 2010.11.29 |
(왕초보시리즈2)매시간 알리미 (4) | 2010.11.29 |