這篇我們來實作MainActivity部分
layout部分我設計了兩個Button做跳轉頁面,分別為開始、設置,並使用Guideline作綁定元件位置
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:background="#ab7557"
tools:context=".main.MainActivity">
<Button
android:id="@+id/btn_start"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/redbackground_blackline"
android:gravity="center"
android:text="開始"
android:textColor="@color/white"
android:textSize="30dp"
android:textStyle="bold"
app:backgroundTint="@null"
app:layout_constraintBottom_toTopOf="@+id/guideline27"
app:layout_constraintEnd_toStartOf="@+id/guideline25"
app:layout_constraintStart_toStartOf="@+id/guideline24"
app:layout_constraintTop_toTopOf="@+id/guideline26" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline24"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.2" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline25"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.8" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline62"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.1" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline26"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.3" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline27"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.45" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline52"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.65" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline53"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.8" />
<Button
android:id="@+id/btn_set"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/redbackground_blackline"
android:gravity="center"
android:text="設置"
android:textColor="@color/white"
android:textSize="30dp"
android:textStyle="bold"
app:backgroundTint="@null"
app:layout_constraintBottom_toTopOf="@+id/guideline53"
app:layout_constraintEnd_toStartOf="@+id/guideline25"
app:layout_constraintStart_toStartOf="@+id/guideline24"
app:layout_constraintTop_toTopOf="@+id/guideline52" />
<TextView
android:id="@+id/textView11"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/black"
android:gravity="center"
android:text="Chess Game"
android:textSize="30dp"
android:textColor="@color/white"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/guideline62"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
public class MainActivity extends AppCompatActivity {
private Button btn_start,btn_set;
private Intent intent;
private ArrayList musicList = new ArrayList();
private ArrayList colorList = new ArrayList();
private DialogData dialogData = new DialogData(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_start = findViewById(R.id.btn_start);
btn_set = findViewById(R.id.btn_set);
getLastPageData();
btn_start.setOnClickListener(v -> {
intent = new Intent(this, CreateJoinRoom.class);
intent.putIntegerArrayListExtra("musicData",musicList);
intent.putIntegerArrayListExtra("colorData",colorList);
startActivity(intent);
});
btn_set.setOnClickListener(v -> {
intent = new Intent(this, Setting.class);
intent.putIntegerArrayListExtra("musicData",musicList);
intent.putIntegerArrayListExtra("colorData",colorList);
startActivity(intent);
finish();
});
}
public void getLastPageData(){
musicList = getIntent().getIntegerArrayListExtra("musicData");
colorList = getIntent().getIntegerArrayListExtra("colorData");
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
dialogData.showExitDialog("確定退出?","確定","取消");
}
return false;
}
}
完成之後,我們可以到AndroidManifest中設置畫面鎖定為直向
<activity
android:name=".main.MainActivity"
android:exported="true"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
這樣我們基本的主畫面就完成了