Android Studio - 重定向到另一个页面

时间:2017-03-16 12:21:47

标签: java android xml database

我已经制作了一个Android应用程序来登录数据库,我需要它在单击按钮时重定向到用户个人资料页面,到目前为止它只显示一条消息,表示登录成功然后确实没有其他的。我在下面的三个代码片段是应用程序的XML,同一页面的Java和BackgroundWorker,其中完成了许多基础工作。有人可以告诉我如何重定向到名为Activity_user_profile.xml的页面吗?

编辑回答:

我添加了一行:startActivity(new Intent(this,UserProfileActivity.class));对于我的onLogin方法,这允许我将它链接到我想要的页面

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="com.example.programmingknowledge.finalyearproject2017.MainActivity">

<Button
    android:text="Register"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btnRegister"
    android:onClick="OpenReg"
    android:layout_below="@+id/btnLogin"
    android:layout_alignLeft="@+id/btnLogin"
    android:layout_alignStart="@+id/btnLogin"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:id="@+id/et_doctor_number"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:hint="Doctor number "
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:inputType="number" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPassword"
    android:ems="10"
    android:id="@+id/et_Password"
    android:layout_below="@+id/et_doctor_number"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:hint="Password" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Login"
    android:id="@+id/btnLogin"
    android:onClick="OnLogin"
    android:layout_below="@+id/et_Password"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

</RelativeLayout>

主要活动:

package com.example.programmingknowledge.finalyearproject2017;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {

EditText EmailEt, PasswordEt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    EmailEt = (EditText) findViewById(R.id.et_doctor_number);
    PasswordEt = (EditText) findViewById(R.id.et_Password);
}

public void OnLogin(View view){
    String email = EmailEt.getText().toString();
    String password = PasswordEt.getText().toString();
    String type = "Login";

    BackgroundWorker backgroundWorker = new BackgroundWorker(this);
    backgroundWorker.execute(type, email, password);
    startActivity(new Intent(this,UserProfileActivity.class));
}

public void OpenReg(View view){
    startActivity(new Intent(this,Register.class));
}

}

Background Worker:
package com.example.programmingknowledge.finalyearproject2017;

import android.content.Context;
import android.os.AsyncTask;
import android.app.AlertDialog;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

/**
* Created by john on 25-Feb-17.
*/

public class BackgroundWorker extends AsyncTask<String, Void, String> {
    Context context;
    AlertDialog alertDialog;
    BackgroundWorker (Context ctx){
        context = ctx;
    }
    @Override
    protected String doInBackground(String... params) {
        String type = params[0];
        String login_url = "http://10.0.2.2/login.php";
        String register_url = "http://10.0.2.2/register.php";
        if (type.equals("login")){
            try {
                String email = params[1];
                String password = params[2];
                URL url = new URL(login_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "utf-8"));
                String post_data = URLEncoder.encode("email", "UTF-8")+"="+URLEncoder.encode(email, "utf-8")+"&"+URLEncoder.encode("password", "UTF-8")+"="+URLEncoder.encode(password, "utf-8");
                bufferedWriter.write(post_data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
                String result ="";
                String line ="";
                while ((line=bufferedReader.readLine())!=null){
                    result+=line;
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return result;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e){
                e.printStackTrace();
            }
        } else if(type.equals("register")){
            try {
                String patient_name = params[1];
                String check_in_date = params[2];
                String room_number = params[3];
                String bed_number = params[4];
                String notes = params[5];
                URL url = new URL(register_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "utf-8"));
                String post_data = URLEncoder.encode("patient_name", "UTF-8")+"="+URLEncoder.encode(patient_name, "utf-8")+"&"
                    + URLEncoder.encode("check_in_date", "UTF-8")+"="+URLEncoder.encode(check_in_date, "utf-8")+"&"
                    + URLEncoder.encode("room_number", "UTF-8")+"="+URLEncoder.encode(room_number, "utf-8")+"&"
                    + URLEncoder.encode("bed_number", "UTF-8")+"="+URLEncoder.encode(bed_number, "utf-8")+"&"
                    + URLEncoder.encode("notes", "UTF-8")+"="+URLEncoder.encode(notes, "utf-8");
                bufferedWriter.write(post_data);
                bufferedWriter.flush();
                bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
            String result ="";
            String line ="";
            while ((line=bufferedReader.readLine())!=null){
                result+=line;
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return result;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }
    }

    return null;
}

@Override
protected void onPreExecute() {
    alertDialog = new AlertDialog.Builder(context).create();
    alertDialog.setMessage("Login Status");
}

@Override
protected void onPostExecute(String result) {
    alertDialog.setMessage(result);
    alertDialog.show();
}

@Override
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
}

}

1 个答案:

答案 0 :(得分:0)

on on on onSostExecute,Call Intent to go your Userprofile Activity

@Override
protected void onPostExecute(String result) {
    alertDialog.setMessage(result);
    alertDialog.show();

activity.startActivity(new Intent(activity,UserProfile.class));
}