android專案點餐app1:基礎功能:閃屏頁、登入頁面、註冊頁面

wealth_gold發表於2020-11-23

一、基礎功能:閃屏頁、登入頁面、註冊頁面

1、閃屏頁: SplashActivity

onCreate()方法 -> initView() -> initEvent()

public class SplashActivity extends AppCompatActivity {

    private Button mBtnSkip;
    private Handler mHandler = new Handler();
    private Runnable mRunnableToLogin = new Runnable() {
        @Override
        public void run() {
            toLoginActivity();
        }
    };
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        initView();
        initEvent();

        mHandler.postDelayed(mRunnableToLogin,3000); // 3秒後跳轉
    }

    private void initEvent() {
        mBtnSkip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mHandler.removeCallbacks(mRunnableToLogin); // 取消postDelayed方法
                toLoginActivity();
            }
        });
    }

    private void initView() {
        mBtnSkip = findViewById(R.id.id_btn_skip);
    }

    public void toLoginActivity(){
        Intent intent = new Intent(this,LoginActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacks(mRunnableToLogin); // 避免造成記憶體洩漏
    }
}
主題設定
<activity
          android:name=".SplashActivity"
          android:theme="@style/AppTheme_FullScreen">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

    <style name="AppTheme_FullScreen" parent="AppTheme">
        <item name="android:windowFullscreen">true</item>
    </style>
Button
android:background="@drawable/btn_bg_skip"

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <corners android:radius="8dp" />
            <solid android:color="#aae7bfa0" />
            <stroke android:width="1dp" android:color="#f56b09" />
        </shape>
    </item>
    <item>
        <shape>
            <corners android:radius="8dp" />
            <solid android:color="#AA444444" />
            <stroke android:width="1dp" android:color="#b6b1b1" />
        </shape>
    </item>
</selector>

2、LoginActivity

按鈕樣式抽離
<style name="Button_Login" parent="Widget.AppCompat.Button">
    <item name="android:background">@drawable/btn_bg_black</item>
    <item name="android:paddingTop">16dp</item>
    <item name="android:paddingBottom">16dp</item>
    <item name="android:textColor">#ffffff</item>
    <item name="android:textSize">18sp</item>
</style>

<Button
        android:id="@+id/id_btn_login"
        style="@style/Button_Login"
        android:text="確定" />

3、RegisterActivity

toolbar樣式設定

common_toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/id_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#000000"
    app:navigationIcon="@drawable/back"
    app:titleTextColor="#ffffff"/>
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);
    setUpToolbar();
    setTitle("手機註冊");
    initView();
    initEvent();
}

4、OrderActivity

5、BaseActivity

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 21(5.0)以後設定 狀態顏色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(0xFF000000);
        }
    }
    
    protected void setUpToolbar() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.id_toolbar);
        setSupportActionBar(toolbar);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

相關文章