我正在创建一个需要用户登录才能进入主应用程序的应用程序。当他点击“登录”按钮时,它应该在允许用户进入应用程序之前检查以确保电子邮件/用户名和密码正确,否则会给他们一个错误。我一直很难弄清楚如何做到这一点。这是我的 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 的下一个主页。
如果需要任何其他页面,请告诉我。提前致谢!我真的很欣赏它。