java - 当用户登录时,它会导航到主页

标签 java android xml authentication

我只是想知道是否有人可以帮助我。我创建了一个登录页面,要求用户输入用户名和密码。一旦详细信息正确,它应该移动到我的主页吗?我只是想知道我该怎么做。先感谢您 这是我的登录 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#808080"
    android:orientation="vertical" >

    <!-- Login Title -->

    <TextView
        android:layout_width="222dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="20dp"
        android:gravity="center_horizontal"
        android:text="@string/login_title"
        android:textColor="#FFFF00"
        android:textSize="24sp" />

    <!-- Login Image -->
    <!-- layout for Username -->

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="260dp"
        android:layout_marginTop="-75dp"
        android:src="@drawable/logov2" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="60dp"
        android:gravity="center_horizontal"
        android:text="@string/pleaselogin"
        android:textColor="#FFFFFF"
        android:textSize="20sp" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal"
        android:padding="10dp" >

        <TextView
            android:layout_width="60dp"
            android:layout_height="match_parent"
            android:layout_marginTop="0dp"
            android:layout_weight="0.83"
            android:text="@string/username"
            android:textColor="#FFFF00"
            android:textSize="24sp" />

        <EditText
            android:id="@+id/username1"
            android:layout_width="200dp"
            android:layout_height="30dp"
            android:background="#FFFFFF"
            android:ems="10"
            android:inputType="text"
            android:textSize="24sp" >

            <requestFocus />
        </EditText>
    </LinearLayout>

    <!-- layout for Password -->

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="67dp"
        android:layout_marginTop="20dp"
        android:orientation="horizontal"
        android:padding="10dp" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="0dp"
            android:layout_weight="1.0"
            android:text="@string/password"
            android:textColor="#FFFF00"
            android:textSize="24sp" />

        <EditText
            android:id="@+id/password1"
           android:layout_width="200dp"
            android:layout_height="30dp"

            android:background="#FFFFFF"
            android:inputType="textPassword" />
    </LinearLayout>

    <Button
        android:id="@+id/login1"
        android:layout_width="150sp"
        android:layout_height="50dp"

        android:layout_marginLeft="200dp"
        android:background="#FF0000"
        android:text="@string/login"
        android:textColor="#FFFFFF"
        android:textSize="28sp" />

</LinearLayout>

登录类

package com.example.workplease;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.workplease_v2.R;
public class LoginActivity extends Activity {
      String userName, passWord;
      EditText username, password;
      Button login;    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        // UI elements gets bind in form of Java Objects
        username = (EditText)findViewById(R.id.username1);
        password = (EditText)findViewById(R.id.password1);
        login = (Button)findViewById(R.id.login1);
        // now we have got the handle over the UI widgets
        // setting listener on Login Button
        // i.e. OnClick Event
        login.setOnClickListener(loginListener);  
    }
    private OnClickListener loginListener = new OnClickListener() {
      public void onClick(View v) {
//getting inputs from user and performing data operations

            if(username.getText().toString().equals("test") &&
                        password.getText().toString().equals("test")){
// responding to the User inputs
                  Toast.makeText(getApplicationContext(), "Login Successfully !!!", Toast.LENGTH_LONG).show();      
            }else
                  Toast.makeText(getApplicationContext(), "Login Not Successful !!!", Toast.LENGTH_LONG).show();                           
      }
    };
}

主页 XML

<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:background="#808080"
    android:orientation="vertical"
    tools:context=".Home" >

    <TextView
        android:id="@+id/title"
        android:layout_width="220dp"
        android:layout_height="100dp"
        android:layout_marginLeft="45dp"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="@string/description"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#FFFF00"
        android:textSize="28sp" />

    <ImageButton
        android:id="@+id/exercise"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="70dp"
        android:layout_marginTop="40dp"
        android:src="@drawable/exercisesv2"
        android:background="@null" />

    <ImageButton
        android:id="@+id/routines"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="70dp"
        android:layout_marginTop="22dp"
        android:src="@drawable/routines"
        android:background="@null" />

    <ImageButton
        android:id="@+id/logbook"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="70dp"
        android:layout_marginTop="22dp"
        android:src="@drawable/logbook"
        android:background="@null" />

    <ImageButton
        android:id="@+id/qrscanner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="70dp"
        android:layout_marginTop="22dp"
        android:src="@drawable/qrscanner1"
        android:background="@null" />

    <TextView
        android:id="@+id/Copyright"
        android:layout_width="206dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="260dp"
        android:layout_marginTop="70dp"
        android:text="@string/copyright"
        android:textColor="#FFFF00"
        android:textSize="14sp"
        android:background="@null" />

    <ImageView
        android:id="@+id/Logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="260dp"
        android:layout_marginTop="-550dp"
        android:src="@drawable/logov2" />

</LinearLayout>

家庭课

package com.example.workplease;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import com.example.workplease_v2.R;

public class Home extends Activity {
    ImageButton imageButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        AddListenerOnButton();
        AddListenerOnButton1();
    }

    public void AddListenerOnButton(){
        final Context context = this;

        imageButton = (ImageButton) findViewById(R.id.exercise);

        imageButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent(context, Exercises.class);
                            startActivity(intent);  
            }
        });
    }
    public void AddListenerOnButton1(){
        final Context context = this;

        imageButton = (ImageButton) findViewById(R.id.routines);

        imageButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent(context, Routines.class);
                            startActivity(intent);  
            }
        });
    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

最佳答案

要启动一个新的Activity,您可以调用startActivity(Intent Intent)

 Intent intent = new Intent(this, Home.class);
 startActivity(intent);

因此您的 loginListener 按钮 OnClickListener 将执行此操作:

 public void onClick(View v) {
    if(username.getText().toString().equals("test") &&
                          password.getText().toString().equals("test")){
        // responding to the User inputs
        Toast.makeText(getApplicationContext(), "Login Successfully !!!",  
                                      Toast.LENGTH_LONG).show();     
        Intent intent = new Intent(LoginActivity.this, Home.class);
        startActivity(intent); 
    }else{
        Toast.makeText(getApplicationContext(), "Login Not Successful !!!", 
                                      Toast.LENGTH_LONG).show();   
    }
}

关于java - 当用户登录时,它会导航到主页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22254678/

相关文章:

java - 方法中的异常错误未使用异常

java - Android 多种实现

android - 有没有办法检索 MediaCodec 实例的限制?

asp.net - 使用 ASP.NET 如何从网站读取外部 XML?

xml - Perl XML::Parser - 如何访问一个简单的解析树?

java - 如何在服务类中设置ContentView以在打开时保护应用程序

java - for 循环第一次运行时什么都不做?可能是编程逻辑错误?

java - 在 Kotlin 中装饰方法集的惯用方法是什么

android - hive 上的 db4o Db4oException : File format incompatible

对错误命名空间的 .net xml 模式验证