AndroidStudioでのSnakeMiniゲームの制作(1)ログインインターフェースの設計

今回は、AndroidプラットフォームでJavaを使った古典的な小さなゲームSnakeを作成するプロセス全体を説明します(レイアウトデザインは、主に使用される機能を実現するために比較的一般的です。もちろん、美しい背景やレイアウトがあれば、 xmlで変更することもできます)、このセクションでは主にSnakeのログインと登録について説明します。デフォルトのアカウントはadminで、パスワードのデフォルトは123456です。もちろん、これはmainacticityで変更できます。アイデアは非常に簡単です。activity_mainで.xml、TableLayoutを使用して関連するTextViewとEditTextを設定し、アカウントを配置します。パスワードとボタン、SharePreデータへのアクセスはMainActivity.javajiezheファイルで使用されます。特定のプロセスには次の4つのステップがあります。

SharedPreferencesは、軽量データを保存する最も簡単な方法です

1SharedPreferencesオブジェクトを取得します

2Editorオブジェクトを取得します

3エディターは、値を取得するためにストレージの書き込み/取得を実行します

4 commit editor.commit()

次に、アカウントのパスワードが正しい場合、インテントは次のインターフェースであるmain.xmlにジャンプします。正しくない場合、Toastは「アカウントまたはパスワードのエラー」を表示します。具体的なコードは次のとおりです。

アクティビティ、メイン、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();
        }

    }
}

以下のスクリーンショット:

 

 

さて、今日のSnakeのログインと登録のインターフェースはここにあります。今後、さまざまな機能を徐々に改善していきます。皆さんの注目、サポート、そして一緒に学ぶことを楽しみにしています!

おすすめ

転載: blog.csdn.net/Abtxr/article/details/123980406