1. 程式人生 > >Android--簡單登入頁面

Android--簡單登入頁面

登入(activity_main.xml )--->if(名稱==“abc” && pwd=="123")----->跳轉到登入成功頁面(activity_two.xml)

activity_main.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:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="歡迎來到我的登入介面"
            android:textSize="30sp"
            android:textColor="#603700"
            android:layout_gravity="top"
            android:gravity="center"

            android:id="@+id/textView" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">
        <TextView
            android:layout_width="100sp"
            android:layout_height="wrap_content"
            android:text="賬號"
            android:textSize="30sp"
            android:textColor="#603700"
            android:gravity="left"

            android:id="@+id/textView1" />
        <EditText
            android:layout_width="200sp"
            android:layout_height="wrap_content"
            android:text=""
            android:id="@+id/zhangHu"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
       android:orientation="horizontal">
        <TextView
            android:layout_width="100sp"
            android:layout_height="wrap_content"
            android:text="密碼"
            android:textSize="30sp"
            android:textColor="#603700"
            android:gravity="left"

            android:id="@+id/textView2" />
        <EditText
            android:layout_width="200sp"
            android:layout_height="wrap_content"
            android:text=""
            android:id="@+id/miMa"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">
        <Button
            android:text="重置"
            android:layout_width="100sp"
            android:layout_height="wrap_content"
            android:id="@+id/button" />
        <Button
            android:text="提交"
            android:layout_width="100sp"
            android:layout_height="wrap_content"

            android:id="@+id/button1" />

    </LinearLayout>




</LinearLayout>

 MainActivity.java

package com.example.administrator.test;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {
    Button login, cacel;
    EditText user, pass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); //放在前面   先載入佈局 再獲取下面的值
        login =(Button) findViewById(R.id.button1);
        cacel = (Button)findViewById(R.id.button);
        user = (EditText)findViewById(R.id.zhangHu);
        pass = (EditText)findViewById(R.id.miMa);

        //匿部類
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = user.getText().toString();
                String pwd = pass.getText().toString();
                if(name.equals("abc") && pwd.equals("123")){
                    Intent intent = new Intent(MainActivity.this, TwoActivity.class);//跳轉
                     startActivity(intent);


                }else{
                    user.setText("");
                    pass.setText("");
                    //彈出對話方塊
                    Toast.makeText(MainActivity.this, "使用者名稱或密碼錯誤", Toast.LENGTH_LONG).show(); //引數:上下文、要顯示的內容、要顯示的時間
                }
            }
        });




    }

    //內部類
    class MyListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {

        }
    }


}

 TwoActivity.java

package com.example.administrator.test;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class TwoActivity extends AppCompatActivity {

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

activity_two.xml

<?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_two"
    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.administrator.test.TwoActivity">

    <TextView
        android:text="登入成功"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="99dp"
        android:layout_marginStart="99dp"
        android:layout_marginTop="168dp"
        android:id="@+id/textView3" />
</RelativeLayout>