1. Process
1. Configure Mainactive.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@drawable/first"
android:orientation="vertical"
>
<TextView
android:layout_marginTop="250dp"
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="危险!!"
android:textAlignment="center"
android:textColor="#C75450"
android:textSize="50dp" />
<Button
android:id="@+id/button7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="坚持骑行" />
</LinearLayout>
2. Create a new active and set the file name to nextactive
3. Configure the activity_nextactive.xml code file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@drawable/second"
android:orientation="vertical"
>
<TextView
android:layout_marginTop="250dp"
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="危险难免"
android:textAlignment="center"
android:textColor="#C75450"
android:textSize="50dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请注意驾驶交通安全!!!!"
android:textSize="20dp"
android:textColor="#C75450"
android:textAlignment="center"/>
</LinearLayout>
4. Configure the MainActive file
package com.example.tiaozhuan;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView(){
btn = (Button) findViewById(R.id.button7);
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent intent = new Intent(MainActivity.this, nextactive.class);
startActivity(intent);
}
});
}
}
2. Display
image display
Show results
3. Summary
1.protected void onCreate(Bundle savedInstanceState) Bundle savedInstanceState是什么?
Bundle savedInstanceState
parameter is an optional parameter ofonCreate
the method . It allows you to save the state of your activity, which can be restored in cases such as rotating the screen or starting another activity. If an activity needs to preserve some information between destruction and re-creation, it can store that information in a Bundle object, which can then be passed toonCreate
the method
2. @Override是什么?
@Override
is an annotation used in Java to indicate that a method overrides a method in a superclass. Using@Override
annotations can help us detect errors, such as when we try to override a method that does not exist in the parent class, the compiler will prompt an error. Also, it helps in code readability and maintainability.
3. Function methods used
1. onCreate() - the function called when creating the Activity, to initialize the layout and view of the Activity
2. initView() - the function to initialize the control, including obtaining the control object, setting the listener, etc.
3. onClick() - the processing of the button click event Function, judge whether it is correct according to the input content, if it is correct, open the next page, otherwise, an error message will pop up.
4. getIntent() - get the Intent object to start other activities
5. setContentView() - set the layout file of the activity
6. findViewById() - get the control object according to the ID of the control
7. startActivity() - start the next activity