Android studio case-start an activity interface

table of Contents

1. Create a project

Open Android studio, create a project named demo,
Insert picture description here
select a blank project, click next to
Insert picture description here
complete the project information, customize the path, select Java as the language, and click finish to
Insert picture description here
wait for the creation of the workspace (the first time will be a bit slow, the later will be faster) , The interface creation is as shown in the figure.
Insert picture description here
At this point, the project creation is complete. Next, let's see how to open the activity interface.

Second, start the activity interface

1. Create a new activity

In the Java file, create a new activity
Insert picture description here

Note: The newly created activity is under the com.example.demo file, do not select new in the mainactivity file.

Select the blank interface to
Insert picture description here
complete the relevant information, click the finish
Insert picture description here
file to view (xml file is the display file of the interface)
Insert picture description here

2. Add pictures

Find a picture in the computer file, which is used to start the activity.
Insert picture description here
Copy this picture and paste it under the mipmap file.
Insert picture description here
Click OK (the path is automatically configured) and
Insert picture description here
modify the picture signature
Insert picture description here
. View the result
Insert picture description here

3. Addition of code

Add controls in the activity_image_view.xml file

<ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@mipmap/background"/>

Insert picture description here
View the design and
Insert picture description here
place the button in the activity_main_xml file

 <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="显示图片"/>

Insert picture description here
View effect
Insert picture description here
Add button ID

android:id="@+id/showImageBtn"

Insert picture description here
Define related buttons and instantiate them

public class MainActivity extends AppCompatActivity {
    
    
    Button btn_showImage;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_showImage=(Button) findViewById(R.id.showImageBtn);
        btn_showImage.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View v) {
    
    
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,ImageView.class);
                startActivity(intent);
            }
        });
    }
}

Insert picture description here

4. Compile and run

Run after the code is added. The
Insert picture description here
effect is displayed.
Insert picture description here
Click the button to display the picture on the simulator.

Insert picture description here

Three, summary

It's relatively simple to start the activity interface, but I just started to do this aspect of the content, not very skilled, so it is a bit slow to do it. Practice makes perfect, just wait for more practice.

Guess you like

Origin blog.csdn.net/QWERTYzxw/article/details/115070463