Android實例開發中登錄註冊界面的框架實現(android studio)

小項目框架

今天用QQ的時候想到了,不如用android studio 做一個類似於這樣的登錄軟件。當然QQ的實現的功能特別複雜,UI界面也很多,不是單純的一時新奇就可以做出來的。就是簡單的實現了一些功能,做了三個界面;1.登錄界面。2.註冊界面。3.登陸後的界面。

功能描述

登錄按鈕------按鈕實現跳轉到下一個界面,並且判斷輸入的賬號、密碼是否符合規則(不爲空),提示,登陸成功或失敗
註冊按鈕------按鈕實現跳轉到註冊界面

登錄界面
在這裏插入圖片描述
main_activity.xml

 <LinearLayout
        android:id="@+id/number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/iv"
        android:layout_centerVertical="true"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="15dp"
        android:background="#ffffff">
        <TextView
            android:id="@+id/tv_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="賬號"
            android:textColor="#000"
            android:textSize="20dp" />
        <EditText
            android:id="@+id/et_username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:background="@null"
            android:padding="10dp" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/number"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="#ffffff">
        <TextView
            android:id="@+id/tv_password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="密碼"
            android:textSize="20dp"
            android:textColor="#000" />
        <EditText
            android:id="@+id/et_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_toRightOf="@id/tv_password"
            android:background="@null"
            android:inputType="textPassword"
            android:padding="10dp" />
    </LinearLayout>
    <Button
        android:id="@+id/button_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/password"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="60dp"
        android:background="#3c8dc4"
        android:text="登錄"
        android:textColor="#ffffff"
        android:textSize="20dp" />
    <Button
        android:id="@+id/button_register"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/button_login"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="30dp"
        android:background="#b7585556"
        android:text="註冊"
        android:textColor="#ffffff"
        android:textSize="20dp" />

    <CheckBox
        android:checked="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="記住密碼"
        android:id="@+id/checkBox"
        android:layout_below="@+id/password"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"/>

註冊界面
確定註冊------按鈕實現註冊,判斷以上四個註冊信息是否符合規則,判斷兩次輸入密碼是否一樣,並且不爲空。並且顯示提示信息
返回登錄------按鈕實現跳轉到剛纔的登錄界面
在這裏插入圖片描述
main_activity.java


public class MainActivity extends AppCompatActivity {
    private EditText et_username;
    private EditText et_password;
    private EditText et_password2;
    private EditText et_mail;
    private Button btn_login;
    private Button btn_register;
    private CheckBox checkbox;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Map<String, String> userInfo = SaveInfo.getSaveInformation(this);
        if (userInfo != null) {
            et_username.setText(userInfo.get("username"));
            et_password.setText(userInfo.get("password"));
        }
        et_username =(EditText) findViewById(R.id.et_username);
        et_password =(EditText) findViewById(R.id.et_password);
        et_password2 =(EditText) findViewById(R.id.reg_password2);
        et_mail = (EditText) findViewById(R.id.reg_mail);
        checkbox = (CheckBox) findViewById(R.id.checkBox);
        btn_login =(Button) findViewById(R.id.button_login);
        btn_register =(Button) findViewById(R.id.button_register);
        btn_login.setOnClickListener(new MyButton());
        btn_register.setOnClickListener(new MyButton());
                    }
    public  class MyButton implements View.OnClickListener{
        @Override
        public void onClick(View view){
            String username =et_username.getText().toString().trim();
            String password =et_password.getText().toString().trim();
            switch (view.getId()) {
                //當點擊登錄按鈕時
                case R.id.button_login:
                    if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)){
                        Toast.makeText(MainActivity.this,"密碼或賬號不能爲空",Toast.LENGTH_SHORT).show();
                    } else {
                        if(checkbox.isChecked()){
                            //保存密碼的操作
                        }
                        Toast.makeText(MainActivity.this,"登錄成功",Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                            startActivity(intent);
                        }
                    break;
                //當點擊註冊按鈕事件時
                case R.id.button_register:
                    Intent intent = new Intent(MainActivity.this,RegisterActivity.class);
                    startActivity(intent);
                    break;

            }
        }
    }
                }

register_activity

    <TextView
        android:layout_marginTop="60dp"
        android:id="@+id/reg_number1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="賬號:"
        android:textColor="#000"
        android:textSize="20dp" />
    <EditText
        android:layout_alignBottom="@+id/reg_number1"
        android:layout_toRightOf="@+id/reg_number1"
        android:id="@+id/reg_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp" />
    <TextView
        android:id="@+id/reg_number2"
        android:layout_marginTop="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/reg_number1"
        android:padding="10dp"
        android:text="密碼:"
        android:textColor="#000"
        android:textSize="20dp" />
    <EditText
        android:layout_alignBottom="@id/reg_number2"
        android:layout_toRightOf="@+id/reg_number2"
        android:id="@+id/reg_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp" />
    <TextView
        android:id="@+id/reg_number3"
        android:layout_marginTop="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/reg_number2"
        android:padding="10dp"
        android:text="密碼:"
        android:textColor="#000"
        android:textSize="20dp" />
    <EditText
        android:layout_alignBottom="@id/reg_number3"
        android:layout_toRightOf="@+id/reg_number3"
        android:id="@+id/reg_password2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp" />
    <TextView
        android:id="@+id/reg_number4"
        android:layout_marginTop="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/reg_number3"
        android:padding="10dp"
        android:text="郵箱:"
        android:textColor="#000"
        android:textSize="20dp" />
    <EditText
        android:layout_alignBottom="@id/reg_number4"
        android:layout_toRightOf="@+id/reg_number4"
        android:id="@+id/reg_mail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="確定註冊"
        android:background="#74e674"
        android:id="@+id/reg_btn_sure"
        android:layout_marginTop="38dp"
        android:layout_below="@+id/reg_mail"
        android:layout_marginLeft="50dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="返回登錄"
        android:background="#f27758"
        android:id="@+id/reg_btn_login"
        android:layout_alignBottom="@id/reg_btn_sure"
        android:layout_toRightOf="@id/reg_btn_sure"
        android:layout_marginLeft="60dp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="賬號註冊"
        android:textSize="30dp"
        android:layout_marginTop="5dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

registeractivity.java

public class RegisterActivity extends AppCompatActivity {
    private EditText reg_username;
    private EditText reg_password;
    private EditText reg_password2;
    private EditText reg_mail;
    private Button reg_btn_sure;
    private Button reg_btn_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        reg_username = (EditText) findViewById(R.id.reg_username);
        reg_password = (EditText) findViewById(R.id.reg_password);
        reg_password2 = (EditText) findViewById(R.id.reg_password2);
        reg_mail = (EditText) findViewById(R.id.reg_mail);
        reg_btn_sure = (Button) findViewById(R.id.reg_btn_sure);
        reg_btn_login = (Button) findViewById(R.id.reg_btn_login);
        reg_btn_sure.setOnClickListener(new RegisterButton());
        reg_btn_login.setOnClickListener(new RegisterButton());
    }

    public class RegisterButton implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            String username = reg_username.getText().toString().trim();
            String password = reg_password.getText().toString().trim();
            String password2 = reg_password2.getText().toString().trim();
            String mail = reg_mail.getText().toString().trim();
            switch (v.getId()) {
                //註冊開始,判斷註冊條件
                case R.id.reg_btn_sure:
                    if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password) || TextUtils.isEmpty(password2) || TextUtils.isEmpty(mail)) {
                        Toast.makeText(RegisterActivity.this, "各項均不能爲空", Toast.LENGTH_SHORT).show();
                    } else {
                        if (TextUtils.equals(password, password2)) {
                            //執行註冊操作
                            SaveInfo.SaveInformation(RegisterActivity.this,username,password,password2,mail);
                            Toast.makeText(RegisterActivity.this,"註冊成功,請返回登錄",Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(RegisterActivity.this, "兩次輸入的密碼不一樣", Toast.LENGTH_SHORT).show();
                        }
                    }
                        break;
                        case R.id.reg_btn_login:
                            Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
                            startActivity(intent);
                            break;

                    }
            }
        }
    }

登錄成功界面創建一個佈局文件就可以了,寫上你想要的東西,我自己就是創建了一個佈局,什麼都沒有,所以就在這裏不寫了
在這裏因爲要做一個保存操作,所以創建了一個java工具類,其中定義了兩個方法,一個保存登錄名和密碼,一個負責調用保存的登錄名和密碼
saveinfo

public class SaveInfo {
    public static boolean SaveInformation(Context context, String username, String password, String password2, String mail) {
        try {
            FileOutputStream fos = context.openFileOutput("data.txt", Context.MODE_APPEND);
            fos.write(("用戶名:" + username + " 密碼:" + password + "郵箱:" + mail).getBytes());
            fos.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static Map<String, String> getSaveInformation(Context context) {
        try {
            FileInputStream fis = context.openFileInput("data.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
            String str = br.readLine();
            String[] infos = str.split("用戶名:"+"密碼:"+"郵箱:");
            Map<String, String> map = new HashMap<String, String>();
            map.put("username", infos[0]);
            map.put("password", infos[1]);
            fis.close();
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

示例圖片
註冊賬號
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
【評論需要解決的問題或者文章中的不恰當的地方,接受改正】

發佈了34 篇原創文章 · 獲贊 41 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章