IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> 第一个 Android 程序 -> 正文阅读

[移动开发]第一个 Android 程序

Android Studio 创建项目

??启动 Android Studio 进入到下图所示界面。如果选哟创建新的项目,选择 Create New Project,如果是打开现有项目,选择 Open an Existing Project。

在这里插入图片描述

??这里演示一下创建新项目的流程。选择 Empty Activity 点击 Next。

??这里选中 Empty Activity 是因为这个选项会默认帮我们创建好一个 Activity 的模板,作为初次接触 Android 的童鞋,选择 Empty Activity 会比较容易。而 No Activity 需要我们手动创建,至于如何手动创建 Activity 之后在 Activity 部分会讲到的。

在这里插入图片描述

??在接下来的页面中,修改好项目名称、包名、保存路径、使用的语言(默认是 Kotlin,需要修改为 Java),点击 Finish 即可。然后就会进入到 Android Studio 的编辑界面,此时需要等待比较长的一段加载时间,加载完成之后,即可见到下面的界面。

在这里插入图片描述

首个 Android 项目

??res/layout/activity_main.xml 文件是布局文件,在这里面的控件会显示在 Activity 页面中。

在这里插入图片描述

MainActivity 类的代码:

package com.gm.audio;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button mTrueButton = null;
    private Button mFalseButton = null;
    private Button mNextButton = null;
    private TextView mTextView = null;
    private int mCurrentIndex = 0;
    private QuestionModel[] mQuestionList = new QuestionModel[]{
            new QuestionModel(R.string.q1, true),
            new QuestionModel(R.string.q2, false),
            new QuestionModel(R.string.q3, true),
            new QuestionModel(R.string.q4, true),
            new QuestionModel(R.string.q5, false),
            new QuestionModel(R.string.q6, true),
            new QuestionModel(R.string.q7, true)
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTrueButton  = (Button) findViewById(R.id.true_button);
        mFalseButton = (Button) findViewById(R.id.false_button);
        mNextButton  = (Button) findViewById(R.id.next_button);
        mTextView    = (TextView) findViewById(R.id.textView);
        mTextView.setText(mQuestionList[0].getQuestion());

        mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentIndex = ++mCurrentIndex%mQuestionList.length;
                mTextView.setText(mQuestionList[mCurrentIndex].getQuestion());
            }
        });

        mTrueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast toast = Toast.makeText(MainActivity.this, R.string.correct, Toast.LENGTH_LONG);
                toast.setGravity(Gravity.BOTTOM, 0, 100);
                toast.show();
            }
        });

        mFalseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast toast = Toast.makeText(MainActivity.this, R.string.incorrect, Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER,0, 0);
                toast.show();
            }
        });

        mNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentIndex = ++mCurrentIndex % 7;
                mTextView.setText(mQuestionList[mCurrentIndex].getQuestion());
            }
        });
    }
}

QuestionMode 类的代码:

package com.gm.audio;

public class QuestionModel {
    private int mQuestion;
    private boolean mAnswer;
    public QuestionModel(int mQuestion, boolean mAnswer){
        this.mAnswer   = mAnswer;
        this.mQuestion = mQuestion;
    }

    public int getQuestion() {
        return mQuestion;
    }

    public boolean isAnswer() {
        return mAnswer;
    }
}

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=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp"
        android:textSize="20dp" />

    <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:layout_marginRight="10dp"
            android:text="@string/app_true" />

        <Button
            android:id="@+id/false_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_false" />
        
    </LinearLayout>
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/next_button"
        android:text="@string/next_btn"/>
</LinearLayout>
  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-07-17 12:03:47  更:2021-07-17 12:05:27 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/16 4:19:41-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码