使用Android studio创建一个简单项目

在刚开始学Android开发时,下载好了Android studio,但是不知道如何下手,现在就通过一个简单的小项目熟悉如何使用这个软件进行Android开发。

  • 前提:下载好Android studio并配置好相关环境。

首先介绍一下Android开发过程中需要修改的三类文件:

  • 位于java包下的各类activity文件:实现了用户与软件的交互,主要为java代码实现。
  • 位于res包下的.xml文件:在layout包里的为布局文件,即Android界面显示的视图,而drawable里则放置了某个控件的样式,设计好后可以直接在布局文件里调用。
  • AndroidManifest.xml:是Android应用程序里的清单文件,所有的activity文件都需要在这里面声明才可以使用。

首先来看一下这个项目,只有简单的四个控件:文本显示、文本输入、“放大”按钮以及“修改按钮”,可以点击“放大”使得上方文本字体变大,也可以在文本输入中输入想要显示的内容,然后点击“修改”即可更新上方文本显示的内容。
在这里插入图片描述
下面通过Android studio实现:

  • 创建一个新项目:点击Start a new Android Studio project,选择Empty Activity。
  • 编辑自动生成的activity_main.xml文件,点击下方的Text选项编辑代码(这里的布局我用的是RelativeLayout相对布局)
 <?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>
  • 在drawable下新建一个xml文件(这里我命名为bg_btn4.xml),为按钮控件的样式
 <?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>
  • 编辑自动生成的MainActivity.java文件
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());//设置字符

    }
}
  • 完成以上步骤项目就创建好了,现在新建一个模拟器运行看看效果。点击右数第三个手机图标,然后按照自己需求新建一个设备。设备新建完成后点击三角形图标运行此项目
    在这里插入图片描述

  • 运行效果
    在这里插入图片描述
    在这里插入图片描述
    到此一个简单的Android项目就完成了!

猜你喜欢

转载自blog.csdn.net/m0_55887872/article/details/122148520