Android入门教程(二)

来源:互联网 发布:贝思外贸软件数据导出 编辑:程序博客网 时间:2024/05/18 01:51

Hello World 项目

首先当我们启动Android Studio的虚拟机时,可以看到第一个项目Hello World,那么虚拟机中的Hello World!是如何书写的呢?

看看虚拟机运行结果截图吧!

这里写图片描述

根据Android入门教程(一)介绍了项目结构,那么就知道布局文件就是书写界面文件如图:

这里写图片描述

那么我们来了解一下其中的TextView标签,如图:

<TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Hello World!"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintLeft_toLeftOf="parent"        app:layout_constraintRight_toRightOf="parent"        app:layout_constraintTop_toTopOf="parent"/>

再来看看这个截图:

这里写图片描述

我们可以看到虚拟机中,软件的名称 就是 Boxuegu ,可以知道就是从这里从来的。那么这个是又传到哪里了呢?在看看这里的截图:

这里写图片描述

可以知道这里 android:label=”@string/app_name”,就是用来显示软件名称的。其中可以先了解一下这几行代码:

<activity android:name=".Boxuegu">            <intent-filter>                <action android:name="android.intent.action.MAIN"/>                <category android:name="android.intent.category.LAUNCHER"/>            </intent-filter></activity>

那么我们来做个TextViewtest的小练习,更改如下代码,在res/values/strings.xml文件中更新代码如下:

<resources>    <string name="app_name">Boxuegu</string>    <string name="textView_text">欢迎您!</string></resources>

那么我们如何用textView_text呢?在布局文件中更新代码res/layout/ activity _ boxuegu.xml的文件中:

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/textView_text"/></LinearLayout>

附上运行截图:

这里写图片描述

就可以知道用android:text=”@string/textView_text”就可以引用此语句,text文本属性,那么同类,如果想改软件名称既可以在strings.xml文件中改文字,也可以在AndroidManifest.xml文件中改引用语句。

如果你想说,我要在Java类中表示,那么请看如下代码,我们在布局文件中定义一个文本id就可以在类中书写代码,布局文件更新:

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <TextView        android:id="@+id/text"  //定义        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/textView_text"       /></LinearLayout>

那么接下来更新类代码 Boxuegu.java 如下:

package cn.edu.gdmec.android.boxuegu;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.TextView;public class Boxuegu extends AppCompatActivity {    private TextView text;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate ( savedInstanceState );        setContentView ( R.layout.activity_boxuegu );        text = (TextView) findViewById ( R.id.text );        String name = "GD阿達";        text.setText ( name );    }}

首先定义一个test变量,其实setContentView是载入布局文件的,那么我们接下来利用 test 变量 = (TextView) {强制类型} 利用 findViewById 函数,利用id,找到我们想要的TextView对象,其中String name = “GD阿達”; 就如同赋值,然后通过text.setText显示出来就好。

运行截图如下:

这里写图片描述

现在你了解了吗?细心的人会发现在布局中

<TextView        android:id="@+id/text"  //定义        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/textView_text"       />

如代码出现:android:text=”@string/textView_text还存在,然而在虚拟机中不出现之前的文件,而是被id替代,那么这就是留下的问题,希望你们来解答,欢迎在评论中有你精彩的表现。

关注我,每天都有优质技术文章推送,工作,学习累了的时候放松一下自己。

本篇文章同步微信公众号

欢迎大家关注我的微信公众号:「醉翁猫咪」

这里写图片描述

阅读全文
'); })();
1 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 下饭家常菜 米饭炒菜哪些菜下饭 配米饭的下饭菜 简单下饭菜做法大全 下饭菜什么好 儿童下饭菜 孕妇下饭菜 家常下饭菜菜谱图解 下饭菜图片 家常下饭菜菜谱 简单家常下饭菜辣的菜谱 开胃下饭菜做法大全 简单饭菜的做法大全 自制下饭菜 米饭下饭菜 米饭下饭菜都有哪些 下饭菜怎么做 四川下饭菜菜谱 好吃的饭菜图片 什么下饭菜好吃 好吃的下饭菜菜谱大全 下饭菜的做法 米饭的下饭菜家常做法 拌饭菜 简单的下饭菜 什么的饭菜 家庭下饭菜 下饭菜做法 饭菜的图片 宝宝下饭菜 百吃不厌的下饭菜 开胃菜 自制下饭辣椒酱 辣酱炒饭 下饭酱的做法 自制下饭酱 下饭酱 烤酱 酱焖 熟酱 海天招牌拌饭酱