Create a simple project using Android studio

When I first started to learn Android development, I downloaded Android studio, but I didn't know how to start. Now I am familiar with how to use this software for Android development through a simple small project.

  • Prerequisite: Download Android studio and configure the relevant environment.

First, let's introduce the three types of files that need to be modified in the Android development process:

  • Various activity files located under the java package: realize the interaction between the user and the software, mainly implemented by java code.
  • The .xml file under the res package: the layout file in the layout package is the view displayed on the Android interface, and the drawable contains the style of a control, which can be called directly in the layout file after it is designed.
  • AndroidManifest.xml: is the manifest file in the Android application, all activity files need to be declared in this before they can be used.

Let's first look at this project, there are only four simple controls: text display, text input, "enlarge" button and "modify button", you can click "enlarge" to make the upper text font larger, or you can enter the desired text in the text input. what you want to show, then click Edit to update what the text above says.
insert image description here
The following is achieved through Android studio:

  • Create a new project: Click Start a new Android Studio project and select Empty Activity.
  • Edit the automatically generated activity_main.xml file, click the Text option below to edit the code (I use RelativeLayout for the layout here)
 <?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">

    <TextView
        android:id="@+id/tvx"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="30dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="200dp"/>

    <Button
        android:id="@+id/button"
        android:layout_width="150dp"
        android:layout_height="60dp"
        android:background="@drawable/bg_btn4"
        android:text="放大"
        android:textSize="30dp"
        android:layout_marginTop="350dp"
        android:layout_centerHorizontal="true"
        android:onClick="bigger">
    </Button>

    <EditText
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:hint="请输入要显示的字符"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="500dp"
        android:textSize="20dp">
    </EditText>

    <Button
        android:id="@+id/confim"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_toRightOf="@id/name"
        android:layout_marginTop="500dp"
        android:layout_marginLeft="10dp"
        android:background="@drawable/bg_btn4"
        android:text="修改"
        android:textSize="25dp"
        android:onClick="display">
    </Button>

</RelativeLayout>
  • Create a new xml file under drawable (here I name it bg_btn4.xml), which is the style of the button control
 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <solid android:color="#03363C"/>
            <corners android:radius="25dp"/>
        </shape>
    </item>
    <item android:state_pressed="false">
        <shape>
            <solid android:color="#B7E6FA"/>
            <corners android:radius="25dp"/>
        </shape>
    </item>
</selector>
  • Edit the auto-generated MainActivity.java file
package com.example.changesize;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
    
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    int size=30;
    public void bigger(View v){
    
     //放大按钮对应的onclick响应
        TextView txv;
        txv=(TextView)findViewById(R.id.tvx); //根据id找到对应的text对象
        txv.setTextSize(++size);//修改对象的字符大小size
    }
    public void display(View v){
    
     //修改按钮对应的响应
        EditText name=(EditText)findViewById(R.id.name);//根据id找到对象并进行接下来的操作
        TextView text2=(TextView)findViewById(R.id.tvx);
        text2.setText(name.getText().toString());//设置字符

    }
}
  • After completing the above steps, the project is created. Now create a new simulator to run it to see the effect. Click the third mobile phone icon from the right, and then create a new device according to your needs. After the device is created, click the triangle icon to run the project
    insert image description here

  • Running effect
    insert image description here
    insert image description here
    At this point, a simple Android project is completed!

Guess you like

Origin blog.csdn.net/m0_55887872/article/details/122148520
Recommended