java用户登录和密码:数据库确认
我正在创建一个应用程序,要求用户在进入主应用程序之前登录。当他点击“登录”按钮时,在允许用户进入应用程序之前,它应该检查确保电子邮件/用户名和密码正确,否则会给他们一个错误。我一直在努力想办法做到这一点。这是我的主要活动页面,显示主要登录屏幕
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:paddingBottom="@dimen/activity_vertical_margin"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
安卓:orientation="vertical">
<!-- Email Label -->
<TextView 安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:textColor="#372c24"
安卓:text="@string/Email"/>
<EditText
安卓:id="@+id/Email_enter"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:layout_marginBottom="20dip"
安卓:layout_marginTop="5dip"
安卓:hint="@string/Email_enter"
安卓:singleLine="true"
安卓:inputType="textEmailAddress"/>
<!-- Password Label -->
<TextView 安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:textColor="#372c24"
安卓:text="@string/edit_message2"/>
<EditText
安卓:id="@+id/Password_enter"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="5dip"
安卓:hint="@string/Password_enter"
安卓:inputType="textPassword"
安卓:singleLine="true" />
<!-- Login button -->
<Button 安卓:id="@+id/btnLogin"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="10dip"
安卓:text="@string/Login"
安卓:onClick="sendMessage"/>
<Button
安卓:id="@+id/query"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="@string/Query"
安卓:onClick="View arg0"/>
请暂时忽略“查询”按钮。 以下是java文件:
package com.example.awesomefilebuilder;
import 安卓.content.ContentValues;
import 安卓.content.Intent;
import 安卓.database.Cursor;
import 安卓.database.sqlite.SQLiteDatabase;
import 安卓.os.Bundle;
import 安卓.util.Log;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.View.OnClickListener;
import 安卓.view.ViewGroup;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.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()
安卓.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的主页。上课
如果需要其他页面,请告诉我。 提前谢谢!我真的很喜欢
# 1 楼答案
这是一个广泛的问题。您可能需要做的是,您必须检测用户何时首次运行应用程序。首次登录时,您可以要求用户输入用户名和密码。现在最简单(但可能不是最好)的方法是将其保存在
SharedPreferences
。然后,当用户下次访问应用程序时,您可以从SharedPreference
检查它,如果登录正确,则继续,否则显示错误在
sendMessage
函数中,您可以首先检查sharepreference中是否存储了任何内容,以及您用于用户名和密码的特定密钥。如果它没有被保存,那么用户是第一次来。然后开始一个新的意图,在那里你可以保存它。如果保存了一些内容,则可以从共享首选项中提取thme,并将这些值与输入的值进行比较现在将密码保存在
SharedPreference
中并不是一个好主意,因为它是在可以访问的根设备上保存的。您可以对密码使用哈希/加密见remember password in android app
SharedPreferences for login in android和
Best option to store username and password in android app
用于代码剪和其他逻辑