天天看点

Android开发初体验一、搭建工程二、开发初体验三、应用的运行

Android开发初体验(Android Studio4.1.1版本)

  • 一、搭建工程
  • 二、开发初体验
  • 三、应用的运行
    • 1.在模拟器上运行
    • 2.在真实机上运行

一、搭建工程

1.启动Android Studion程序,点击创建新的工程,如下图所示

Android开发初体验一、搭建工程二、开发初体验三、应用的运行

2.进入下面的界面,可以看到,Android设备有很多,比如手机和平板电脑、可穿戴设备(手环之类的)、Android电视、汽车、其它,这里选择手机就行了,然后选择“Emptp Activity”的空白项目,点击Next继续

Android开发初体验一、搭建工程二、开发初体验三、应用的运行

3.进入下面的界面工程名可以自己修改,开发语言选择自己熟悉的就可以,其它可以默认,或是这一页面全部默认也是可以的,点击Finish,如下图所示

Android开发初体验一、搭建工程二、开发初体验三、应用的运行

4.进入了Android Studio页面,继续等待几分钟后,自动加载完成,如图所示

Android开发初体验一、搭建工程二、开发初体验三、应用的运行

二、开发初体验

1.点击

MainActivity.java

页面,进行程序的编写,如下图所示

Android开发初体验一、搭建工程二、开发初体验三、应用的运行

MainActivity.java

代码

package com.example.geoquiz;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
    private Button mTrueButton; //添加两个按钮成员变量,m开头表示menmber成员变量
    private Button mFalseButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTrueButton = (Button) findViewById(R.id.true_button);//引用组件
        //为按钮设置监听器,使用匿名内部类
        mTrueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //创建提示消息
                Toast.makeText(MainActivity.this, R.string.incorrect_toast, Toast.LENGTH_SHORT).show();
            }
        });
        mFalseButton = (Button) findViewById(R.id.false_button);
        mFalseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, R.string.correct_toast,
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
}
           

2.两按钮的间距可从布局中直接拖动到显示界面,或是通过程序的编写来现实,如下图所示

Android开发初体验一、搭建工程二、开发初体验三、应用的运行

3.点击进入

activity_main.xml

页面,进行程序的编写,如图所示

Android开发初体验一、搭建工程二、开发初体验三、应用的运行

activity_main.xml

代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
    android:orientation="vertical"
    tools:context="com.example.geoquiz.MainActivity">
    <!--android:orientation="vertical"垂直布局-->

    <!--android:padding="24dp"内边距-->
    <!--android:text="@string/question_text"对字符串资源的引用-->

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp"
        android:text="@string/question_text" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/true_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/true_button"
            android:layout_marginRight="8dp"
            />
        <Button
            android:id="@+id/false_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/false_button"
            android:layout_marginLeft="8dp"
            />

    </LinearLayout>
    <!--android:orientation="horizontal"水平布局-->
</LinearLayout>

           

4.进入

strings.xml

页面,进行程序的编写,如图所示

Android开发初体验一、搭建工程二、开发初体验三、应用的运行

strings.xml

代码

<resources>
    <string name="app_name">GeoQuiz</string>
    <string name="question_text">
        Constantinople is the largest city in Turkey.
    </string>
    <string name="true_button">TRUE</string>
    <string name="false_button">FALSE</string>
    <string name="incorrect_toast">Correct1!</string>
    <string name="correct_toast">Correct0!</string>
</resources>
           

三、应用的运行

1.在模拟器上运行

1.第1步选择自己下载好的模拟器,第2步点击运行(还没下载过的模拟器的先点击模拟器下载,之前的文章我做过详细的介绍及下载步骤)

Android开发初体验一、搭建工程二、开发初体验三、应用的运行

2.点击按下“TRUE”按钮,会弹出对应的消息框“Correct1!”,如按下“FALSE”按钮,会弹出对应的消息框“Correct0!”,若两个按钮都没有按下,不会弹出任何消息框,如图所示

Android开发初体验一、搭建工程二、开发初体验三、应用的运行

2.在真实机上运行

1.我本人用的手机型号是小米手机,到设置打开手机的开发者选项,进入开发者选项,打开USB调试,还需要打开USB安装。

Android开发初体验一、搭建工程二、开发初体验三、应用的运行

2.插入usb数据线与电脑连接,选择手机型号后点击运行,在手机上点击“继续安装”应用,安装完成后运行结果如下图所示

Android开发初体验一、搭建工程二、开发初体验三、应用的运行