Android开发初体验

Android开发初体验

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

前言

通过几天学习,慢慢掌握了一些Android Studion知识,下面就是我学习的第二个实验(仅供参考):

一、搭建工程

1.启动Android Studion程序,点击File创建新的工程。在这里插入图片描述
2.进入下面的界面,然后选择“Emptp Activity”的空白项目,点击Next继续。在这里插入图片描述
3.进入下面的界面工程名和包名可以改,Laguage这里建议选择Java,然后点击Finish,在这里插入图片描述

二、开发初体验

1.点击 MainActivity.java 页面,进行程序的编写在这里插入图片描述
2.点击进入strings.xml页面,进行程序的编写。
在这里插入图片描述
3.点击进入activity_main.xml页面,进行程序的编写
在这里插入图片描述

附代码如下(示例):

这是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();
            }
        });
    }
}

这是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>

这是activity_main.xm的代码

   <?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>

三、应用的运行

1.如果不会找虚拟机请看我的第二篇文章,这就是虚拟机运行后的结果:
在这里插入图片描述
2.真实机运行后的结果:
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值