android login

Learned some basic common sense of Android today

Try to write an android login by yourself

The first is the interface

Since it is essential for the login interface, the account number and password and the login button are:


 Code:

<RelativeLayout 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="com.example.nono.MainActivity" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:layout_marginTop="82dp"
        android:layout_toRightOf="@+id/imageView2"
        android:src="@drawable/qq" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/imageView1"
        android:src="@drawable/logo" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText3"
        android:layout_alignRight="@+id/editText3"
        android:layout_below="@+id/editText3"
        android:layout_marginTop="15dp"
        android:hint="password"
        android:inputType="textPassword" />

        <Button
            android:id="@+id/login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/editText2"
            android:layout_alignRight="@+id/editText2"
            android:layout_below="@+id/editText2"
            android:layout_marginTop="29dp"
            android:background="#58A69F"
            android:text="login"
            android:textColor="#ffffff" />

        <EditText
            android:id="@+id/editText3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/imageView2"
            android:layout_below="@+id/imageView1"
            android:layout_marginTop="34dp"
            android:hint="QQ号"
            android:inputType="textPersonName" >

            <requestFocus />
        </EditText>

</RelativeLayout>

 To log in, you need to compare the entered user name and password with the data in the database. If the verification is successful, you can enter the next interface.

 

The method implemented is mainly to monitor the button and jump between the two activities

Code part:

 

package com.example.nono;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
    private EditText editText3,editText2;
    private Button login;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		setListener();
	}
	private void initView() {
		editText3 = (EditText) super.findViewById(R.id.editText3);
		editText2 = (EditText) super.findViewById(R.id.editText2);
		login = (Button) super.findViewById(R.id.login);
    }
    private void setListener(){
    	login.setOnClickListener(new OnClickListener() {
    		 public void onClick(View v) {
                if (TextUtils.isEmpty(editText3.getText())) {
                    Toast.makeText(getApplicationContext(), "QQ number cannot be empty", Toast.LENGTH_SHORT).show();
                    return;
                }
                else if(TextUtils.isEmpty(editText2.getText()))
                {
                	Toast.makeText(getApplicationContext(), "Password cannot be empty", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(editText3.getText().toString().equals("123")&&editText2.getText().toString().equals("123")){
                login();}
                else
                	{
                	Toast.makeText(getApplicationContext(), "Incorrect username or password", Toast.LENGTH_SHORT).show();
                    return;
                	}
                	}
    	});
    	}
    private void login(){
    	Intent intent=new Intent();
    	intent.setClass(MainActivity.this, second.class);
    	MainActivity.this.startActivity(intent);
    }
    
}

 

initView() This method is equivalent to doing an initialization, binding the corresponding input box and button to xml

login.setOnClickListener is to monitor the login button. After clicking the login button, take corresponding measures for the state of the input box, and call the login() method to jump when the user name and password are entered correctly.

The jump is actually a jump between two activities, use the intent to connect the two activities, and use the startactivity to perform the jump

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326145906&siteId=291194637