Production of Snake Mini Game in AndroidStudio (1) Design of login interface

This time, I will explain the whole process of making a classic small game Snake with java on the Android platform (the layout design is relatively general, mainly to realize the functions used. Of course, if you have a beautiful background or layout, you can also modify it in xml), This section mainly explains the login and registration of Snake. The default account is admin and the default password is 123456. Of course, this can be modified in the mainacticity. The idea is very simple. In activity_main.xml, use TableLayout to set the relevant TextView and EditText to place the account. and password and button, the access to SharePre data is used in the MainActivity.javajiezhe file. The specific process has four steps as follows:

SharedPreferences is the easiest way to store lightweight data

1 Get the SharedPreferences object

2 Get the Editor object

3 The editor performs put/get storage to get the value

4 commit editor.commit()

Then, if the account password is correct, the Intent jumps to the next interface second, main.xml. If it is incorrect, Toast will prompt "Account or password error". The specific code is as follows:

activity,main,xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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"
    tools:context="com.example.greedy.MainActivity"
    >
    <ImageView
        android:background="@drawable/title4"/>

    <TextView />

    <TableRow>
        <TextView
            android:id="@+id/user"
            android:text="请输入你的账号:"
            android:textStyle="bold"
            android:textSize="20sp"

            android:layout_height="wrap_content"
            android:layout_weight="0.1"
            android:gravity="end"/>
        <EditText
            android:id="@+id/input1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:hint="" />
    </TableRow>

    <TableRow>
        <TextView
            android:id="@+id/password"
            android:text="请输入你的密码:"
            android:textStyle="bold"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.1"
            android:gravity="end"/>
        <EditText
            android:id="@+id/input2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"

            android:hint=" "/>
    </TableRow>

    <TableRow>
        <CheckBox
            android:id="@+id/remember_button"
            android:layout_width="wrap_content"
            android:layout_marginLeft="10dp"
            />
        <TextView
            android:id="@+id/remember_text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textStyle="bold"
            android:text="是否保存本次密码"/>
    </TableRow>
    <ProgressBar
        android:id="@+id/progress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        style="?android:attr/progressBarStyleHorizontal"
        android:max="250" />
    <Button
        android:id="@+id/login_button"
        android:text="点击注册进入"
        android:textSize="25sp"
        android:background="#add8e6"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</TableLayout>

second.main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/bj"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="欢迎来到贪吃蛇"
        android:gravity="center"
        android:textSize="30sp"
        />

    <ImageButton
        android:id="@+id/button_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="51dp"
        android:layout_marginTop="190dp"
        android:background="@drawable/circle1" />

    <ImageButton
        android:id="@+id/button_difficulty"
        android:background="@drawable/circle2"
        android:layout_marginTop="192dp"
        android:layout_marginLeft="235dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageButton
        android:id="@+id/button_music"
        android:background="@drawable/circle3"
        android:layout_below="@id/button_start"
        android:layout_marginLeft="51dp"
        android:layout_marginTop="120dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageButton
        android:id="@+id/button_about"
        android:background="@drawable/circle4"
        android:layout_below="@id/button_difficulty"
        android:layout_marginTop="120dp"
        android:layout_marginLeft="235dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />




</RelativeLayout>

MainActivity.java

package com.example.greedy;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;


public class MainActivity extends Activity implements OnClickListener{
    Button button;
    EditText edit1,edit2;
    CheckBox checkbox;
    ProgressBar bar;
    SharedPreferences pref;
    SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=(Button) findViewById(R.id.login_button);
        edit1=(EditText) findViewById(R.id.input1);
        edit2=(EditText) findViewById(R.id.input2);
        checkbox=(CheckBox) findViewById(R.id.remember_button);
        bar=(ProgressBar) findViewById(R.id.progress);
        pref= PreferenceManager.getDefaultSharedPreferences(this);
        boolean isRemember=pref.getBoolean("rem",false); //用于给是否保存密码赋值

        if(isRemember) {
            //将账号和密码设置到文本框中
            String account=pref.getString("account","");
            String password=pref.getString("password","");
            edit1.setText(account);
            edit2.setText(password);
            checkbox.setChecked(true);
        }
        button.setOnClickListener(this);
    }
    @Override
    public void onClick(View v){
       /* new Thread(new Runnable(){  //开启线程运行进度条
            @Override
            public void run() {
                for (int i = 0; i < 25; i++) {
                    int progress = bar.getProgress();
                    progress = progress + 10;
                    bar.setProgress(progress);
                }
            }
        }).start();*/

        String account=edit1.getText().toString();
        String password=edit2.getText().toString();
        if(account.equals("admin") && password.equals("123456")) {
            editor = pref.edit();
            if(checkbox.isChecked()) {
                editor.putBoolean("rem",true);
                editor.putString("account",account);
                editor.putString("password",password);
            }
            else {
                editor.clear();
            }
            editor.commit();
            Intent intent=new Intent(MainActivity.this,SecondActivity.class);
            startActivity(intent);

        }
        else{
            Toast.makeText(MainActivity.this,"账号或用户名错误",Toast.LENGTH_SHORT).show();
        }

    }
}

Screenshot below:

 

 

Well, today's Snake's login and registration interface is here. We will gradually improve various functions in the future. I look forward to everyone's attention, support and learning together!

Guess you like

Origin blog.csdn.net/Abtxr/article/details/123980406