Android火車票訂購軟件之註冊和文件儲存(2)

上次我們做了app啓動時的延時頁,這次我們來做登陸註冊的頁面。
在這裏插入圖片描述

xml代碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
              tools:context=".MainActivity"
              android:background="@drawable/login"
              android:orientation="vertical">

    <LinearLayout

            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">

        <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical"
                android:gravity="center">


        </LinearLayout>

        <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical"
                android:gravity="center">


        </LinearLayout>


    </LinearLayout>


    <LinearLayout

            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">

        <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical"
                android:gravity="center">

            <Button

                    android:id="@+id/login"
                    android:layout_marginTop="170dp"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="140dp"
                    android:layout_height="wrap_content"
                    android:background="@drawable/shape_green"
                    android:text="登 錄"
                    android:textColor="#FFFFFF"
                    android:textSize="22sp"


            />

        </LinearLayout>

        <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical"
                android:gravity="center">

            <Button
                    android:id="@+id/register"
                    android:layout_marginTop="170dp"

                    android:textSize="22sp"
                    android:layout_gravity="center_horizontal"
                    android:layout_height="wrap_content"
                    android:background="@drawable/shape"
                    android:text="注 冊"
                    android:textColor="#FFFFFF"
                    android:layout_width="140dp"/>
        </LinearLayout>


    </LinearLayout>


</LinearLayout>


Acticity類中只填加了兩個按鈕的點擊監聽事件,在這裏不寫了。
順便說一下,我做的按鈕樣式,如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle" >

    <!-- 填充的顏色 -->
    <solid android:color="#49bbf7" />

    <!-- 設置按鈕的四個角爲弧形 -->
    <!-- android:radius 弧形的半徑 -->
    <corners android:radius="13dip" />

    <!-- padding:Button裏面的文字與Button邊界的間隔 -->
    <padding
            android:bottom="10dp"
            android:left="10dp"
            android:right="10dp"
            android:top="10dp" />
</shape>

下面是註冊的頁面
在這裏插入圖片描述

一樣的,我來寫一下我的輸入框樣式設計代碼:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#80b9b5aa" />
    <stroke android:width="1dip" android:color="#fefefe" />
    <corners android:radius="20dp"/>
    <padding android:bottom="10dp"
             android:left="10dp"
             android:right="10dp"
             android:top="10dp"/>
</shape>

註冊頁面xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".LoginActivity"
                android:background="@drawable/login">


    <EditText
            android:id="@+id/phone_input"
            android:gravity="center"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/phonetext"
            android:layout_centerVertical="true"
            android:background="@drawable/shape_input"
            android:textSize="20sp"
    />

    <EditText
            android:layout_marginTop="37dp"
            android:id="@+id/password_input"
            android:gravity="center"

            android:layout_marginRight="15dp"
            android:background="@drawable/shape_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/password"
            android:inputType="textPassword"
            android:textSize="20sp" android:layout_below="@+id/phone_input"
            android:layout_alignStart="@+id/phone_input"/>

    <Button
            android:id="@+id/btn_login"
            android:layout_marginTop="60dp"
            android:textSize="20sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="注            冊"
            android:textColor="#FFFFFF"
            android:layout_below="@id/password_input"
            android:layout_centerHorizontal="true"
            android:background="@drawable/shape"/>


</RelativeLayout>


acticity代碼:

public class RegisterActivity extends Activity {

private EditText phone, password;
private Button reg;
//聲明一個SharedPreferences對象和一個Editor對象
private SharedPreferences preferences;
private SharedPreferences.Editor editor;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);
    phone = (EditText) findViewById(R.id.phone_input);
    password = (EditText) findViewById(R.id.password_input);
    reg = (Button) findViewById(R.id.btn_login);
    //獲取preferences和editor對象
    preferences = getSharedPreferences("UserInfo", MODE_PRIVATE);
    editor = preferences.edit();

    reg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String name = phone.getText().toString().trim();
            String passwords = password.getText().toString().trim();
            if(name!=null){
                if(name.length()!=11){
                    Toast.makeText(getApplicationContext(),"手機號格式錯誤",Toast.LENGTH_SHORT).show();
                }else {

                    if (passwords != null) {
                        editor.putString("userPhone", name);
                        editor.putString("userPassword", passwords);
                        editor.apply();
                        try {
                            saveFile(name + "@" + passwords);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                        startActivity(intent);
                        RegisterActivity.this.finish();

                    } else {
                        //密碼健壯性
                        Toast.makeText(getApplicationContext(), "密碼不能爲空", Toast.LENGTH_SHORT).show();
                    }
                }
            }else{
                //手機號健壯性
                Toast.makeText(getApplicationContext(),"手機號不能爲空",Toast.LENGTH_SHORT).show();
            }
        }
    });
}

private void saveFile(String str) throws Exception {

    try{
        String sdCardDir = Environment.getExternalStorageDirectory().getAbsolutePath();//獲取SDCard目錄
        File dir = new File(sdCardDir + "/" + getPackageName());
        if(!dir.exists())
        {
            if(!dir.mkdirs())
                throw new Exception("failure");
        }
        File saveFile = new File(dir,"userInfo.txt");
        if(!saveFile.exists())
            if(!saveFile.createNewFile())
                throw new Exception("failure");

        FileOutputStream outStream = new FileOutputStream(saveFile,true);

        outStream.write((str+"\r\n").getBytes());

        outStream.close();

    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

注意:這裏我們用了SharedPreferences對象,作用呢,就是儲存當前註冊所輸入的文本,存儲到sharedPreferences中,我們直接把它初始化到登錄頁面的輸入框中,這樣就不用用戶重新輸入了。

還有就是文件儲存用戶信息的代碼,我們首先寫入讀寫權限,上一篇有提到。Environment.getExternalStorageDirectory().getAbsolutePath();是獲取到sd卡的根目錄,一般的設備都有sd卡,我們利用包名來命名創建一個文件夾,創建一個txt來儲存用戶信息。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章