我正在創建一個應用程序,需要用戶在進入主應用程序之前登錄。當他點擊「登錄」按鈕時,應該檢查以確保電子郵件/用戶名和密碼是正確的,然後讓用戶進入應用程序或給他們一個錯誤。我一直在努力弄清楚如何做到這一點。這是我的main_activity頁面,顯示主登錄屏幕。用戶登錄和密碼:數據庫構造
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<!-- Email Label -->
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#372c24"
android:text="@string/Email"/>
<EditText
android:id="@+id/Email_enter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_marginTop="5dip"
android:hint="@string/Email_enter"
android:singleLine="true"
android:inputType="textEmailAddress"/>
<!-- Password Label -->
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#372c24"
android:text="@string/edit_message2"/>
<EditText
android:id="@+id/Password_enter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:hint="@string/Password_enter"
android:inputType="textPassword"
android:singleLine="true" />
<!-- Login button -->
<Button android:id="@+id/btnLogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="@string/Login"
android:onClick="sendMessage"/>
<Button
android:id="@+id/query"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Query"
android:onClick="View arg0"/>
請忽略 「查詢」 按鈕現在。 這裏是java文件:
package com.example.awesomefilebuilder;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Base_Activity {
public final static String EXTRA_MESSAGE = "com.example.awesomefilebuilder.MESSAGE";
public final String TAG = "MainActivity";
public final String edit_message ="Username";
public static final String DATABASE_NAME = "data";
public static final String TABLE_NAME = "comments_table";
public static final String C_ID = "_id";
public static final String NAME = "name";
public static final String COMMENT = "comment";
public static final String EMAIL = "email";
public static final String TIME = "time";
public static final String PASSWORD = "password";
public static final int VERSION = 1;
View view;
SQLiteDatabase db;
DbHelper dbhelper;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
DbHelper dbhelper = new DbHelper(getApplicationContext());
db = dbhelper.getWritableDatabase();
view = inflater.inflate(R. layout.activity_main, container,false);
ContentValues cv = new ContentValues();
cv.put(DbHelper.NAME, NAME);
cv.put(DbHelper.COMMENT, COMMENT);
cv.put(DbHelper.EMAIL, EMAIL);
cv.put(DbHelper.PASSWORD, PASSWORD);
db.insert(DbHelper.TABLE_NAME, null, cv);
Button query = (Button) view.findViewById(R.id.query);
query.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
String [] columns = {DbHelper.NAME, DbHelper.COMMENT, DbHelper.EMAIL};
Cursor cursor = db.query(DbHelper.TABLE_NAME, null, null, null, null, null, null);
cursor.moveToFirst();
while(cursor.moveToNext())
{
String name = cursor.getString(cursor.getColumnIndex(DbHelper.NAME));
String comment = cursor.getString(cursor.getColumnIndex(DbHelper.COMMENT));
String password = cursor.getString(cursor.getColumnIndex(DbHelper.PASSWORD));
String email = cursor.getString(cursor.getColumnIndex(DbHelper.EMAIL));
Toast.makeText(getApplicationContext(), "Name = "+ name +"/nComment= "+ comment,Toast.LENGTH_SHORT).show();
}
cursor.close();
}
});
return view;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "onCreate");
}
@Override
public void onDestroy() {
super.onDestroy(); // Always call the superclass
// Stop method tracing that the activity started during onCreate()
android.os.Debug.stopMethodTracing();
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, HomePageMain.class);
EditText editText = (EditText) findViewById(R.id.Email_enter);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
Log.i(TAG, "sendMessage");
}
};
當用戶點擊登錄按鈕,我想要的應用,讓他們在或提供錯誤之前檢查數據。我如何在這裏做到這一點? 之後,如果數據檢查正確,我希望應用程序將用戶發送到名爲HomePageMain.class的下一個主頁面。
如果需要其他頁面,請告訴我。 在此先感謝!我真的不知道它。
環顧四周後,我想知道如果這個網站是一個好主意,用作模板:http://www.androidhive.info/2012/01/android-login-and-registration-with-php -mysql-and-sqlite /你覺得怎麼樣? – user2712795
如果你想涉及服務器端,那麼Ooo似乎是一個相當不錯的模板。我也會爲它加書籤。稍後再評論並告訴我它是如何發生的。 :) –
所以我得到了所有這一切,當我通過我的媽媽的機器人運行它我得到的錯誤,它已經意外停止。你認爲你可以快速看看可能是錯的嗎?我在eclipse中沒有錯誤,只有一些警告說某些變量沒有被使用。一旦我收到問題,我會發佈一個鏈接... – user2712795